org.apache.directory.shared.ldap.entry.client
Class DefaultClientAttribute

java.lang.Object
  extended by org.apache.directory.shared.ldap.entry.client.DefaultClientAttribute
All Implemented Interfaces:
java.io.Externalizable, java.io.Serializable, java.lang.Cloneable, java.lang.Iterable<Value<?>>, EntryAttribute
Direct Known Subclasses:
DefaultServerAttribute

public class DefaultClientAttribute
extends java.lang.Object
implements EntryAttribute

A client side entry attribute. The client is not aware of the schema, so we can't tell if the stored value will be String or Binary. We will default to Binary.

To define the kind of data stored, the client must set the isHR flag.

Version:
$Rev$, $Date$
Author:
Apache Directory Project
See Also:
Serialized Form

Field Summary
protected  AttributeType attributeType
          The associated AttributeType
protected  java.lang.String id
          The normalized ID (will be the OID if we have a AttributeType)
protected  java.lang.Boolean isHR
          Tells if the attribute is Human Readable or not.
protected  java.lang.String upId
          The User provided ID
protected  java.util.Set<Value<?>> values
          The set of contained values
 
Constructor Summary
DefaultClientAttribute()
          Create a new instance of a EntryAttribute, without ID nor value.
DefaultClientAttribute(java.lang.String upId)
          Create a new instance of a EntryAttribute, without value.
DefaultClientAttribute(java.lang.String upId, byte[]... vals)
          Create a new instance of a EntryAttribute, with some byte[] values.
DefaultClientAttribute(java.lang.String upId, java.lang.String... vals)
          Create a new instance of a EntryAttribute.
DefaultClientAttribute(java.lang.String upId, Value<?>... vals)
          If the value does not correspond to the same attributeType, then it's wrapped value is copied into a new ClientValue which uses the specified attributeType.
 
Method Summary
 int add(byte[]... vals)
          Adds some values to this attribute.
 int add(java.lang.String... vals)
          Adds some values to this attribute.
 int add(Value<?>... vals)
          Adds some values to this attribute.
 void clear()
          Remove all the values from this attribute.
 EntryAttribute clone()
           
 boolean contains(byte[]... vals)
           Indicates whether the specified values are some of the attribute's values.
 boolean contains(java.lang.Object... vals)
           
 boolean contains(java.lang.String... vals)
           Indicates whether the specified values are some of the attribute's values.
 boolean contains(Value<?>... vals)
           Indicates whether the specified values are some of the attribute's values.
 boolean equals(java.lang.Object obj)
           
 Value<?> get()
           Get the first value of this attribute.
 Value<?> get(int i)
           Get the nth value of this attribute.
 java.util.Iterator<Value<?>> getAll()
          Returns an iterator over all the attribute's values.
 AttributeType getAttributeType()
          Get the attribute type associated with this ServerAttribute.
 byte[] getBytes()
           Get the byte[] value, if and only if the value is known to be Binary, otherwise a InvalidAttributeValueException will be thrown
 java.lang.String getId()
          Get's the attribute identifier.
 java.lang.String getString()
           Get the String value, if and only if the value is known to be a String, otherwise a InvalidAttributeValueException will be thrown
 java.lang.String getUpId()
          Get's the user provided identifier for this entry.
 int hashCode()
          The hashCode is based on the id, the isHR flag and on the internal values.
 boolean instanceOf(java.lang.String attributeId)
           Check if the current attribute type is of the expected attributeType
 boolean isHR()
           Tells if the attribute is Human Readable.
 boolean isValid()
          Checks to see if this attribute is valid along with the values it contains.
 boolean isValid(SyntaxChecker checker)
          Checks to see if this attribute is valid along with the values it contains.
 java.util.Iterator<Value<?>> iterator()
          An iterator on top of the stored values.
 int put(byte[]... vals)
          Puts some values to this attribute.
 int put(java.util.List<Value<?>> vals)
           Puts a list of values into this attribute.
 int put(java.lang.String... vals)
          Puts some values to this attribute.
 int put(Value<?>... vals)
          Puts some values to this attribute.
 void readExternal(java.io.ObjectInput in)
           
 boolean remove(byte[]... vals)
           Removes all the values that are equal to the given values.
 boolean remove(java.lang.String... vals)
          Removes all the values that are equal to the given values.
 boolean remove(Value<?>... vals)
           Removes all the values that are equal to the given values.
 void setAttributeType(AttributeType attributeType)
           Set the attribute type associated with this ServerAttribute.
 void setHR(boolean isHR)
           Set the attribute to Human Readable or to Binary.
 void setId(java.lang.String id)
          Set the normalized ID.
 void setUpId(java.lang.String upId)
          Set the user provided ID.
 void setUpId(java.lang.String upId, AttributeType attributeType)
           Set the user provided ID.
 int size()
          Retrieves the number of values in this attribute.
 EntryAttribute toClientAttribute()
          Convert the ServerAttribute to a ClientAttribute
 java.lang.String toString()
           
 void writeExternal(java.io.ObjectOutput out)
           
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

