Class DefaultAttribute

java.lang.Object
org.apache.directory.api.ldap.model.entry.DefaultAttribute
All Implemented Interfaces:
Externalizable, Serializable, Cloneable, Iterable<Value>, Attribute

public class DefaultAttribute extends Object implements Attribute, Cloneable
An LDAP attribute.

To define the kind of data stored, the client must set the isHR flag, or inject an AttributeType.

Author:
Apache Directory Project
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    DefaultAttribute(byte[] upId)
    Create a new instance of an Attribute, without value.
    Create a new instance of an Attribute, without value.
    DefaultAttribute(String upId, byte[]... vals)
    Create a new instance of an Attribute, with some binary values, and a user provided ID.
    DefaultAttribute(String upId, String... vals)
    Create a new instance of an Attribute, with some String values, and a user provided ID.
    DefaultAttribute(String upId, Value... vals)
    Create a new instance of an Attribute, with some values, and a user provided ID.
    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.
    DefaultAttribute(String upId, AttributeType attributeType)
    Create a new instance of a schema aware Attribute, without value.
    DefaultAttribute(String upId, AttributeType attributeType, byte[]... vals)
    Create a new instance of a schema aware Attribute, with some byte[] values, and a user provided ID.
    DefaultAttribute(String upId, AttributeType attributeType, String... vals)
    Create a new instance of a schema aware Attribute, with some values, and a user provided ID.
    DefaultAttribute(String upId, AttributeType attributeType, Value... vals)
    Create a new instance of a schema aware Attribute, with some values, and a user provided ID.
    If the value does not correspond to the same attributeType, then it's wrapped value is copied into a new Value which uses the specified attributeType.
    Create a new instance of a schema aware Attribute, without ID nor value.
    DefaultAttribute(AttributeType attributeType, byte[]... vals)
    Create a new instance of a schema aware Attribute, with some byte[] values.
    DefaultAttribute(AttributeType attributeType, String... vals)
    Create a new instance of a schema aware Attribute, without ID but with some values.
    DefaultAttribute(AttributeType attributeType, Attribute attribute)
    Creates a new instance of schema aware Attribute, by copying another attribute.
    DefaultAttribute(AttributeType attributeType, Value... vals)
    Create a new instance of a schema aware Attribute, with some values.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    add(byte[]... vals)
    Adds some values to this attribute.
    int
    add(String... vals)
    Adds some values to this attribute.
    int
    add(Value... vals)
    Adds some values to this attribute.
    void
    apply(AttributeType attributeType)
    Set the attribute type associated with this EntryAttribute.
    void
    Remove all the values from this attribute.
    boolean
    contains(byte[]... vals)
    Indicates whether the specified values are some of the attribute's values.
    boolean
    contains(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
     
    get()
    Get the first value of this attribute.
    Get the attribute type associated with this EntryAttribute.
    byte[]
    Get the byte[] value, if and only if the value is known to be Binary, otherwise a InvalidAttributeValueException will be thrown
    Get's the attribute identifier for this entry.
    Get the String value, if and only if the value is known to be a String, otherwise a InvalidAttributeValueException will be thrown
    Get's the user provided identifier for this entry.
    int
    The hashCode is based on the id, the isHR flag and on the internal values.
    boolean
    Tells if the attribute is human readable.
    boolean
    isInstanceOf(AttributeType attributeType)
    Check if the current attribute type has the same type (or is a descendant of) than the given attributeType
    boolean
    isValid(AttributeType attributeType)
    Checks to see if this attribute is valid along with the values it contains.
    An iterator on top of the stored values.
    void
    boolean
    remove(byte[]... vals)
    Removes all the values that are equal to the given values.
    boolean
    remove(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
    setUpId(byte[] upId)
    Sets the User Provided ID as a byte[]
    void
    setUpId(byte[] upId, AttributeType attributeType)
    Sets the User Provided ID as a byte[]
    void
    Set the user provided ID.
    void
    setUpId(String upId, AttributeType attributeType)
    Set the user provided ID.
    int
    Retrieves the number of values in this attribute.
     
    A pretty-pinter for Attribute
    void
    This is the place where we serialize attributes, and all theirs elements.

    Methods inherited from class Object

    finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface Iterable

    forEach, spliterator
  • Constructor Details

    • DefaultAttribute

      public DefaultAttribute(AttributeType attributeType)
      Create a new instance of a schema aware Attribute, without ID nor value.
      Parameters:
      attributeType - the attributeType for the empty attribute added into the entry
    • DefaultAttribute

      public DefaultAttribute(String upId)
      Create a new instance of an Attribute, without value.
      Parameters:
      upId - The user provided ID
    • DefaultAttribute

      public DefaultAttribute(byte[] upId)
      Create a new instance of an Attribute, without value.
      Parameters:
      upId - The user provided ID
    • DefaultAttribute

      public DefaultAttribute(String upId, AttributeType attributeType)
      Create a new instance of a schema aware Attribute, without value.
      Parameters:
      upId - the ID for the added attributeType
      attributeType - the added AttributeType
    • DefaultAttribute

      public DefaultAttribute(String upId, Value... vals)
      Create a new instance of an Attribute, with some values, and a user provided ID.
      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 - the attributeType ID
      vals - an initial set of values for this attribute
    • DefaultAttribute

      public DefaultAttribute(AttributeType attributeType, String... vals) throws LdapInvalidAttributeValueException
      Create a new instance of a schema aware Attribute, without ID but with some values.
      Parameters:
      attributeType - The attributeType added on creation
      vals - The added value for this attribute
      Throws:
      LdapInvalidAttributeValueException - If any of the added values is not valid
    • DefaultAttribute

      public DefaultAttribute(String upId, AttributeType attributeType, String... vals) throws LdapInvalidAttributeValueException
      Create a new instance of a schema aware Attribute, with some values, and a user provided ID.
      Parameters:
      upId - the ID for the created attribute
      attributeType - The attributeType added on creation
      vals - the added values for this attribute
      Throws:
      LdapInvalidAttributeValueException - If any of the added values is not valid
    • DefaultAttribute

      public DefaultAttribute(String upId, AttributeType attributeType, Value... vals) throws LdapInvalidAttributeValueException
      Create a new instance of a schema aware Attribute, with some values, and a user provided ID.
      If the value does not correspond to the same attributeType, then it's wrapped value is copied into a new Value which uses the specified attributeType.

      Otherwise, the value is stored, but as a reference. It's not a copy.

      Parameters:
      upId - the ID of the created attribute
      attributeType - the attribute type according to the schema
      vals - an initial set of values for this attribute
      Throws:
      LdapInvalidAttributeValueException - If any of the added values is not valid
    • DefaultAttribute

      public DefaultAttribute(AttributeType attributeType, Value... vals) throws LdapInvalidAttributeValueException
      Create a new instance of a schema aware Attribute, with some values.

      If the value does not correspond to the same attributeType, then it's wrapped value is copied into a new Value which uses the specified attributeType.

      Parameters:
      attributeType - the attribute type according to the schema
      vals - an initial set of values for this attribute
      Throws:
      LdapInvalidAttributeValueException - If one of the value is invalid
    • DefaultAttribute

      public DefaultAttribute(String upId, String... vals)
      Create a new instance of an Attribute, with some String values, and a user provided ID.
      Parameters:
      upId - the ID of the created attribute
      vals - an initial set of String values for this attribute
    • DefaultAttribute

      public DefaultAttribute(String upId, byte[]... vals)
      Create a new instance of an Attribute, with some binary values, and a user provided ID.
      Parameters:
      upId - the ID of the created attribute
      vals - an initial set of binary values for this attribute
    • DefaultAttribute

      public DefaultAttribute(AttributeType attributeType, byte[]... vals) throws LdapInvalidAttributeValueException
      Create a new instance of a schema aware Attribute, with some byte[] values.
      Parameters:
      attributeType - The attributeType added on creation
      vals - The added binary values
      Throws:
      LdapInvalidAttributeValueException - If any of the added values is not valid
    • DefaultAttribute

      public DefaultAttribute(String upId, AttributeType attributeType, byte[]... vals) throws LdapInvalidAttributeValueException
      Create a new instance of a schema aware Attribute, with some byte[] values, and a user provided ID.
      Parameters:
      upId - the ID for the added attribute
      attributeType - the AttributeType to be added
      vals - the binary values for the added attribute
      Throws:
      LdapInvalidAttributeValueException - If any of the added values is not valid
    • DefaultAttribute

      public DefaultAttribute(AttributeType attributeType, Attribute attribute) throws LdapException
      Creates a new instance of schema aware Attribute, by copying another attribute. If the initial Attribute is not schema aware, the copy will be if the attributeType argument is not null.
      Parameters:
      attributeType - The attribute's type
      attribute - The attribute to be copied
      Throws:
      LdapException - If the attribute can't be created
  • Method Details

    • 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 Attribute
      Returns:
      The value as a byte[]
      Throws:
      LdapInvalidAttributeValueException - If the value is a String
    • getString

      public 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 Attribute
      Returns:
      The value as a String
      Throws:
      LdapInvalidAttributeValueException - If the value is a byte[]
    • getId

      public String getId()
      Get's the attribute identifier for this entry. This is the value that will be used as the identifier for the attribute within the entry.
      Specified by:
      getId in interface Attribute
      Returns:
      the identifier for this attribute
    • getUpId

      public 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 Attribute
      Returns:
      the user provided identifier for this attribute
    • setUpId

      public void setUpId(String upId)
      Set the user provided ID. It will also set the ID, normalizing the upId (removing spaces before and after, and lower casing it)
      Specified by:
      setUpId in interface Attribute
      Parameters:
      upId - The attribute ID
    • setUpId

      public void setUpId(byte[] upId)
      Sets the User Provided ID as a byte[]
      Parameters:
      upId - The User Provided ID
    • setUpId

      public void setUpId(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 compatible with the new AttributeType.

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

      public void setUpId(byte[] upId, AttributeType attributeType)
      Sets the User Provided ID as a byte[]
      Parameters:
      upId - The User Provided ID
      attributeType - The asscoiated AttributeType
    • isHumanReadable

      public boolean isHumanReadable()

      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:
      isHumanReadable in interface Attribute
      Returns:
      true if the attribute is human readable
    • isValid

      public boolean isValid(AttributeType attributeType) throws LdapInvalidAttributeValueException
      Checks to see if this attribute is valid along with the values it contains.
      Specified by:
      isValid in interface Attribute
      Parameters:
      attributeType - The AttributeType
      Returns:
      true if the attribute and it's values are valid, false otherwise
      Throws:
      LdapInvalidAttributeValueException - 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 String Value into a Binary attribute, we just store the UTF-8 byte array encoding for this Value.

      If we try to store some Value in a HR attribute, we try to convert those Value 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 Attribute
      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
    • add

      public int add(String... vals) throws LdapInvalidAttributeValueException
      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 Attribute
      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
      Throws:
      LdapInvalidAttributeValueException - if some of the added values are not valid
    • add

      public int add(byte[]... vals) throws LdapInvalidAttributeValueException
      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 Attribute
      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
      Throws:
      LdapInvalidAttributeValueException - if some of the added values are not valid
    • clear

      public void clear()
      Remove all the values from this attribute.
      Specified by:
      clear in interface Attribute
    • 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 Attribute
      Parameters:
      vals - the values
      Returns:
      true if this attribute contains all the given values, otherwise false
    • contains

      public boolean contains(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 Attribute
      Parameters:
      vals - the values
      Returns:
      true if this attribute contains all the given 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 Attribute
      Parameters:
      vals - the values
      Returns:
      true if this attribute contains all the given values, otherwise false
    • get

      public Value get()

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

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

      Specified by:
      get in interface Attribute
      Returns:
      The first value for this attribute.
    • size

      public int size()
      Retrieves the number of values in this attribute.
      Specified by:
      size in interface Attribute
      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 Attribute
      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 Attribute
      Parameters:
      vals - the values to be removed
      Returns:
      true if all the values are removed, otherwise false
    • remove

      public boolean remove(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 Attribute
      Parameters:
      vals - the values to be removed
      Returns:
      true if all the values are removed, otherwise false
    • iterator

      public Iterator<Value> iterator()
      An iterator on top of the stored values.
      Specified by:
      iterator in interface Iterable<Value>
      Returns:
      an iterator over the stored values.
    • getAttributeType

      public AttributeType getAttributeType()
      Get the attribute type associated with this EntryAttribute.
      Specified by:
      getAttributeType in interface Attribute
      Returns:
      the attributeType associated with this entry attribute
    • apply

      public void apply(AttributeType attributeType) throws LdapInvalidAttributeValueException

      Set the attribute type associated with this EntryAttribute.

      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:
      apply in interface Attribute
      Parameters:
      attributeType - the attributeType associated with this entry attribute
      Throws:
      LdapInvalidAttributeValueException - if the contained values are not valid accordingly to the added AttributeType
    • isInstanceOf

      public boolean isInstanceOf(AttributeType attributeType) throws LdapInvalidAttributeValueException

      Check if the current attribute type has the same type (or is a descendant of) than the given attributeType

      Specified by:
      isInstanceOf in interface Attribute
      Parameters:
      attributeType - The AttributeType to check
      Returns:
      true if the current attribute is of the expected attributeType or a descendant of it
      Throws:
      LdapInvalidAttributeValueException - If there is no AttributeType
    • hashCode

      public int hashCode()
      The hashCode is based on the id, the isHR flag and on the internal values.
      Overrides:
      hashCode in class Object
      Returns:
      the instance's hashcode
      See Also:
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
      See Also:
    • clone

      public Attribute clone()
      Specified by:
      clone in interface Attribute
      Overrides:
      clone in class Object
      Returns:
      A clone of the current object
    • writeExternal

      public void writeExternal(ObjectOutput out) throws IOException
      This is the place where we serialize attributes, and all theirs elements.
      Specified by:
      writeExternal in interface Externalizable
      Throws:
      IOException
    • readExternal

      public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
      Specified by:
      readExternal in interface Externalizable
      Throws:
      IOException
      ClassNotFoundException
    • toString

      public String toString()
      Overrides:
      toString in class Object
      See Also:
    • toString

      public String toString(String tabs)
      A pretty-pinter for Attribute
      Specified by:
      toString in interface Attribute
      Parameters:
      tabs - The tabs to add before any output
      Returns:
      The pretty-printed entry