JNDI 1.1.1

javax.naming.directory
Class BasicAttribute

java.lang.Object
  |
  +--javax.naming.directory.BasicAttribute

public class BasicAttribute
extends java.lang.Object
implements Attribute

This class provides a basic implementation of the Attribute interface.

This implementation does not support schema. The schema methods, getAttributeDefinition() and getAttributeSyntaxDefinition(), simply throw OperationNotSupportedException. Subclasses of BasicAttribute should override these methods if they support schemas.

The BasicAttribute class by default uses Object.equals() to determine equality of attribute values when testing for equality or when searching for values, except when the value is an array. For an array, each element of the array is checked using Object.equals(). Subclasses of BasicAttribute can make use of schema information when doing similar equality checks by overriding methods in which such use of schema is meaningful. Similarly, the BasicAttribute class by default returns the values passed to its constructor and/or manipulated using the add/remove methods. Subclasses of BasicAttribute can override get() and getAll() to get the values dynamically from the directory (or implement the Attribute interface directly instead of subclassing BasicAttribute).

Note that updates to BasicAttribute (such as adding or removing a value) does not affect the corresponding representation of the attribute in the directory. Updates to the directory can only be effected using operations in the DirContext interface.

A BasicAttribute instance is not synchronized against concurrent multithreaded access. Multiple threads trying to access and modify a BasicAttribute should lock the object.

See Also:
Serialized Form

Field Summary
protected  java.lang.String attrID
          Holds the attribute's id.
protected  java.util.Vector values
          Holds the attribute's values.
 
Constructor Summary
BasicAttribute(java.lang.String id)
          Constructs a new instance of an attribute with no value.
BasicAttribute(java.lang.String id, java.lang.Object value)
          Constructs a new instance of an attribute with a single value.
 
Method Summary
 boolean add(java.lang.Object attrVal)
          Adds a new value to this attribute.
 void clear()
          Removes all values from this attribute.
 java.lang.Object clone()
          Makes a copy of this attribute.
 boolean contains(java.lang.Object attrVal)
          Determines whether a value is in this attribute.
 boolean equals(java.lang.Object obj)
          Determines whether obj is equal to this attribute.
 java.lang.Object get()
          Retrieves one of this attribute's values.
 NamingEnumeration getAll()
          Retrieves an enumeration of this attribute's values.
 DirContext getAttributeDefinition()
          Retrieves this attribute's schema definition.
 DirContext getAttributeSyntaxDefinition()
          Retrieves the syntax definition associated with this attribute.
 java.lang.String getID()
          Retrieves the id of this attribute.
 int hashCode()
          Calculates the hash code of this attribute.
 boolean remove(java.lang.Object attrval)
          Removes a specified value from this attribute.
 int size()
          Retrieves the number of values in this attribute.
 java.lang.String toString()
          Generates the string representation of this attribute.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

attrID

protected java.lang.String attrID
Holds the attribute's id. It is initialized by the public constructor and cannot be null unless methods in Attribute that use attrID have been overridden.

values

protected transient java.util.Vector values
Holds the attribute's values. Initialized by public constructors. Cannot be null unless methods in Attribute that use values have been overridden.
Constructor Detail

BasicAttribute

public BasicAttribute(java.lang.String id)
Constructs a new instance of an attribute with no value.
Parameters:
id - The attribute's id. It cannot be null.

BasicAttribute

public BasicAttribute(java.lang.String id,
                      java.lang.Object value)
Constructs a new instance of an attribute with a single value.
Parameters:
id - The attribute's id. It cannot be null.
value - The attribute's value. If null, a null value is added to the attribute.
Method Detail

clone

public java.lang.Object clone()
Makes a copy of this attribute. The copy contains the same attribute values as the original attribute: the attribute values are not themselves cloned. Changes to the copy does not affect the original and vice versa.
Specified by:
clone in interface Attribute
Returns:
A non-null copy of this attribute.
Overrides:
clone in class java.lang.Object

equals

public boolean equals(java.lang.Object obj)
Determines whether obj is equal to this attribute. Two attributes are equal if their attribute-ids, syntaxes and values are equal. The order that the values were added are irrelevant. If obj is null or not an Attribute, false is returned.

By default Object.equals() is used when comparing the attribute id and its values except when a value is an array. For an array, each element of the array is checked using Object.equals(). A subclass may override this to make use of schema syntax information and matching rules, which define what it means for two attributes to be equal. How and whether a subclass makes use of the schema information is determined by the subclass. If a subclass overrides equals(), it should also override hashCode() such that two attributes that are equal have the same hash code.

Parameters:
obj - The possibly null object to check.
Returns:
true if obj is equal to this attribute; false otherwise.
Overrides:
equals in class java.lang.Object
See Also:
hashCode(), contains(java.lang.Object)

hashCode

public int hashCode()
Calculates the hash code of this attribute.

The hash code is computed by adding the hash code of the attribute's id and that of all of its values except for values that are arrays. For an array, the hash code of each element of the array is summed. If a subclass overrides hashCode(), it should override equals() as well so that two attributes that are equal have the same hash code.

