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