001    /*
002     * Copyright 2016 UnboundID Corp.
003     * All Rights Reserved.
004     */
005    /*
006     * Copyright (C) 2016 UnboundID Corp.
007     *
008     * This program is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU General Public License (GPLv2 only)
010     * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
011     * as published by the Free Software Foundation.
012     *
013     * This program is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016     * GNU General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with this program; if not, see <http://www.gnu.org/licenses>.
020     */
021    package com.unboundid.ldif;
022    
023    
024    
025    import java.util.ArrayList;
026    import java.util.Collection;
027    import java.util.Collections;
028    import java.util.List;
029    
030    import com.unboundid.ldap.sdk.Entry;
031    import com.unboundid.util.StaticUtils;
032    import com.unboundid.util.ThreadSafety;
033    import com.unboundid.util.ThreadSafetyLevel;
034    
035    
036    
037    /**
038     * This class provides an implementation of an LDIF reader entry translator that
039     * can be used to invoke multiple LDIF reader entry translators for each entry
040     * to be processed.
041     */
042    @ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
043    public final class AggregateLDIFReaderEntryTranslator
044           implements LDIFReaderEntryTranslator
045    {
046      // The set of LDIF reader entry translators to be invoked for each entry to
047      // process.
048      private final List<LDIFReaderEntryTranslator> translators;
049    
050    
051    
052      /**
053       * Creates a new aggregate LDIF reader entry translator that will invoke all
054       * of the provided translators for each entry to be processed.
055       *
056       * @param  translators  The set of LDIF reader entry translators to be invoked
057       *                      for each entry to be processed.
058       */
059      public AggregateLDIFReaderEntryTranslator(
060                  final LDIFReaderEntryTranslator... translators)
061      {
062        this(StaticUtils.toList(translators));
063      }
064    
065    
066    
067      /**
068       * Creates a new aggregate LDIF reader entry translator that will invoke all
069       * of the provided translators for each entry to be processed.
070       *
071       * @param  translators  The set of LDIF reader entry translators to be invoked
072       *                      for each entry to be processed.
073       */
074      public AggregateLDIFReaderEntryTranslator(
075                  final Collection<? extends LDIFReaderEntryTranslator> translators)
076      {
077        if (translators == null)
078        {
079          this.translators = Collections.emptyList();
080        }
081        else
082        {
083          this.translators = Collections.unmodifiableList(
084               new ArrayList<LDIFReaderEntryTranslator>(translators));
085        }
086      }
087    
088    
089    
090      /**
091       * {@inheritDoc}
092       */
093      public Entry translate(final Entry original, final long firstLineNumber)
094             throws LDIFException
095      {
096        if (original == null)
097        {
098          return null;
099        }
100    
101        Entry e = original;
102        for (final LDIFReaderEntryTranslator t : translators)
103        {
104          e = t.translate(e, firstLineNumber);
105          if (e == null)
106          {
107            return null;
108          }
109        }
110    
111        return e;
112      }
113    }