src/share/classes/javax/naming/directory/BasicAttribute.java

Print this page




  74      * Holds the attribute's id. It is initialized by the public constructor and
  75      * cannot be null unless methods in BasicAttribute that use attrID
  76      * have been overridden.
  77      * @serial
  78      */
  79     protected String attrID;
  80 
  81     /**
  82      * Holds the attribute's values. Initialized by public constructors.
  83      * Cannot be null unless methods in BasicAttribute that use
  84      * values have been overridden.
  85      */
  86     protected transient Vector<Object> values;
  87 
  88     /**
  89      * A flag for recording whether this attribute's values are ordered.
  90      * @serial
  91      */
  92     protected boolean ordered = false;
  93 

  94     public Object clone() {
  95         BasicAttribute attr;
  96         try {
  97             attr = (BasicAttribute)super.clone();
  98         } catch (CloneNotSupportedException e) {
  99             attr = new BasicAttribute(attrID, ordered);
 100         }
 101         attr.values = (Vector)values.clone();
 102         return attr;
 103     }
 104 
 105     /**
 106       * Determines whether obj is equal to this attribute.
 107       * Two attributes are equal if their attribute-ids, syntaxes
 108       * and values are equal.
 109       * If the attribute values are unordered, the order that the values were added
 110       * are irrelevant. If the attribute values are ordered, then the
 111       * order the values must match.
 112       * If obj is null or not an Attribute, false is returned.
 113       *<p>
 114       * By default <tt>Object.equals()</tt> is used when comparing the attribute
 115       * id and its values except when a value is an array. For an array,
 116       * each element of the array is checked using <tt>Object.equals()</tt>.
 117       * A subclass may override this to make
 118       * use of schema syntax information and matching rules,
 119       * which define what it means for two attributes to be equal.
 120       * How and whether a subclass makes
 121       * use of the schema information is determined by the subclass.


 132         if ((obj != null) && (obj instanceof Attribute)) {
 133             Attribute target = (Attribute)obj;
 134 
 135             // Check order first
 136             if (isOrdered() != target.isOrdered()) {
 137                 return false;
 138             }
 139             int len;
 140             if (attrID.equals(target.getID()) &&
 141                 (len=size()) == target.size()) {
 142                 try {
 143                     if (isOrdered()) {
 144                         // Go through both list of values
 145                         for (int i = 0; i < len; i++) {
 146                             if (!valueEquals(get(i), target.get(i))) {
 147                                 return false;
 148                             }
 149                         }
 150                     } else {
 151                         // order is not relevant; check for existence
 152                         Enumeration theirs = target.getAll();
 153                         while (theirs.hasMoreElements()) {
 154                             if (find(theirs.nextElement()) < 0)
 155                                 return false;
 156                         }
 157                     }
 158                 } catch (NamingException e) {
 159                     return false;
 160                 }
 161                 return true;
 162             }
 163         }
 164         return false;
 165     }
 166 
 167     /**
 168       * Calculates the hash code of this attribute.
 169       *<p>
 170       * The hash code is computed by adding the hash code of
 171       * the attribute's id and that of all of its values except for
 172       * values that are arrays.


 198                     hash += val.hashCode();
 199                 }
 200             }
 201         }
 202         return hash;
 203     }
 204 
 205     /**
 206       * Generates the string representation of this attribute.
 207       * The string consists of the attribute's id and its values.
 208       * This string is meant for debugging and not meant to be
 209       * interpreted programmatically.
 210       * @return The non-null string representation of this attribute.
 211       */
 212     public String toString() {
 213         StringBuffer answer = new StringBuffer(attrID + ": ");
 214         if (values.size() == 0) {
 215             answer.append("No values");
 216         } else {
 217             boolean start = true;
 218             for (Enumeration e = values.elements(); e.hasMoreElements(); ) {
 219                 if (!start)
 220                     answer.append(", ");
 221                 answer.append(e.nextElement());
 222                 start = false;
 223             }
 224         }
 225         return answer.toString();
 226     }
 227 
 228     /**
 229       * Constructs a new instance of an unordered attribute with no value.
 230       *
 231       * @param id The attribute's id. It cannot be null.
 232       */
 233     public BasicAttribute(String id) {
 234         this(id, false);
 235     }
 236 
 237     /**
 238       * Constructs a new instance of an unordered attribute with a single value.
 239       *
 240       * @param id The attribute's id. It cannot be null.
 241       * @param value The attribute's value. If null, a null
 242       *        value is added to the attribute.
 243       */
 244     public BasicAttribute(String id, Object value) {
 245         this(id, value, false);
 246     }
 247 
 248     /**
 249       * Constructs a new instance of a possibly ordered attribute with no value.
 250       *
 251       * @param id The attribute's id. It cannot be null.
 252       * @param ordered true means the attribute's values will be ordered;
 253       * false otherwise.
 254       */
 255     public BasicAttribute(String id, boolean ordered) {
 256         attrID = id;
 257         values = new Vector();
 258         this.ordered = ordered;
 259     }
 260 
 261     /**
 262       * Constructs a new instance of a possibly ordered attribute with a
 263       * single value.
 264       *
 265       * @param id The attribute's id. It cannot be null.
 266       * @param value The attribute's value. If null, a null
 267       *        value is added to the attribute.
 268       * @param ordered true means the attribute's values will be ordered;
 269       * false otherwise.
 270       */
 271     public BasicAttribute(String id, Object value, boolean ordered) {
 272         this(id, ordered);
 273         values.addElement(value);
 274     }
 275 
 276     /**
 277       * Retrieves an enumeration of this attribute's values.


 310         return attrID;
 311     }
 312 
 313     /**
 314       * Determines whether a value is in this attribute.
 315       *<p>
 316       * By default,
 317       * <tt>Object.equals()</tt> is used when comparing <tt>attrVal</tt>
 318       * with this attribute's values except when <tt>attrVal</tt> is an array.
 319       * For an array, each element of the array is checked using
 320       * <tt>Object.equals()</tt>.
 321       * A subclass may use schema information to determine equality.
 322       */
 323     public boolean contains(Object attrVal) {
 324         return (find(attrVal) >= 0);
 325     }
 326 
 327     // For finding first element that has a null in JDK1.1 Vector.
 328     // In the Java 2 platform, can just replace this with Vector.indexOf(target);
 329     private int find(Object target) {
 330         Class cl;
 331         if (target == null) {
 332             int ct = values.size();
 333             for (int i = 0 ; i < ct ; i++) {
 334                 if (values.elementAt(i) == null)
 335                     return i;
 336             }
 337         } else if ((cl=target.getClass()).isArray()) {
 338             int ct = values.size();
 339             Object it;
 340             for (int i = 0 ; i < ct ; i++) {
 341                 it = values.elementAt(i);
 342                 if (it != null && cl == it.getClass()
 343                     && arrayEquals(target, it))
 344                     return i;
 345             }
 346         } else {
 347             return values.indexOf(target, 0);
 348         }
 349         return -1;  // not found
 350     }


 497      * @serialData Default field (the attribute ID -- a String),
 498      * followed by the number of values (an int), and the
 499      * individual values.
 500      */
 501     private void writeObject(java.io.ObjectOutputStream s)
 502             throws java.io.IOException {
 503         s.defaultWriteObject(); // write out the attrID
 504         s.writeInt(values.size());
 505         for (int i = 0; i < values.size(); i++) {
 506             s.writeObject(values.elementAt(i));
 507         }
 508     }
 509 
 510     /**
 511      * Overridden to avoid exposing implementation details.
 512      */
 513     private void readObject(java.io.ObjectInputStream s)
 514             throws java.io.IOException, ClassNotFoundException {
 515         s.defaultReadObject();  // read in the attrID
 516         int n = s.readInt();    // number of values
 517         values = new Vector(n);
 518         while (--n >= 0) {
 519             values.addElement(s.readObject());
 520         }
 521     }
 522 
 523 
 524     class ValuesEnumImpl implements NamingEnumeration<Object> {
 525     Enumeration list;
 526 
 527     ValuesEnumImpl() {
 528         list = values.elements();
 529     }
 530 
 531     public boolean hasMoreElements() {
 532         return list.hasMoreElements();
 533     }
 534 
 535     public Object nextElement() {
 536         return(list.nextElement());
 537     }
 538 
 539     public Object next() throws NamingException {
 540         return list.nextElement();
 541     }
 542 
 543     public boolean hasMore() throws NamingException {
 544         return list.hasMoreElements();
 545     }


  74      * Holds the attribute's id. It is initialized by the public constructor and
  75      * cannot be null unless methods in BasicAttribute that use attrID
  76      * have been overridden.
  77      * @serial
  78      */
  79     protected String attrID;
  80 
  81     /**
  82      * Holds the attribute's values. Initialized by public constructors.
  83      * Cannot be null unless methods in BasicAttribute that use
  84      * values have been overridden.
  85      */
  86     protected transient Vector<Object> values;
  87 
  88     /**
  89      * A flag for recording whether this attribute's values are ordered.
  90      * @serial
  91      */
  92     protected boolean ordered = false;
  93 
  94     @SuppressWarnings("unchecked")
  95     public Object clone() {
  96         BasicAttribute attr;
  97         try {
  98             attr = (BasicAttribute)super.clone();
  99         } catch (CloneNotSupportedException e) {
 100             attr = new BasicAttribute(attrID, ordered);
 101         }
 102         attr.values = (Vector<Object>)values.clone();
 103         return attr;
 104     }
 105 
 106     /**
 107       * Determines whether obj is equal to this attribute.
 108       * Two attributes are equal if their attribute-ids, syntaxes
 109       * and values are equal.
 110       * If the attribute values are unordered, the order that the values were added
 111       * are irrelevant. If the attribute values are ordered, then the
 112       * order the values must match.
 113       * If obj is null or not an Attribute, false is returned.
 114       *<p>
 115       * By default <tt>Object.equals()</tt> is used when comparing the attribute
 116       * id and its values except when a value is an array. For an array,
 117       * each element of the array is checked using <tt>Object.equals()</tt>.
 118       * A subclass may override this to make
 119       * use of schema syntax information and matching rules,
 120       * which define what it means for two attributes to be equal.
 121       * How and whether a subclass makes
 122       * use of the schema information is determined by the subclass.


 133         if ((obj != null) && (obj instanceof Attribute)) {
 134             Attribute target = (Attribute)obj;
 135 
 136             // Check order first
 137             if (isOrdered() != target.isOrdered()) {
 138                 return false;
 139             }
 140             int len;
 141             if (attrID.equals(target.getID()) &&
 142                 (len=size()) == target.size()) {
 143                 try {
 144                     if (isOrdered()) {
 145                         // Go through both list of values
 146                         for (int i = 0; i < len; i++) {
 147                             if (!valueEquals(get(i), target.get(i))) {
 148                                 return false;
 149                             }
 150                         }
 151                     } else {
 152                         // order is not relevant; check for existence
 153                         Enumeration<?> theirs = target.getAll();
 154                         while (theirs.hasMoreElements()) {
 155                             if (find(theirs.nextElement()) < 0)
 156                                 return false;
 157                         }
 158                     }
 159                 } catch (NamingException e) {
 160                     return false;
 161                 }
 162                 return true;
 163             }
 164         }
 165         return false;
 166     }
 167 
 168     /**
 169       * Calculates the hash code of this attribute.
 170       *<p>
 171       * The hash code is computed by adding the hash code of
 172       * the attribute's id and that of all of its values except for
 173       * values that are arrays.


 199                     hash += val.hashCode();
 200                 }
 201             }
 202         }
 203         return hash;
 204     }
 205 
 206     /**
 207       * Generates the string representation of this attribute.
 208       * The string consists of the attribute's id and its values.
 209       * This string is meant for debugging and not meant to be
 210       * interpreted programmatically.
 211       * @return The non-null string representation of this attribute.
 212       */
 213     public String toString() {
 214         StringBuffer answer = new StringBuffer(attrID + ": ");
 215         if (values.size() == 0) {
 216             answer.append("No values");
 217         } else {
 218             boolean start = true;
 219             for (Enumeration<Object> e = values.elements(); e.hasMoreElements(); ) {
 220                 if (!start)
 221                     answer.append(", ");
 222                 answer.append(e.nextElement());
 223                 start = false;
 224             }
 225         }
 226         return answer.toString();
 227     }
 228 
 229     /**
 230       * Constructs a new instance of an unordered attribute with no value.
 231       *
 232       * @param id The attribute's id. It cannot be null.
 233       */
 234     public BasicAttribute(String id) {
 235         this(id, false);
 236     }
 237 
 238     /**
 239       * Constructs a new instance of an unordered attribute with a single value.
 240       *
 241       * @param id The attribute's id. It cannot be null.
 242       * @param value The attribute's value. If null, a null
 243       *        value is added to the attribute.
 244       */
 245     public BasicAttribute(String id, Object value) {
 246         this(id, value, false);
 247     }
 248 
 249     /**
 250       * Constructs a new instance of a possibly ordered attribute with no value.
 251       *
 252       * @param id The attribute's id. It cannot be null.
 253       * @param ordered true means the attribute's values will be ordered;
 254       * false otherwise.
 255       */
 256     public BasicAttribute(String id, boolean ordered) {
 257         attrID = id;
 258         values = new Vector<>();
 259         this.ordered = ordered;
 260     }
 261 
 262     /**
 263       * Constructs a new instance of a possibly ordered attribute with a
 264       * single value.
 265       *
 266       * @param id The attribute's id. It cannot be null.
 267       * @param value The attribute's value. If null, a null
 268       *        value is added to the attribute.
 269       * @param ordered true means the attribute's values will be ordered;
 270       * false otherwise.
 271       */
 272     public BasicAttribute(String id, Object value, boolean ordered) {
 273         this(id, ordered);
 274         values.addElement(value);
 275     }
 276 
 277     /**
 278       * Retrieves an enumeration of this attribute's values.


 311         return attrID;
 312     }
 313 
 314     /**
 315       * Determines whether a value is in this attribute.
 316       *<p>
 317       * By default,
 318       * <tt>Object.equals()</tt> is used when comparing <tt>attrVal</tt>
 319       * with this attribute's values except when <tt>attrVal</tt> is an array.
 320       * For an array, each element of the array is checked using
 321       * <tt>Object.equals()</tt>.
 322       * A subclass may use schema information to determine equality.
 323       */
 324     public boolean contains(Object attrVal) {
 325         return (find(attrVal) >= 0);
 326     }
 327 
 328     // For finding first element that has a null in JDK1.1 Vector.
 329     // In the Java 2 platform, can just replace this with Vector.indexOf(target);
 330     private int find(Object target) {
 331         Class<?> cl;
 332         if (target == null) {
 333             int ct = values.size();
 334             for (int i = 0 ; i < ct ; i++) {
 335                 if (values.elementAt(i) == null)
 336                     return i;
 337             }
 338         } else if ((cl=target.getClass()).isArray()) {
 339             int ct = values.size();
 340             Object it;
 341             for (int i = 0 ; i < ct ; i++) {
 342                 it = values.elementAt(i);
 343                 if (it != null && cl == it.getClass()
 344                     && arrayEquals(target, it))
 345                     return i;
 346             }
 347         } else {
 348             return values.indexOf(target, 0);
 349         }
 350         return -1;  // not found
 351     }


 498      * @serialData Default field (the attribute ID -- a String),
 499      * followed by the number of values (an int), and the
 500      * individual values.
 501      */
 502     private void writeObject(java.io.ObjectOutputStream s)
 503             throws java.io.IOException {
 504         s.defaultWriteObject(); // write out the attrID
 505         s.writeInt(values.size());
 506         for (int i = 0; i < values.size(); i++) {
 507             s.writeObject(values.elementAt(i));
 508         }
 509     }
 510 
 511     /**
 512      * Overridden to avoid exposing implementation details.
 513      */
 514     private void readObject(java.io.ObjectInputStream s)
 515             throws java.io.IOException, ClassNotFoundException {
 516         s.defaultReadObject();  // read in the attrID
 517         int n = s.readInt();    // number of values
 518         values = new Vector<>(n);
 519         while (--n >= 0) {
 520             values.addElement(s.readObject());
 521         }
 522     }
 523 
 524 
 525     class ValuesEnumImpl implements NamingEnumeration<Object> {
 526     Enumeration<Object> list;
 527 
 528     ValuesEnumImpl() {
 529         list = values.elements();
 530     }
 531 
 532     public boolean hasMoreElements() {
 533         return list.hasMoreElements();
 534     }
 535 
 536     public Object nextElement() {
 537         return(list.nextElement());
 538     }
 539 
 540     public Object next() throws NamingException {
 541         return list.nextElement();
 542     }
 543 
 544     public boolean hasMore() throws NamingException {
 545         return list.hasMoreElements();
 546     }