attributeType

protected AttributeType attributeType
The associated AttributeType


values

protected java.util.Set<Value<?>> values
The set of contained values


upId

protected java.lang.String upId
The User provided ID


id

protected java.lang.String id
The normalized ID (will be the OID if we have a AttributeType)


isHR

protected java.lang.Boolean isHR
Tells if the attribute is Human Readable or not. When not set, this flag is null.

Constructor Detail

DefaultClientAttribute

public DefaultClientAttribute()
Create a new instance of a EntryAttribute, without ID nor value.


DefaultClientAttribute

public DefaultClientAttribute(java.lang.String upId)
Create a new instance of a EntryAttribute, without value.


DefaultClientAttribute

public DefaultClientAttribute(java.lang.String upId,
                              Value<?>... vals)
If the value does not correspond to the same attributeType, then it's wrapped value is copied into a new ClientValue which uses the specified attributeType. Otherwise, the value is stored, but as a reference. It's not a copy.

Parameters:
upId -
attributeType - the attribute type according to the schema
vals - an initial set of values for this attribute

DefaultClientAttribute

public DefaultClientAttribute(java.lang.String upId,
                              java.lang.String... vals)
Create a new instance of a EntryAttribute.


DefaultClientAttribute

public DefaultClientAttribute(java.lang.String upId,
                              byte[]... vals)
Create a new instance of a EntryAttribute, with some byte[] values.

Method Detail

getBytes

public byte[] getBytes()
                throws LdapInvalidAttributeValueException

Get the byte[] value, if and only if the value is known to be Binary, otherwise a InvalidAttributeValueException will be thrown

Note that this method returns the first value only.

Specified by:
getBytes in interface EntryAttribute
Returns:
The value as a byte[]
Throws:
LdapInvalidAttributeValueException - If the value is a String

getString

public java.lang.String getString()
                           throws LdapInvalidAttributeValueException

Get the String value, if and only if the value is known to be a String, otherwise a InvalidAttributeValueException will be thrown

Note that this method returns the first value only.

Specified by:
getString in interface EntryAttribute
Returns:
The value as a String
Throws:
LdapInvalidAttributeValueException - If the value is a byte[]

getId

public java.lang.String getId()
Get's the attribute identifier. Its value is the same than the user provided ID.

Specified by:
getId in interface EntryAttribute
Returns:
the attribute's identifier

setHR

public void setHR(boolean isHR)

Set the attribute to Human Readable or to Binary.

Specified by:
setHR in interface EntryAttribute
Parameters:
isHR - true for a Human Readable attribute, false for a Binary attribute.

setId

public void setId(java.lang.String id)
Set the normalized ID. The ID will be lowercased, and spaces will be trimmed.

Specified by:
setId in interface EntryAttribute
Parameters:
id - The attribute ID
Throws:
java.lang.IllegalArgumentException - If the ID is empty or null or resolve to an empty value after being trimmed

getUpId

public java.lang.String getUpId()
Get's the user provided identifier for this entry. This is the value that will be used as the identifier for the attribute within the entry. If this is a commonName attribute for example and the user provides "COMMONname" instead when adding the entry then this is the format the user will have that entry returned by the directory server. To do so we store this value as it was given and track it in the attribute using this property.

