FileDocCategorySizeDatePackage
BasicAttributes.javaAPI DocJava SE 5 API11052Fri Aug 26 14:57:40 BST 2005javax.naming.directory

BasicAttributes

public class BasicAttributes extends Object implements Attributes
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
version
1.12 04/05/05
see
DirContext#getAttributes
see
DirContext#modifyAttributes
see
DirContext#bind
see
DirContext#rebind
see
DirContext#createSubcontext
see
DirContext#search
since
1.3

Fields Summary
private boolean
ignoreCase
Indicates whether case of attribute ids is ignored.
transient Hashtable
attrs
private static final long
serialVersionUID
Use serialVersionUID from JNDI 1.1.1 for interoperability.
Constructors Summary
public BasicAttributes()
Constructs a new instance of Attributes. The character case of attribute identifiers is significant when subsequently retrieving or adding attributes.


                              
      
    
public BasicAttributes(boolean ignoreCase)
Constructs a new instance of Attributes. If ignoreCase 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.

	this.ignoreCase = ignoreCase;
    
public BasicAttributes(String attrID, Object val)
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.

	this();
	this.put(new BasicAttribute(attrID, val));
    
public BasicAttributes(String attrID, Object val, boolean ignoreCase)
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.

	this(ignoreCase);
	this.put(new BasicAttribute(attrID, val));
    
Methods Summary
public java.lang.Objectclone()

	BasicAttributes attrset;
	try {
	    attrset = (BasicAttributes)super.clone();
	} catch (CloneNotSupportedException e) {
	    attrset = new BasicAttributes(ignoreCase);
	}
	attrset.attrs = (Hashtable)attrs.clone();
	return attrset;
    
public booleanequals(java.lang.Object obj)
Determines whether this BasicAttributes is equal to another Attributes Two Attributes are equal if they are both instances of Attributes, treat the case of attribute IDs the same way, and contain the same attributes. Each Attribute in this BasicAttributes is checked for equality using Object.equals(), which may have be overridden by implementations of Attribute). If a subclass overrides equals(), it should override hashCode() as well so that two Attributes instances that are equal have the same hash code.

param
obj the possibly null object to compare against.
return
true If obj is equal to this BasicAttributes.
see
#hashCode

	if ((obj != null) && (obj instanceof Attributes)) {
	    Attributes target = (Attributes)obj;
	    
	    // Check case first
	    if (ignoreCase != target.isCaseIgnored()) {
		return false;
	    }

	    if (size() == target.size()) {
		Attribute their, mine;
		try {
		    NamingEnumeration theirs = target.getAll();
		    while (theirs.hasMore()) {
			their = (Attribute)theirs.next();
			mine = get(their.getID());
			if (!their.equals(mine)) {
			    return false;
			}
		    }
		} catch (NamingException e) {
		    return false;
		}
		return true;
	    }
	}
	return false;
    
public javax.naming.directory.Attributeget(java.lang.String attrID)

	Attribute attr = (Attribute) attrs.get(
		ignoreCase ? attrID.toLowerCase() : attrID);
	return (attr);
    
public javax.naming.NamingEnumerationgetAll()

	return new AttrEnumImpl();
    
public javax.naming.NamingEnumerationgetIDs()

	return new IDEnumImpl();
    
public inthashCode()
Calculates the hash code of this BasicAttributes.

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

	int hash = (ignoreCase ? 1 : 0);
	try {
	    NamingEnumeration all = getAll();
	    while (all.hasMore()) {
		hash += all.next().hashCode();
	    }
	} catch (NamingException e) {}
	return hash;
    
public booleanisCaseIgnored()

	return ignoreCase;
    
public javax.naming.directory.Attributeput(java.lang.String attrID, java.lang.Object val)

	return (Attribute)this.put(new BasicAttribute(attrID, val));
    
public javax.naming.directory.Attributeput(javax.naming.directory.Attribute attr)

	String id = attr.getID();
	if (ignoreCase) {
	    id = id.toLowerCase();
	}
	return (Attribute)attrs.put(id, attr);
    
private voidreadObject(java.io.ObjectInputStream s)
Overridden to avoid exposing implementation details.

        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());
	}
    
public javax.naming.directory.Attributeremove(java.lang.String attrID)

	String id = (ignoreCase ? attrID.toLowerCase() : attrID);
	return (Attribute)attrs.remove(id);
    
public intsize()

	return attrs.size();
    
public java.lang.StringtoString()
Generates the string representation of this attribute set. The string consists of each attribute identifier and the contents of each attribute. The contents of this string is useful for debugging and is not meant to be interpreted programmatically.

return
A non-null string listing the contents of this attribute set.

	if (attrs.size() == 0) {
	    return("No attributes");
	} else {
	    return attrs.toString();
	}
    
private voidwriteObject(java.io.ObjectOutputStream s)
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.

	s.defaultWriteObject();	// write out the ignoreCase flag
	s.writeInt(attrs.size());
	Enumeration attrEnum = attrs.elements();
	while (attrEnum.hasMoreElements()) {
	    s.writeObject(attrEnum.nextElement());
	}