< prev index next >

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

Print this page




 190         return attrs.remove(id);
 191     }
 192 
 193     /**
 194      * Generates the string representation of this attribute set.
 195      * The string consists of each attribute identifier and the contents
 196      * of each attribute. The contents of this string is useful
 197      * for debugging and is not meant to be interpreted programmatically.
 198      *
 199      * @return A non-null string listing the contents of this attribute set.
 200      */
 201     public String toString() {
 202         if (attrs.size() == 0) {
 203             return("No attributes");
 204         } else {
 205             return attrs.toString();
 206         }
 207     }
 208 
 209     /**
 210      * Determines whether this <tt>BasicAttributes</tt> is equal to another
 211      * <tt>Attributes</tt>
 212      * Two <tt>Attributes</tt> are equal if they are both instances of
 213      * <tt>Attributes</tt>,
 214      * treat the case of attribute IDs the same way, and contain the
 215      * same attributes. Each <tt>Attribute</tt> in this <tt>BasicAttributes</tt>
 216      * is checked for equality using <tt>Object.equals()</tt>, which may have
 217      * be overridden by implementations of <tt>Attribute</tt>).
 218      * If a subclass overrides <tt>equals()</tt>,
 219      * it should override <tt>hashCode()</tt>
 220      * as well so that two <tt>Attributes</tt> instances that are equal
 221      * have the same hash code.
 222      * @param obj the possibly null object to compare against.
 223      *
 224      * @return true If obj is equal to this BasicAttributes.
 225      * @see #hashCode
 226      */
 227     public boolean equals(Object obj) {
 228         if ((obj != null) && (obj instanceof Attributes)) {
 229             Attributes target = (Attributes)obj;
 230 
 231             // Check case first
 232             if (ignoreCase != target.isCaseIgnored()) {
 233                 return false;
 234             }
 235 
 236             if (size() == target.size()) {
 237                 Attribute their, mine;
 238                 try {
 239                     NamingEnumeration<?> theirs = target.getAll();
 240                     while (theirs.hasMore()) {


 242                         mine = get(their.getID());
 243                         if (!their.equals(mine)) {
 244                             return false;
 245                         }
 246                     }
 247                 } catch (NamingException e) {
 248                     return false;
 249                 }
 250                 return true;
 251             }
 252         }
 253         return false;
 254     }
 255 
 256     /**
 257      * Calculates the hash code of this BasicAttributes.
 258      *<p>
 259      * The hash code is computed by adding the hash code of
 260      * the attributes of this object. If this BasicAttributes
 261      * ignores case of its attribute IDs, one is added to the hash code.
 262      * If a subclass overrides <tt>hashCode()</tt>,
 263      * it should override <tt>equals()</tt>
 264      * as well so that two <tt>Attributes</tt> instances that are equal
 265      * have the same hash code.
 266      *
 267      * @return an int representing the hash code of this BasicAttributes instance.
 268      * @see #equals
 269      */
 270     public int hashCode() {
 271         int hash = (ignoreCase ? 1 : 0);
 272         try {
 273             NamingEnumeration<?> all = getAll();
 274             while (all.hasMore()) {
 275                 hash += all.next().hashCode();
 276             }
 277         } catch (NamingException e) {}
 278         return hash;
 279     }
 280 
 281     /**
 282      * Overridden to avoid exposing implementation details.
 283      * @serialData Default field (ignoreCase flag -- a boolean), followed by
 284      * the number of attributes in the set




 190         return attrs.remove(id);
 191     }
 192 
 193     /**
 194      * Generates the string representation of this attribute set.
 195      * The string consists of each attribute identifier and the contents
 196      * of each attribute. The contents of this string is useful
 197      * for debugging and is not meant to be interpreted programmatically.
 198      *
 199      * @return A non-null string listing the contents of this attribute set.
 200      */
 201     public String toString() {
 202         if (attrs.size() == 0) {
 203             return("No attributes");
 204         } else {
 205             return attrs.toString();
 206         }
 207     }
 208 
 209     /**
 210      * Determines whether this {@code BasicAttributes} is equal to another
 211      * {@code Attributes}
 212      * Two {@code Attributes} are equal if they are both instances of
 213      * {@code Attributes},
 214      * treat the case of attribute IDs the same way, and contain the
 215      * same attributes. Each {@code Attribute} in this {@code BasicAttributes}
 216      * is checked for equality using {@code Object.equals()}, which may have
 217      * be overridden by implementations of {@code Attribute}).
 218      * If a subclass overrides {@code equals()},
 219      * it should override {@code hashCode()}
 220      * as well so that two {@code Attributes} instances that are equal
 221      * have the same hash code.
 222      * @param obj the possibly null object to compare against.
 223      *
 224      * @return true If obj is equal to this BasicAttributes.
 225      * @see #hashCode
 226      */
 227     public boolean equals(Object obj) {
 228         if ((obj != null) && (obj instanceof Attributes)) {
 229             Attributes target = (Attributes)obj;
 230 
 231             // Check case first
 232             if (ignoreCase != target.isCaseIgnored()) {
 233                 return false;
 234             }
 235 
 236             if (size() == target.size()) {
 237                 Attribute their, mine;
 238                 try {
 239                     NamingEnumeration<?> theirs = target.getAll();
 240                     while (theirs.hasMore()) {


 242                         mine = get(their.getID());
 243                         if (!their.equals(mine)) {
 244                             return false;
 245                         }
 246                     }
 247                 } catch (NamingException e) {
 248                     return false;
 249                 }
 250                 return true;
 251             }
 252         }
 253         return false;
 254     }
 255 
 256     /**
 257      * Calculates the hash code of this BasicAttributes.
 258      *<p>
 259      * The hash code is computed by adding the hash code of
 260      * the attributes of this object. If this BasicAttributes
 261      * ignores case of its attribute IDs, one is added to the hash code.
 262      * If a subclass overrides {@code hashCode()},
 263      * it should override {@code equals()}
 264      * as well so that two {@code Attributes} instances that are equal
 265      * have the same hash code.
 266      *
 267      * @return an int representing the hash code of this BasicAttributes instance.
 268      * @see #equals
 269      */
 270     public int hashCode() {
 271         int hash = (ignoreCase ? 1 : 0);
 272         try {
 273             NamingEnumeration<?> all = getAll();
 274             while (all.hasMore()) {
 275                 hash += all.next().hashCode();
 276             }
 277         } catch (NamingException e) {}
 278         return hash;
 279     }
 280 
 281     /**
 282      * Overridden to avoid exposing implementation details.
 283      * @serialData Default field (ignoreCase flag -- a boolean), followed by
 284      * the number of attributes in the set


< prev index next >