Specified by:
getUpId in interface EntryAttribute
Returns:
the user provided identifier for this attribute

setUpId

public void setUpId(java.lang.String upId)
Set the user provided ID. It will also set the ID, normalizing the upId (removing spaces before and after, and lowercasing it)

If the Attribute already has an AttributeType, then the upId must be either the AttributeType name, or OID

Specified by:
setUpId in interface EntryAttribute
Parameters:
upId - The attribute ID
Throws:
java.lang.IllegalArgumentException - If the ID is empty or null or resolve to an empty value after being trimmed

setUpId

public void setUpId(java.lang.String upId,
                    AttributeType attributeType)

Set the user provided ID. If we have none, the upId is assigned the attributetype's name. If it does not have any name, we will use the OID.

If we have an upId and an AttributeType, they must be compatible. : - if the upId is an OID, it must be the AttributeType's OID - otherwise, its normalized form must be equals to ones of the attributeType's names.

In any case, the ATtributeType will be changed. The caller is responsible for the present values to be compatoble with the new AttributeType.

Specified by:
setUpId in interface EntryAttribute
Parameters:
upId - The attribute ID
attributeType - The associated attributeType

isHR

public boolean isHR()

Tells if the attribute is Human Readable.

This flag is set by the caller, or implicitly when adding String values into an attribute which is not yet declared as Binary.

Specified by:
isHR in interface EntryAttribute
Returns:

isValid

public boolean isValid()
                throws LdapException
Checks to see if this attribute is valid along with the values it contains.

Specified by:
isValid in interface EntryAttribute
Returns:
true if the attribute and it's values are valid, false otherwise
Throws:
LdapException - if there is a failure to check syntaxes of values

isValid

public boolean isValid(SyntaxChecker checker)
                throws LdapException
Checks to see if this attribute is valid along with the values it contains.

Specified by:
isValid in interface EntryAttribute
Parameters:
checker - The syntax checker
Returns:
true if the attribute and it's values are valid, false otherwise
Throws:
LdapException - if there is a failure to check syntaxes of values

add

public int add(Value<?>... vals)
Adds some values to this attribute. If the new values are already present in the attribute values, the method has no effect.

The new values are added at the end of list of values.

This method returns the number of values that were added.

If the value's type is different from the attribute's type, a conversion is done. For instance, if we try to set some StringValue into a Binary attribute, we just store the UTF-8 byte array encoding for this StringValue.

If we try to store some BinaryValue in a HR attribute, we try to convert those BinaryValue assuming they represent an UTF-8 encoded String. Of course, if it's not the case, the stored value will be incorrect.

It's the responsibility of the caller to check if the stored values are consistent with the attribute's type.

The caller can set the HR flag in order to enforce a type for the current attribute, otherwise this type will be set while adding the first value, using the value's type to set the flag.

Note : If the entry contains no value, and the unique added value is a null length value, then this value will be considered as a binary value.

Specified by:
add in interface EntryAttribute
Parameters:
val - some new values to be added which may be null
Returns:
the number of added values, or 0 if none has been added

add

public int add(java.lang.String... vals)
Description copied from interface: EntryAttribute
Adds some values to this attribute. If the new values are already present in the attribute values, the method has no effect.

The new values are added at the end of list of values.

This method returns the number of values that were added.

If the value's type is different from the attribute's type, a conversion is done. For instance, if we try to set some String into a Binary attribute, we just store the UTF-8 byte array encoding for this String.

If we try to store some byte[] in a HR attribute, we try to convert those byte[] assuming they represent an UTF-8 encoded String. Of course, if it's not the case, the stored value will be incorrect.

It's the responsibility of the caller to check if the stored values are consistent with the attribute's type.

The caller can set the HR flag in order to enforce a type for the current attribute, otherwise this type will be set while adding the first value, using the value's type to set the flag.

