001/*
002 * Copyright 2009-2014 UnboundID Corp.
003 * All Rights Reserved.
004 */
005/*
006 * Copyright (C) 2009-2014 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 */
021package com.unboundid.ldap.sdk.migrate.ldapjdk;
022
023
024
025import java.io.Serializable;
026import java.util.Arrays;
027import java.util.Enumeration;
028import java.util.Set;
029
030import com.unboundid.ldap.sdk.Attribute;
031import com.unboundid.util.Mutable;
032import com.unboundid.util.NotExtensible;
033import com.unboundid.util.ThreadSafety;
034import com.unboundid.util.ThreadSafetyLevel;
035
036import static com.unboundid.util.StaticUtils.*;
037
038
039
040/**
041 * This class provides a data structure that holds information about an LDAP
042 * attribute, including an attribute description (a base name or OID and
043 * optional set of options) and zero or more values.
044 * <BR><BR>
045 * This class is primarily intended to be used in the process of updating
046 * applications which use the Netscape Directory SDK for Java to switch to or
047 * coexist with the UnboundID LDAP SDK for Java.  For applications not written
048 * using the Netscape Directory SDK for Java, the {@link Attribute} class should
049 * be used instead.
050 */
051@NotExtensible()
052@Mutable()
053@ThreadSafety(level=ThreadSafetyLevel.NOT_THREADSAFE)
054public class LDAPAttribute
055       implements Serializable
056{
057  /**
058   * The serial version UID for this serializable attribute.
059   */
060  private static final long serialVersionUID = 839217229050750570L;
061
062
063
064  // The Attribute object wrapped by this LDAPAttribute.
065  private Attribute attribute;
066
067
068
069  /**
070   * Creates a new LDAP attribute from the provided {@link Attribute} object.
071   *
072   * @param  attr  The LDAP attribute to use to create this attribute.
073   */
074  public LDAPAttribute(final Attribute attr)
075  {
076    attribute = attr;
077  }
078
079
080
081  /**
082   * Creates a new LDAP attribute that is a duplicate of the provided attribute.
083   *
084   * @param  attr  The LDAP attribute to use to create this attribute.
085   */
086  public LDAPAttribute(final LDAPAttribute attr)
087  {
088    attribute = attr.attribute;
089  }
090
091
092
093  /**
094   * Creates a new LDAP attribute with the specified name and no values.
095   *
096   * @param  attrName  The name for this attribute.
097   */
098  public LDAPAttribute(final String attrName)
099  {
100    attribute = new Attribute(attrName);
101  }
102
103
104
105  /**
106   * Creates a new LDAP attribute with the specified name and value.
107   *
108   * @param  attrName   The name for this attribute.
109   * @param  attrBytes  The value for this attribute.
110   */
111  public LDAPAttribute(final String attrName, final byte[] attrBytes)
112  {
113    attribute = new Attribute(attrName, attrBytes);
114  }
115
116
117
118  /**
119   * Creates a new LDAP attribute with the specified name and value.
120   *
121   * @param  attrName    The name for this attribute.
122   * @param  attrString  The value for this attribute.
123   */
124  public LDAPAttribute(final String attrName, final String attrString)
125  {
126    attribute = new Attribute(attrName, attrString);
127  }
128
129
130
131  /**
132   * Creates a new LDAP attribute with the specified name and values.
133   *
134   * @param  attrName     The name for this attribute.
135   * @param  attrStrings  The values for this attribute.
136   */
137  public LDAPAttribute(final String attrName, final String[] attrStrings)
138  {
139    attribute = new Attribute(attrName, attrStrings);
140  }
141
142
143
144  /**
145   * Retrieves the name for this attribute.
146   *
147   * @return  The name for this attribute.
148   */
149  public String getName()
150  {
151    return attribute.getName();
152  }
153
154
155
156  /**
157   * Retrieves the base name for this attribute, without any options.
158   *
159   * @return  The base name for this attribute.
160   */
161  public String getBaseName()
162  {
163    return attribute.getBaseName();
164  }
165
166
167
168  /**
169   * Retrieves the base name for the attribute with the provided name.
170   *
171   * @param  attrName  The attribute name for which to retrieve the base name.
172   *
173   * @return  The base name for the attribute with the provided name.
174   */
175  public static String getBaseName(final String attrName)
176  {
177    return Attribute.getBaseName(attrName);
178  }
179
180
181
182  /**
183   * Retrieves the subtypes (i.e., attribute options) contained in the name for
184   * this attribute.
185   *
186   * @return  The subtypes contained in the name for this attribute, or
187   *          {@code null} if there are none.
188   */
189  public String[] getSubtypes()
190  {
191    final Set<String> optionSet = attribute.getOptions();
192    if (optionSet.isEmpty())
193    {
194      return null;
195    }
196
197    final String[] options = new String[optionSet.size()];
198    return optionSet.toArray(options);
199  }
200
201
202
203  /**
204   * Retrieves the subtypes (i.e., attribute options) contained in the provided
205   * attribute name.
206   *
207   * @param  attrName  The attribute name from which to extract the subtypes.
208   *
209   * @return  The subtypes contained in the provided attribute name, or
210   *          {@code null} if there are none.
211   */
212  public static String[] getSubtypes(final String attrName)
213  {
214    return new LDAPAttribute(attrName).getSubtypes();
215  }
216
217
218
219  /**
220   * Retrieves the language subtype (i.e., the attribute option which begins
221   * with "lang-") for this attribute, if present.
222   *
223   * @return  The language subtype for this attribute, or {@code null} if there
224   *          is none.
225   */
226  public String getLangSubtype()
227  {
228    for (final String s : attribute.getOptions())
229    {
230      final String lowerName = toLowerCase(s);
231      if (lowerName.startsWith("lang-"))
232      {
233        return s;
234      }
235    }
236
237    return null;
238  }
239
240
241
242  /**
243   * Indicates whether this attribute contains the specified subtype.
244   *
245   * @param  subtype  The subtype for which to make the determination.
246   *
247   * @return  {@code true} if this option has the specified subtype, or
248   *          {@code false} if not.
249   */
250  public boolean hasSubtype(final String subtype)
251  {
252    return attribute.hasOption(subtype);
253  }
254
255
256
257  /**
258   * Indicates whether this attribute contains all of the specified subtypes.
259   *
260   * @param  subtypes  The subtypes for which to make the determination.
261   *
262   * @return  {@code true} if this option has all of the specified subtypes, or
263   *          {@code false} if not.
264   */
265  public boolean hasSubtypes(final String[] subtypes)
266  {
267    for (final String s : subtypes)
268    {
269      if (! attribute.hasOption(s))
270      {
271        return false;
272      }
273    }
274
275    return true;
276  }
277
278
279
280  /**
281   * Retrieves an enumeration over the string values for this attribute.
282   *
283   * @return  An enumeration over the string values for this attribute.
284   */
285  public Enumeration<String> getStringValues()
286  {
287    return new IterableEnumeration<String>(
288         Arrays.asList(attribute.getValues()));
289  }
290
291
292
293  /**
294   * Retrieves an array of the values for this attribute.
295   *
296   * @return  An array of the values for this attribute.
297   */
298  public String[] getStringValueArray()
299  {
300    return attribute.getValues();
301  }
302
303
304
305  /**
306   * Retrieves an enumeration over the binary values for this attribute.
307   *
308   * @return  An enumeration over the binary values for this attribute.
309   */
310  public Enumeration<byte[]> getByteValues()
311  {
312    return new IterableEnumeration<byte[]>(
313         Arrays.asList(attribute.getValueByteArrays()));
314  }
315
316
317
318  /**
319   * Retrieves an array of the values for this attribute.
320   *
321   * @return  An array of the values for this attribute.
322   */
323  public byte[][] getByteValueArray()
324  {
325    return attribute.getValueByteArrays();
326  }
327
328
329
330
331  /**
332   * Adds the provided value to the set of values for this attribute.
333   *
334   * @param  attrString  The value to add to this attribute.
335   */
336  public void addValue(final String attrString)
337  {
338    attribute = Attribute.mergeAttributes(attribute,
339         new Attribute(attribute.getName(), attrString));
340  }
341
342
343
344  /**
345   * Adds the provided value to the set of values for this attribute.
346   *
347   * @param  attrBytes  The value to add to this attribute.
348   */
349  public void addValue(final byte[] attrBytes)
350  {
351    attribute = Attribute.mergeAttributes(attribute,
352         new Attribute(attribute.getName(), attrBytes));
353  }
354
355
356
357  /**
358   * Removes the provided value from this attribute.
359   *
360   * @param  attrValue  The value to remove.
361   */
362  public void removeValue(final String attrValue)
363  {
364    attribute = Attribute.removeValues(attribute,
365         new Attribute(attribute.getName(), attrValue));
366  }
367
368
369
370  /**
371   * Removes the provided value from this attribute.
372   *
373   * @param  attrValue  The value to remove.
374   */
375  public void removeValue(final byte[] attrValue)
376  {
377    attribute = Attribute.removeValues(attribute,
378         new Attribute(attribute.getName(), attrValue));
379  }
380
381
382
383  /**
384   * Retrieves the number of values for this attribute.
385   *
386   * @return  The number of values for this attribute.
387   */
388  public int size()
389  {
390    return attribute.size();
391  }
392
393
394
395  /**
396   * Converts this LDAP attribute to an {@link Attribute} object.
397   *
398   * @return  The {@code Attribute} object which corresponds to this LDAP
399   *          attribute.
400   */
401  public final Attribute toAttribute()
402  {
403    return attribute;
404  }
405
406
407
408  /**
409   * Retrieves a string representation of this LDAP attribute.
410   *
411   * @return  A string representation of this LDAP attribute.
412   */
413  @Override()
414  public String toString()
415  {
416    return attribute.toString();
417  }
418}