/* * Copyright (c) 1999, 2004, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * * * * * * * * * * * * * * * * * * * * */ package javax.naming.directory; import java.util.Hashtable; import java.util.Enumeration; import javax.naming.NamingException; import javax.naming.NamingEnumeration; /** * This class provides a basic implementation * of the Attributes interface. *
* BasicAttributes is either case-sensitive or case-insensitive (case-ignore). * This property is determined at the time the BasicAttributes constructor * is called. * In a case-insensitive BasicAttributes, the case of its attribute identifiers * is ignored when searching for an attribute, or adding attributes. * In a case-sensitive BasicAttributes, the case is significant. *
* When the BasicAttributes class needs to create an Attribute, it * uses BasicAttribute. There is no other dependency on BasicAttribute. *
* Note that updates to BasicAttributes (such as adding or removing an attribute) * does not affect the corresponding representation in the directory. * Updates to the directory can only be effected * using operations in the DirContext interface. *
* A BasicAttributes instance is not synchronized against concurrent
* multithreaded access. Multiple threads trying to access and modify
* a single BasicAttributes instance should lock the object.
*
* @author Rosanna Lee
* @author Scott Seligman
*
* @see DirContext#getAttributes
* @see DirContext#modifyAttributes
* @see DirContext#bind
* @see DirContext#rebind
* @see DirContext#createSubcontext
* @see DirContext#search
* @since 1.3
*/
public class BasicAttributes implements Attributes {
/**
* Indicates whether case of attribute ids is ignored.
* @serial
*/
private boolean ignoreCase = false;
// The 'key' in attrs is stored in the 'right case'.
// If ignoreCase is true, key is aways lowercase.
// If ignoreCase is false, key is stored as supplied by put().
// %%% Not declared "private" due to bug 4064984.
transient Hashtable attrs = new Hashtable(11);
/**
* Constructs a new instance of Attributes.
* The character case of attribute identifiers
* is significant when subsequently retrieving or adding attributes.
*/
public BasicAttributes() {
}
/**
* Constructs a new instance of Attributes.
* If
* The hash code is computed by adding the hash code of
* the attributes of this object. If this BasicAttributes
* ignores case of its attribute IDs, one is added to the hash code.
* If a subclass overrides hashCode(),
* it should override equals()
* as well so that two Attributes instances that are equal
* have the same hash code.
*
* @return an int representing the hash code of this BasicAttributes instance.
* @see #equals
*/
public int hashCode() {
int hash = (ignoreCase ? 1 : 0);
try {
NamingEnumeration all = getAll();
while (all.hasMore()) {
hash += all.next().hashCode();
}
} catch (NamingException e) {}
return hash;
}
/**
* Overridden to avoid exposing implementation details.
* @serialData Default field (ignoreCase flag -- a boolean), followed by
* the number of attributes in the set
* (an int), and then the individual Attribute objects.
*/
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException {
s.defaultWriteObject(); // write out the ignoreCase flag
s.writeInt(attrs.size());
Enumeration attrEnum = attrs.elements();
while (attrEnum.hasMoreElements()) {
s.writeObject(attrEnum.nextElement());
}
}
/**
* Overridden to avoid exposing implementation details.
*/
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
s.defaultReadObject(); // read in the ignoreCase flag
int n = s.readInt(); // number of attributes
attrs = (n >= 1)
? new Hashtable(n * 2)
: new Hashtable(2); // can't have initial size of 0 (grrr...)
while (--n >= 0) {
put((Attribute)s.readObject());
}
}
class AttrEnumImpl implements NamingEnumerationignoreCase
is true, the character case of attribute
* identifiers is ignored; otherwise the case is significant.
* @param ignoreCase true means this attribute set will ignore
* the case of its attribute identifiers
* when retrieving or adding attributes;
* false means case is respected.
*/
public BasicAttributes(boolean ignoreCase) {
this.ignoreCase = ignoreCase;
}
/**
* Constructs a new instance of Attributes with one attribute.
* The attribute specified by attrID and val are added to the newly
* created attribute.
* The character case of attribute identifiers
* is significant when subsequently retrieving or adding attributes.
* @param attrID non-null The id of the attribute to add.
* @param val The value of the attribute to add. If null, a null
* value is added to the attribute.
*/
public BasicAttributes(String attrID, Object val) {
this();
this.put(new BasicAttribute(attrID, val));
}
/**
* Constructs a new instance of Attributes with one attribute.
* The attribute specified by attrID and val are added to the newly
* created attribute.
* If ignoreCase
is true, the character case of attribute
* identifiers is ignored; otherwise the case is significant.
* @param attrID non-null The id of the attribute to add.
* If this attribute set ignores the character
* case of its attribute ids, the case of attrID
* is ignored.
* @param val The value of the attribute to add. If null, a null
* value is added to the attribute.
* @param ignoreCase true means this attribute set will ignore
* the case of its attribute identifiers
* when retrieving or adding attributes;
* false means case is respected.
*/
public BasicAttributes(String attrID, Object val, boolean ignoreCase) {
this(ignoreCase);
this.put(new BasicAttribute(attrID, val));
}
public Object clone() {
BasicAttributes attrset;
try {
attrset = (BasicAttributes)super.clone();
} catch (CloneNotSupportedException e) {
attrset = new BasicAttributes(ignoreCase);
}
attrset.attrs = (Hashtable)attrs.clone();
return attrset;
}
public boolean isCaseIgnored() {
return ignoreCase;
}
public int size() {
return attrs.size();
}
public Attribute get(String attrID) {
Attribute attr = (Attribute) attrs.get(
ignoreCase ? attrID.toLowerCase() : attrID);
return (attr);
}
public NamingEnumeration