001/*
002 * Copyright 2019-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2019-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) 2019-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.ldap.sdk.transformations;
037
038
039
040import java.io.Serializable;
041import java.util.Collection;
042import java.util.Collections;
043import java.util.EnumSet;
044import java.util.Set;
045
046import com.unboundid.ldap.sdk.ChangeType;
047import com.unboundid.ldif.LDIFChangeRecord;
048import com.unboundid.util.NotMutable;
049import com.unboundid.util.NotNull;
050import com.unboundid.util.Nullable;
051import com.unboundid.util.StaticUtils;
052import com.unboundid.util.ThreadSafety;
053import com.unboundid.util.ThreadSafetyLevel;
054
055
056
057/**
058 * This class provides an LDIF change record transformation that can exclude
059 * change records that can exclude LDIF change records that match any of a
060 * provided set of change types.  It will not have any effect on LDIF records
061 * that do not contain a change type (which must be entries).
062 */
063@NotMutable()
064@ThreadSafety(level = ThreadSafetyLevel.COMPLETELY_THREADSAFE)
065public final class ExcludeChangeTypeTransformation
066       implements LDIFChangeRecordTransformation, Serializable
067{
068  /**
069   * The serial version UID for this serializable class.
070   */
071  private static final long serialVersionUID = -6927917616913251572L;
072
073
074
075  // The set of change types for records to be excluded.
076  @NotNull private final Set<ChangeType> excludedChangeTypes;
077
078
079
080  /**
081   * Creates a new exclude change type transformation that will exclude change
082   * records with any of the provided change types.
083   *
084   * @param  changeTypes  The set of change types to exclude.
085   */
086  public ExcludeChangeTypeTransformation(
087              @Nullable final ChangeType... changeTypes)
088  {
089    this(StaticUtils.toList(changeTypes));
090  }
091
092
093
094  /**
095   * Creates a new exclude change type transformation that will exclude change
096   * records with any of the provided change types.
097   *
098   * @param  changeTypes  The set of change types to exclude.
099   */
100  public ExcludeChangeTypeTransformation(
101              @Nullable final Collection<ChangeType> changeTypes)
102  {
103    if (changeTypes == null)
104    {
105      excludedChangeTypes = Collections.emptySet();
106    }
107    else
108    {
109      final EnumSet<ChangeType> ctSet = EnumSet.noneOf(ChangeType.class);
110      ctSet.addAll(changeTypes);
111      excludedChangeTypes = Collections.unmodifiableSet(ctSet);
112    }
113  }
114
115
116
117  /**
118   * {@inheritDoc}
119   */
120  @Override()
121  @Nullable()
122  public LDIFChangeRecord transformChangeRecord(
123                               @NotNull final LDIFChangeRecord changeRecord)
124  {
125    if (excludedChangeTypes.contains(changeRecord.getChangeType()))
126    {
127      return null;
128    }
129    else
130    {
131      return changeRecord;
132    }
133  }
134
135
136
137  /**
138   * {@inheritDoc}
139   */
140  @Override()
141  @Nullable()
142  public LDIFChangeRecord translate(@NotNull final LDIFChangeRecord original,
143                                    final long firstLineNumber)
144  {
145    if (excludedChangeTypes.contains(original.getChangeType()))
146    {
147      return null;
148    }
149    else
150    {
151      return original;
152    }
153  }
154
155
156
157  /**
158   * {@inheritDoc}
159   */
160  @Override()
161  @Nullable()
162  public LDIFChangeRecord translateChangeRecordToWrite(
163                               @NotNull final LDIFChangeRecord original)
164  {
165    if (excludedChangeTypes.contains(original.getChangeType()))
166    {
167      return null;
168    }
169    else
170    {
171      return original;
172    }
173  }
174}