Returns:
an int representing the hash code of this attribute.
Overrides:
hashCode in class java.lang.Object
See Also:
equals(java.lang.Object)

toString

public java.lang.String toString()
Generates the string representation of this attribute. The string consists of the attribute's id and its values. This string is meant for debuggin and not meant to be interpreted programmatically.
Returns:
The non-null string representation of this attribute.
Overrides:
toString in class java.lang.Object

getAll

public NamingEnumeration getAll()
                         throws NamingException
Retrieves an enumeration of this attribute's values. The behaviour of this enumeration is unspecified if the this attribute's values are added, changed, or removed.

By default, the values returned are those passed to the constructor and/or manipulated using the add/replace/remove methods. A subclass may override this to retrieve the values dynamically from the directory.

Specified by:
getAll in interface Attribute
Returns:
A non-null enumeration of this attribute's values. Each element of the enumeration an Object. The object's class is the class of the attribute value. If the attribute has zero values, an empty enumeration is returned.
Throws:
NamingException - If a naming exception was encountered while retrieving the values.

get

public java.lang.Object get()
                     throws NamingException
Retrieves one of this attribute's values. If the attribute has more than one value, any of the values are returned.

By default, the value returned is one of those passed to the constructor and/or manipulated using the add/replace/remove methods. A subclass may override this to retrieve the value dynamically from the directory.

Specified by:
get in interface Attribute
Returns:
A possibly null object representing one of the attribute's value.
Throws:
NamingException - If a naming exception was encountered while retrieving the value.
java.util.NoSuchElementException - If this attribute has no values.

size

public int size()
Retrieves the number of values in this attribute.
Specified by:
size in interface Attribute
Returns:
The nonnegative number of values in this attribute.

getID

public java.lang.String getID()
Retrieves the id of this attribute.
Specified by:
getID in interface Attribute
Returns:
The id of this attribute. It cannot be null.

contains

public boolean contains(java.lang.Object attrVal)
Determines whether a value is in this attribute. By default, Object.equals() is used when comparing attrVal with this attribute's values except when attrVal is an array. For an array, each element of the array is checked using Object.equals(). A subclass may use schema information to determine equality.
Specified by:
contains in interface Attribute
Parameters:
attrVal - The possibly null value to check.
Returns:
true if attrVal is one of this attribute's values; false otherwise.
See Also:
equals(java.lang.Object)

add

public boolean add(java.lang.Object attrVal)
Adds a new value to this attribute. If attrVal is already in the attribute, this method does nothing.

By default, Object.equals() is used when comparing attrVal with this attribute's values except when attrVal is an array. For an array, each element of the array is checked using Object.equals(). A subclass may use schema information to determine equality.

Specified by:
add in interface Attribute
Parameters:
attrVal - The new possibly null value to add.
Returns:
true If this attribute did not already have attrVal.

remove

public boolean remove(java.lang.Object attrval)
Removes a specified value from this attribute. If attrval is not in this attribute, just ignore.

By default, Object.equals() is used when comparing attrVal with this attribute's values except when attrVal is an array. For an array, each element of the array is checked using Object.equals(). A subclass may use schema information to determine equality.

Specified by:
remove in interface Attribute
Parameters:
attrVal - The possibly null value to remove from this attribute.
Returns:
true if the value was removed; false otherwise.

clear

public void clear()
Removes all values from this attribute.
Specified by:
clear in interface Attribute

getAttributeSyntaxDefinition

public DirContext getAttributeSyntaxDefinition()
                                        throws NamingException
Retrieves the syntax definition associated with this attribute. An attribute's syntax definition specifies the format of the attribute's value(s). Note that this is different from the attribute value's representation as a Java object. Syntax definition refers to the directory's notion of syntax.

For example, even though a value might be a Java String object, its directory syntax might be "Printable String" or "Telephone Number". Or a value might be a byte array, and its directory syntax is "JPEG" or "Certificate". For example, if this attribute's syntax is "JPEG", this method would return the syntax definition for "JPEG".

The information that you can retrieve from a syntax definition is directory-dependent. This method by default throws OperationNotSupportedException. A subclass should override this method if it supports schema.

Specified by:
getAttributeSyntaxDefinition in interface Attribute
Returns:
This attribute's syntax definition.
Throws:
OperationNotSupportedException - If getting the schema is not supported.
NamingException - If a naming exception occurs while getting the schema.

getAttributeDefinition

public DirContext getAttributeDefinition()
                                  throws NamingException
Retrieves this attribute's schema definition. An attribute's schema definition contains information such as whether the attribute is multivalued or single-valued, the matching rules to use when comparing this attribiute's values. The information that you can retrieve from an attribute definition is directory-dependent. This method by default throws OperationNotSupportedException. A subclass should override this method if it supports schema.
Specified by:
getAttributeDefinition in interface Attribute
Returns:
This attribute's schema definition.
Throws:
OperationNotSupportedException - If getting the schema is not supported.
NamingException - If a naming exception occurs while getting the schema.

JNDI 1.1.1

For more information on JNDI, please see http://java.sun.com/products/jndi