001/*
002 * Copyright 2009-2022 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2009-2022 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) 2009-2022 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.migrate.ldapjdk;
037
038
039
040import java.io.Serializable;
041import java.util.ArrayList;
042import java.util.Iterator;
043
044import com.unboundid.util.Mutable;
045import com.unboundid.util.NotExtensible;
046import com.unboundid.util.NotNull;
047import com.unboundid.util.ThreadSafety;
048import com.unboundid.util.ThreadSafetyLevel;
049
050
051
052/**
053 * This class provides a data structure that represents a set of LDAP
054 * modifications.
055 * <BR><BR>
056 * This class is primarily intended to be used in the process of updating
057 * applications which use the Netscape Directory SDK for Java to switch to or
058 * coexist with the UnboundID LDAP SDK for Java.  For applications not written
059 * using the Netscape Directory SDK for Java, an array or collection of
060 * {@link com.unboundid.ldap.sdk.Modification} objects should be used instead.
061 */
062@NotExtensible()
063@Mutable()
064@ThreadSafety(level=ThreadSafetyLevel.NOT_THREADSAFE)
065public class LDAPModificationSet
066       implements Serializable
067{
068  /**
069   * The serial version UID for this serializable class.
070   */
071  private static final long serialVersionUID = -1789929614205832665L;
072
073
074
075  // The list of modifications.
076  @NotNull private final ArrayList<LDAPModification> mods;
077
078
079
080  /**
081   * Creates an empty set of modifications.
082   */
083  public LDAPModificationSet()
084  {
085    mods = new ArrayList<>(1);
086  }
087
088
089
090  /**
091   * Adds a modification to this modification set.
092   *
093   * @param  op    The modification type for the modification.
094   * @param  attr  The attribute for the modification.
095   */
096  public void add(final int op, @NotNull final LDAPAttribute attr)
097  {
098    mods.add(new LDAPModification(op, attr));
099  }
100
101
102
103  /**
104   * Retrieves the LDAP modification at the specified position in this
105   * modification set.
106   *
107   * @param  index  The position of the LDAP modification to retrieve.
108   *
109   * @return  The requested modification.
110   *
111   * @throws  IndexOutOfBoundsException  If the provided index is invalid.
112   */
113  @NotNull()
114  public LDAPModification elementAt(final int index)
115         throws IndexOutOfBoundsException
116  {
117    return mods.get(index);
118  }
119
120
121
122  /**
123   * Removes the LDAP modification at the specified position in this
124   * modification set.
125   *
126   * @param  index  The position of the LDAP modification to remove.
127   *
128   * @throws  IndexOutOfBoundsException  If the provided index is invalid.
129   */
130  public void removeElementAt(final int index)
131         throws IndexOutOfBoundsException
132  {
133    mods.remove(index);
134  }
135
136
137
138  /**
139   * Removes the first LDAP modification in this set targeting the specified
140   * attribute.
141   *
142   * @param  name  The name of the attribute to remove.
143   */
144  public void remove(@NotNull final String name)
145  {
146    final Iterator<LDAPModification> iterator = mods.iterator();
147    while (iterator.hasNext())
148    {
149      final LDAPModification mod = iterator.next();
150      if (mod.getAttribute().getName().equalsIgnoreCase(name))
151      {
152        iterator.remove();
153        return;
154      }
155    }
156  }
157
158
159
160  /**
161   * Retrieves the number of modifications in this modification set.
162   *
163   * @return  The number of modifications in this modification set.
164   */
165  public int size()
166  {
167    return mods.size();
168  }
169
170
171
172  /**
173   * Retrieves the contents of this set as an array of LDAP modifications.
174   *
175   * @return  An array of the LDAP modifications contained in this set.
176   */
177  @NotNull()
178  public LDAPModification[] toArray()
179  {
180    final LDAPModification[] modArray = new LDAPModification[mods.size()];
181    return mods.toArray(modArray);
182  }
183
184
185
186  /**
187   * Retrieves a string representation of this modification set.
188   *
189   * @return  A string representation of this modification set.
190   */
191  @Override()
192  @NotNull()
193  public String toString()
194  {
195    return mods.toString();
196  }
197}