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.Arrays;
043import java.util.Enumeration;
044import java.util.Iterator;
045
046import com.unboundid.util.Mutable;
047import com.unboundid.util.NotExtensible;
048import com.unboundid.util.NotNull;
049import com.unboundid.util.Nullable;
050import com.unboundid.util.StaticUtils;
051import com.unboundid.util.ThreadSafety;
052import com.unboundid.util.ThreadSafetyLevel;
053
054
055
056/**
057 * This class provides a data structure that contains a set of LDAP attribute
058 * objects.
059 * <BR><BR>
060 * This class is primarily intended to be used in the process of updating
061 * applications which use the Netscape Directory SDK for Java to switch to or
062 * coexist with the UnboundID LDAP SDK for Java.  For applications not written
063 * using the Netscape Directory SDK for Java, arrays or collections of
064 * {@link com.unboundid.ldap.sdk.Attribute} objects should be used instead.
065 */
066@NotExtensible()
067@Mutable()
068@ThreadSafety(level=ThreadSafetyLevel.NOT_THREADSAFE)
069public class LDAPAttributeSet
070       implements Serializable
071{
072  /**
073   * The serial version UID for this serializable class.
074   */
075  private static final long serialVersionUID = -4872457565092606186L;
076
077
078
079  // The list of LDAPAttribute objects.
080  @NotNull private final ArrayList<LDAPAttribute> attributes;
081
082
083
084  /**
085   * Creates a new LDAP attribute set with no attributes.
086   */
087  public LDAPAttributeSet()
088  {
089    attributes = new ArrayList<>(20);
090  }
091
092
093
094  /**
095   * Creates a new LDAP attribute set with the provided attributes.
096   *
097   * @param  attrs  The set of attributes to include in the set.
098   */
099  public LDAPAttributeSet(@NotNull final LDAPAttribute[] attrs)
100  {
101    attributes = new ArrayList<>(Arrays.asList(attrs));
102  }
103
104
105
106  /**
107   * Creates a new LDAP attribute set with the provided attributes.
108   *
109   * @param  attrs  The set of attributes to include in the set.
110   */
111  private LDAPAttributeSet(@NotNull final ArrayList<LDAPAttribute> attrs)
112  {
113    attributes = new ArrayList<>(attrs);
114  }
115
116
117
118  /**
119   * Retrieves an enumeration of the attributes in this set.
120   *
121   * @return  An enumeration of the attributes in this set.
122   */
123  @NotNull()
124  public Enumeration<LDAPAttribute> getAttributes()
125  {
126    return new IterableEnumeration<>(attributes);
127  }
128
129
130
131  /**
132   * Retrieves a subset of the attributes in this attribute set which contain
133   * the specified subtype.
134   *
135   * @param  subtype  The subtype for which to retrieve all of the attributes.
136   *
137   * @return  A new attribute set with all attributes from this set containing
138   *          the specified subtype.
139   */
140  @NotNull()
141  public LDAPAttributeSet getSubset(@NotNull final String subtype)
142  {
143    final ArrayList<LDAPAttribute> subset = new ArrayList<>(attributes.size());
144
145    for (final LDAPAttribute a : attributes)
146    {
147      if (a.hasSubtype(subtype))
148      {
149        subset.add(a);
150      }
151    }
152
153    return new LDAPAttributeSet(subset);
154  }
155
156
157
158  /**
159   * Retrieves the attribute from this set whose name exactly matches the
160   * provided name.
161   *
162   * @param  attrName  The name of the attribute to retrieve.
163   *
164   * @return  The requested attribute, or {@code null} if there is no such
165   *          attribute in this set.
166   */
167  @Nullable()
168  public LDAPAttribute getAttribute(@NotNull final String attrName)
169  {
170    for (final LDAPAttribute a : attributes)
171    {
172      if (a.getName().equalsIgnoreCase(attrName))
173      {
174        return a;
175      }
176    }
177
178    return null;
179  }
180
181
182
183  /**
184   * Retrieves the attribute with the specified base name and the specified
185   * language subtype.
186   *
187   * @param  attrName  The base name for the attribute to retrieve.
188   * @param  lang      The language subtype to retrieve, or {@code null} if
189   *                   there should not be a language subtype.
190   *
191   * @return  The attribute with the specified base name and language subtype,
192   *          or {@code null} if there is no such attribute.
193   */
194  @Nullable()
195  public LDAPAttribute getAttribute(@NotNull final String attrName,
196                                    @Nullable final String lang)
197  {
198    if (lang == null)
199    {
200      return getAttribute(attrName);
201    }
202
203    final String lowerLang = StaticUtils.toLowerCase(lang);
204
205    for (final LDAPAttribute a : attributes)
206    {
207      if (a.getBaseName().equalsIgnoreCase(attrName))
208      {
209        final String[] subtypes = a.getSubtypes();
210        if (subtypes != null)
211        {
212          for (final String s : subtypes)
213          {
214            final String lowerOption = StaticUtils.toLowerCase(s);
215            if (lowerOption.equals(lowerLang) ||
216                lowerOption.startsWith(lang + '-'))
217            {
218              return a;
219            }
220          }
221        }
222      }
223    }
224
225    return null;
226  }
227
228
229
230  /**
231   * Retrieves the attribute at the specified position in this attribute set.
232   *
233   * @param  index  The position of the attribute to retrieve.
234   *
235   * @return  The attribute at the specified position.
236   *
237   * @throws  IndexOutOfBoundsException  If the provided index invalid.
238   */
239  @NotNull()
240  public LDAPAttribute elementAt(final int index)
241         throws IndexOutOfBoundsException
242  {
243    return attributes.get(index);
244  }
245
246
247
248  /**
249   * Adds the provided attribute to this attribute set.
250   *
251   * @param  attr  The attribute to be added to this set.
252   */
253  public void add(@NotNull final LDAPAttribute attr)
254  {
255    for (final LDAPAttribute a : attributes)
256    {
257      if (attr.getName().equalsIgnoreCase(a.getName()))
258      {
259        for (final byte[] value : attr.getByteValueArray())
260        {
261          a.addValue(value);
262        }
263        return;
264      }
265    }
266
267    attributes.add(attr);
268  }
269
270
271
272  /**
273   * Removes the attribute with the specified name.
274   *
275   * @param  name  The name of the attribute to remove.
276   */
277  public void remove(@NotNull final String name)
278  {
279    final Iterator<LDAPAttribute> iterator = attributes.iterator();
280    while (iterator.hasNext())
281    {
282      final LDAPAttribute a = iterator.next();
283      if (name.equalsIgnoreCase(a.getName()))
284      {
285        iterator.remove();
286        return;
287      }
288    }
289  }
290
291
292
293  /**
294   * Removes the attribute at the specified position in this attribute set.
295   *
296   * @param  index  The position of the attribute to remove.
297   *
298   * @throws  IndexOutOfBoundsException  If the provided index is invalid.
299   */
300  public void removeElementAt(final int index)
301         throws IndexOutOfBoundsException
302  {
303    attributes.remove(index);
304  }
305
306
307
308  /**
309   * Retrieves the number of attributes contained in this attribute set.
310   *
311   * @return  The number of attributes contained in this attribute set.
312   */
313  public int size()
314  {
315    return attributes.size();
316  }
317
318
319
320  /**
321   * Creates a duplicate of this attribute set.
322   *
323   * @return  A duplicate of this attribute set.
324   */
325  @NotNull()
326  public LDAPAttributeSet duplicate()
327  {
328    return new LDAPAttributeSet(attributes);
329  }
330
331
332
333  /**
334   * Retrieves a string representation of this attribute set.
335   *
336   * @return  A string representation of this attribute set.
337   */
338  @Override()
339  @NotNull()
340  public String toString()
341  {
342    return attributes.toString();
343  }
344}