1 /*
   2  * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.naming.directory;
  27 
  28 import java.util.Vector;
  29 import java.util.Enumeration;
  30 import java.util.NoSuchElementException;
  31 
  32 import javax.naming.NamingException;
  33 import javax.naming.NamingEnumeration;
  34 import javax.naming.OperationNotSupportedException;
  35 
  36 /**
  37   * This interface represents an attribute associated with a named object.
  38   *<p>
  39   * In a directory, named objects can have associated with them
  40   * attributes.  The {@code Attribute} interface represents an attribute associated
  41   * with a named object.  An attribute contains 0 or more, possibly null, values.
  42   * The attribute values can be ordered or unordered (see {@code isOrdered()}).
  43   * If the values are unordered, no duplicates are allowed.
  44   * If the values are ordered, duplicates are allowed.
  45   *<p>
  46   * The content and representation of an attribute and its values is defined by
  47   * the attribute's <em>schema</em>. The schema contains information
  48   * about the attribute's syntax and other properties about the attribute.
  49   * See {@code getAttributeDefinition()} and
  50   * {@code getAttributeSyntaxDefinition()}
  51   * for details regarding how to get schema information about an attribute
  52   * if the underlying directory service supports schemas.
  53   *<p>
  54   * Equality of two attributes is determined by the implementation class.
  55   * A simple implementation can use {@code Object.equals()} to determine equality
  56   * of attribute values, while a more sophisticated implementation might
  57   * make use of schema information to determine equality.
  58   * Similarly, one implementation might provide a static storage
  59   * structure which simply returns the values passed to its
  60   * constructor, while another implementation might define {@code get()} and
  61   * {@code getAll()}.
  62   * to get the values dynamically from the directory.
  63   *<p>
  64   * Note that updates to {@code Attribute} (such as adding or removing a
  65   * value) do not affect the corresponding representation of the attribute
  66   * in the directory.  Updates to the directory can only be effected
  67   * using operations in the {@code DirContext} interface.
  68   *
  69   * @author Rosanna Lee
  70   * @author Scott Seligman
  71   *
  72   * @see BasicAttribute
  73   * @since 1.3
  74   */
  75 public interface Attribute extends Cloneable, java.io.Serializable {
  76     /**
  77       * Retrieves an enumeration of the attribute's values.
  78       * The behaviour of this enumeration is unspecified
  79       * if the attribute's values are added, changed,
  80       * or removed while the enumeration is in progress.
  81       * If the attribute values are ordered, the enumeration's items
  82       * will be ordered.
  83       *
  84       * @return A non-null enumeration of the attribute's values.
  85       * Each element of the enumeration is a possibly null Object. The object's
  86       * class is the class of the attribute value. The element is null
  87       * if the attribute's value is null.
  88       * If the attribute has zero values, an empty enumeration
  89       * is returned.
  90       * @exception NamingException
  91       *         If a naming exception was encountered while retrieving
  92       *         the values.
  93       * @see #isOrdered
  94       */
  95     NamingEnumeration<?> getAll() throws NamingException;
  96 
  97     /**
  98       * Retrieves one of this attribute's values.
  99       * If the attribute has more than one value and is unordered, any one of
 100       * the values is returned.
 101       * If the attribute has more than one value and is ordered, the
 102       * first value is returned.
 103       *
 104       * @return A possibly null object representing one of
 105       *        the attribute's value. It is null if the attribute's value
 106       *        is null.
 107       * @exception NamingException
 108       *         If a naming exception was encountered while retrieving
 109       *         the value.
 110       * @exception java.util.NoSuchElementException
 111       *         If this attribute has no values.
 112       */
 113     Object get() throws NamingException;
 114 
 115     /**
 116       * Retrieves the number of values in this attribute.
 117       *
 118       * @return The nonnegative number of values in this attribute.
 119       */
 120     int size();
 121 
 122     /**
 123       * Retrieves the id of this attribute.
 124       *
 125       * @return The id of this attribute. It cannot be null.
 126       */
 127     String getID();
 128 
 129     /**
 130       * Determines whether a value is in the attribute.
 131       * Equality is determined by the implementation, which may use
 132       * {@code Object.equals()} or schema information to determine equality.
 133       *
 134       * @param attrVal The possibly null value to check. If null, check
 135       *  whether the attribute has an attribute value whose value is null.
 136       * @return true if attrVal is one of this attribute's values; false otherwise.
 137       * @see java.lang.Object#equals
 138       * @see BasicAttribute#equals
 139       */
 140     boolean contains(Object attrVal);
 141     /**
 142       * Adds a new value to the attribute.
 143       * If the attribute values are unordered and
 144       * {@code attrVal} is already in the attribute, this method does nothing.
 145       * If the attribute values are ordered, {@code attrVal} is added to the end of
 146       * the list of attribute values.
 147       *<p>
 148       * Equality is determined by the implementation, which may use
 149       * {@code Object.equals()} or schema information to determine equality.
 150       *
 151       * @param attrVal The new possibly null value to add. If null, null
 152       *  is added as an attribute value.
 153       * @return true if a value was added; false otherwise.
 154       */
 155     boolean add(Object attrVal);
 156 
 157     /**
 158       * Removes a specified value from the attribute.
 159       * If {@code attrval} is not in the attribute, this method does nothing.
 160       * If the attribute values are ordered, the first occurrence of
 161       * {@code attrVal} is removed and attribute values at indices greater
 162       * than the removed
 163       * value are shifted up towards the head of the list (and their indices
 164       * decremented by one).
 165       *<p>
 166       * Equality is determined by the implementation, which may use
 167       * {@code Object.equals()} or schema information to determine equality.
 168       *
 169       * @param attrval The possibly null value to remove from this attribute.
 170       * If null, remove the attribute value that is null.
 171       * @return true if the value was removed; false otherwise.
 172       */
 173     boolean remove(Object attrval);
 174 
 175     /**
 176       * Removes all values from this attribute.
 177       */
 178     void clear();
 179 
 180     /**
 181       * Retrieves the syntax definition associated with the attribute.
 182       * An attribute's syntax definition specifies the format
 183       * of the attribute's value(s). Note that this is different from
 184       * the attribute value's representation as a Java object. Syntax
 185       * definition refers to the directory's notion of <em>syntax</em>.
 186       *<p>
 187       * For example, even though a value might be
 188       * a Java String object, its directory syntax might be "Printable String"
 189       * or "Telephone Number". Or a value might be a byte array, and its
 190       * directory syntax is "JPEG" or "Certificate".
 191       * For example, if this attribute's syntax is "JPEG",
 192       * this method would return the syntax definition for "JPEG".
 193       * <p>
 194       * The information that you can retrieve from a syntax definition
 195       * is directory-dependent.
 196       *<p>
 197       * If an implementation does not support schemas, it should throw
 198       * OperationNotSupportedException. If an implementation does support
 199       * schemas, it should define this method to return the appropriate
 200       * information.
 201       * @return The attribute's syntax definition. Null if the implementation
 202       *    supports schemas but this particular attribute does not have
 203       *    any schema information.
 204       * @exception OperationNotSupportedException If getting the schema
 205       *         is not supported.
 206       * @exception NamingException If a naming exception occurs while getting
 207       *         the schema.
 208       */
 209 
 210     DirContext getAttributeSyntaxDefinition() throws NamingException;
 211 
 212     /**
 213       * Retrieves the attribute's schema definition.
 214       * An attribute's schema definition contains information
 215       * such as whether the attribute is multivalued or single-valued,
 216       * the matching rules to use when comparing the attribute's values.
 217       *
 218       * The information that you can retrieve from an attribute definition
 219       * is directory-dependent.
 220       *
 221       *<p>
 222       * If an implementation does not support schemas, it should throw
 223       * OperationNotSupportedException. If an implementation does support
 224       * schemas, it should define this method to return the appropriate
 225       * information.
 226       * @return This attribute's schema definition. Null if the implementation
 227       *     supports schemas but this particular attribute does not have
 228       *     any schema information.
 229       * @exception OperationNotSupportedException If getting the schema
 230       *         is not supported.
 231       * @exception NamingException If a naming exception occurs while getting
 232       *         the schema.
 233       */
 234     DirContext getAttributeDefinition() throws NamingException;
 235 
 236     /**
 237       * Makes a copy of the attribute.
 238       * The copy contains the same attribute values as the original attribute:
 239       * the attribute values are not themselves cloned.
 240       * Changes to the copy will not affect the original and vice versa.
 241       *
 242       * @return A non-null copy of the attribute.
 243       */
 244     Object clone();
 245 
 246     //----------- Methods to support ordered multivalued attributes
 247 
 248     /**
 249       * Determines whether this attribute's values are ordered.
 250       * If an attribute's values are ordered, duplicate values are allowed.
 251       * If an attribute's values are unordered, they are presented
 252       * in any order and there are no duplicate values.
 253       * @return true if this attribute's values are ordered; false otherwise.
 254       * @see #get(int)
 255       * @see #remove(int)
 256       * @see #add(int, java.lang.Object)
 257       * @see #set(int, java.lang.Object)
 258       */
 259     boolean isOrdered();
 260 
 261     /**
 262      * Retrieves the attribute value from the ordered list of attribute values.
 263      * This method returns the value at the {@code ix} index of the list of
 264      * attribute values.
 265      * If the attribute values are unordered,
 266      * this method returns the value that happens to be at that index.
 267      * @param ix The index of the value in the ordered list of attribute values.
 268      * {@code 0 <= ix < size()}.
 269      * @return The possibly null attribute value at index {@code ix};
 270      *   null if the attribute value is null.
 271      * @exception NamingException If a naming exception was encountered while
 272      * retrieving the value.
 273      * @exception IndexOutOfBoundsException If {@code ix} is outside the specified range.
 274      */
 275     Object get(int ix) throws NamingException;
 276 
 277     /**
 278      * Removes an attribute value from the ordered list of attribute values.
 279      * This method removes the value at the {@code ix} index of the list of
 280      * attribute values.
 281      * If the attribute values are unordered,
 282      * this method removes the value that happens to be at that index.
 283      * Values located at indices greater than {@code ix} are shifted up towards
 284      * the front of the list (and their indices decremented by one).
 285      *
 286      * @param ix The index of the value to remove.
 287      * {@code 0 <= ix < size()}.
 288      * @return The possibly null attribute value at index {@code ix} that was removed;
 289      *   null if the attribute value is null.
 290      * @exception IndexOutOfBoundsException If {@code ix} is outside the specified range.
 291      */
 292     Object remove(int ix);
 293 
 294     /**
 295      * Adds an attribute value to the ordered list of attribute values.
 296      * This method adds {@code attrVal} to the list of attribute values at
 297      * index {@code ix}.
 298      * Values located at indices at or greater than {@code ix} are
 299      * shifted down towards the end of the list (and their indices incremented
 300      * by one).
 301      * If the attribute values are unordered and already have {@code attrVal},
 302      * {@code IllegalStateException} is thrown.
 303      *
 304      * @param ix The index in the ordered list of attribute values to add the new value.
 305      * {@code 0 <= ix <= size()}.
 306      * @param attrVal The possibly null attribute value to add; if null, null is
 307      * the value added.
 308      * @exception IndexOutOfBoundsException If {@code ix} is outside the specified range.
 309      * @exception IllegalStateException If the attribute values are unordered and
 310      * {@code attrVal} is one of those values.
 311      */
 312     void add(int ix, Object attrVal);
 313 
 314 
 315     /**
 316      * Sets an attribute value in the ordered list of attribute values.
 317      * This method sets the value at the {@code ix} index of the list of
 318      * attribute values to be {@code attrVal}. The old value is removed.
 319      * If the attribute values are unordered,
 320      * this method sets the value that happens to be at that index
 321      * to {@code attrVal}, unless {@code attrVal} is already one of the values.
 322      * In that case, {@code IllegalStateException} is thrown.
 323      *
 324      * @param ix The index of the value in the ordered list of attribute values.
 325      * {@code 0 <= ix < size()}.
 326      * @param attrVal The possibly null attribute value to use.
 327      * If null, 'null' replaces the old value.
 328      * @return The possibly null attribute value at index ix that was replaced.
 329      *   Null if the attribute value was null.
 330      * @exception IndexOutOfBoundsException If {@code ix} is outside the specified range.
 331      * @exception IllegalStateException If {@code attrVal} already exists and the
 332      *    attribute values are unordered.
 333      */
 334     Object set(int ix, Object attrVal);
 335 
 336     /**
 337      * Use serialVersionUID from JNDI 1.1.1 for interoperability.
 338      */
 339     @SuppressWarnings("serial") // serialVersionUID in an interface is ineffectual
 340     static final long serialVersionUID = 8707690322213556804L;
 341 }