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