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