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