Specified by:
add in interface EntryAttribute
Parameters:
vals - some new values to be added which may be null
Returns:
the number of added values, or 0 if none has been added
See Also:
EntryAttribute.add(String...)

add

public int add(byte[]... vals)
Adds some values to this attribute. If the new values are already present in the attribute values, the method has no effect.

The new values are added at the end of list of values.

This method returns the number of values that were added.

If the value's type is different from the attribute's type, a conversion is done. For instance, if we try to set some String into a Binary attribute, we just store the UTF-8 byte array encoding for this String. If we try to store some byte[] in a HR attribute, we try to convert those byte[] assuming they represent an UTF-8 encoded String. Of course, if it's not the case, the stored value will be incorrect.
It's the responsibility of the caller to check if the stored values are consistent with the attribute's type.
The caller can set the HR flag in order to enforce a type for the current attribute, otherwise this type will be set while adding the first value, using the value's type to set the flag.

Specified by:
add in interface EntryAttribute
Parameters:
val - some new values to be added which may be null
Returns:
the number of added values, or 0 if none has been added

clear

public void clear()
Remove all the values from this attribute.

Specified by:
clear in interface EntryAttribute

contains

public boolean contains(Value<?>... vals)

Indicates whether the specified values are some of the attribute's values.

If the Attribute is HR, the binary values will be converted to String before being checked.

Specified by:
contains in interface EntryAttribute
Parameters:
vals - the values
Returns:
true if this attribute contains all the values, otherwise false

contains

public boolean contains(java.lang.String... vals)

Indicates whether the specified values are some of the attribute's values.

If the Attribute is not HR, the values will be converted to byte[]

Specified by:
contains in interface EntryAttribute
Parameters:
vals - the values
Returns:
true if this attribute contains all the values, otherwise false

contains

public boolean contains(byte[]... vals)

Indicates whether the specified values are some of the attribute's values.

If the Attribute is HR, the values will be converted to String

Specified by:
contains in interface EntryAttribute
Parameters:
vals - the values
Returns:
true if this attribute contains all the values, otherwise false

contains

public boolean contains(java.lang.Object... vals)
See Also:
EntryAttribute#contains(Object...)

get

public Value<?> get()

Get the first value of this attribute. If there is none, null is returned.

Note : even if we are storing values into a Set, one can assume the values are ordered following the insertion order.

This method is meant to be used if the attribute hold only one value.

Specified by:
get in interface EntryAttribute
Returns:
The first value for this attribute.

get

public Value<?> get(int i)

Get the nth value of this attribute. If there is none, null is returned.

Note : even if we are storing values into a Set, one can assume the values are ordered following the insertion order.

Specified by:
get in interface EntryAttribute
Parameters:
i - the index of the value to get
Returns:
The nth value for this attribute.

getAll

public java.util.Iterator<Value<?>> getAll()
Returns an iterator over all the attribute's values.

The effect on the returned enumeration of adding or removing values of the attribute is not specified.

This method will throw any LdapException that occurs.

Specified by:
getAll in interface EntryAttribute
Returns:
an enumeration of all values of the attribute

size

public int size()
Retrieves the number of values in this attribute.

Specified by:
size in interface EntryAttribute
Returns:
the number of values in this attribute, including any values wrapping a null value if there is one

remove

public boolean remove(Value<?>... vals)

Removes all the values that are equal to the given values.

Returns true if all the values are removed.

If the attribute type is HR and some value which are not String, we will convert the values first (same thing for a non-HR attribute).

Specified by:
remove in interface EntryAttribute
Parameters:
vals - the values to be removed
Returns:
true if all the values are removed, otherwise false

remove

public boolean remove(byte[]... vals)

Removes all the values that are equal to the given values.

Returns true if all the values are removed.

If the attribute type is HR, then the values will be first converted to String

Specified by:
remove in interface EntryAttribute
Parameters:
vals - the values to be removed
Returns:
true if all the values are removed, otherwise false

remove

public boolean remove(java.lang.String... vals)
Removes all the values that are equal to the given values.

Returns true if all the values are removed.

If the attribute type is not HR, then the values will be first converted to byte[]

Specified by:
remove in interface EntryAttribute
Parameters:
vals - the values to be removed
Returns:
true if all the values are removed, otherwise false

iterator

public java.util.Iterator<Value<?>> iterator()
An iterator on top of the stored values.

Specified by:
iterator in interface java.lang.Iterable<Value<?>>
Returns:
an iterator over the stored values.

put

public int put(java.lang.String... vals)
Puts some values to this attribute.

The new values will replace the previous values.

This method returns the number of values that were put.

Specified by:
put in interface EntryAttribute
Parameters:
val - some values to be put which may be null
Returns:
the number of added values, or 0 if none has been added

put

public int put(byte[]... vals)
Puts some values to this attribute.

The new values will replace the previous values.

This method returns the number of values that were put.

Specified by:
put in interface EntryAttribute
Parameters:
val - some values to be put which may be null
Returns:
the number of added values, or 0 if none has been added

put

public int put(Value<?>... vals)
Puts some values to this attribute.

The new values are replace the previous values.

This method returns the number of values that were put.

Specified by:
put in interface EntryAttribute
Parameters:
val - some values to be put which may be null
Returns:
the number of added values, or 0 if none has been added

put

public int put(java.util.List<Value<?>> vals)

Puts a list of values into this attribute.

The new values will replace the previous values.

This method returns the number of values that were put.

Specified by:
put in interface EntryAttribute
Parameters:
vals - the values to be put
Returns:
the number of added values, or 0 if none has been added

getAttributeType

public AttributeType getAttributeType()
Get the attribute type associated with this ServerAttribute.

Specified by:
getAttributeType in interface EntryAttribute
Returns:
the attributeType associated with this entry attribute

setAttributeType

public void setAttributeType(AttributeType attributeType)

Set the attribute type associated with this ServerAttribute.

The current attributeType will be replaced. It is the responsibility of the caller to insure that the existing values are compatible with the new AttributeType

Specified by:
setAttributeType in interface EntryAttribute
Parameters:
attributeType - the attributeType associated with this entry attribute

instanceOf

public boolean instanceOf(java.lang.String attributeId)
                   throws LdapInvalidAttributeValueException

Check if the current attribute type is of the expected attributeType

This method won't tell if the current attribute is a descendant of the attributeType. For instance, the "CN" serverAttribute will return false if we ask if it's an instance of "Name".

Specified by:
instanceOf in interface EntryAttribute
Parameters:
attributeId - The AttributeType ID to check
Returns:
True if the current attribute is of the expected attributeType
Throws:
LdapInvalidAttributeValueException - If there is no AttributeType

toClientAttribute

public EntryAttribute toClientAttribute()
Convert the ServerAttribute to a ClientAttribute

Specified by:
toClientAttribute in interface EntryAttribute
Returns:
An instance of ClientAttribute

hashCode

public int hashCode()
The hashCode is based on the id, the isHR flag and on the internal values.

Overrides:
hashCode in class java.lang.Object
Returns:
the instance's hashcode
See Also:
Object.hashCode()

equals

public boolean equals(java.lang.Object obj)
Overrides:
equals in class java.lang.Object
See Also:
Object.equals(Object)

clone

public EntryAttribute clone()
Specified by:
clone in interface EntryAttribute
Overrides:
clone in class java.lang.Object
Returns:
A clone of the current object
See Also:
Cloneable#clone()

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object
See Also:
Object.toString()

writeExternal

public void writeExternal(java.io.ObjectOutput out)
                   throws java.io.IOException
Specified by:
writeExternal in interface java.io.Externalizable
Throws:
java.io.IOException
See Also:

This is the place where we serialize attributes, and all theirs elements. The inner structure is :


readExternal

public void readExternal(java.io.ObjectInput in)
                  throws java.io.IOException,
                         java.lang.ClassNotFoundException
Specified by:
readExternal in interface java.io.Externalizable
Throws:
java.io.IOException
java.lang.ClassNotFoundException
See Also:
Externalizable#readExternal(ObjectInput)


Copyright © 2003-2011 Apache Software Foundation. All Rights Reserved.