Class AttributeBuilder

  • All Implemented Interfaces:
    Iterable<ByteString>

    @PublicAPI(stability=UNCOMMITTED,
               mayInstantiate=true,
               mayExtend=false,
               mayInvoke=true)
    public final class AttributeBuilder
    extends Object
    implements Iterable<ByteString>
    This class provides an interface for creating new non-virtual Attributes, or "real" attributes.

    An attribute can be created incrementally using either AttributeBuilder(AttributeType) or AttributeBuilder(AttributeType, String). The caller is then free to add new options using setOption(String) and new values using add(ByteString) or addAll(Collection). Once all the options and values have been added, the attribute can be retrieved using the toAttribute() method.

    A real attribute can also be created based on the values taken from another attribute using the AttributeBuilder(Attribute) constructor. The caller is then free to modify the values within the attribute before retrieving the updated attribute using the toAttribute() method.

    The Attributes class contains convenience factory methods, e.g. Attributes.empty(String) for creating empty attributes, and Attributes.create(String, String) for creating single-valued attributes.

    AttributeBuilders can be re-used. Once an AttributeBuilder has been converted to an Attribute using toAttribute(), its state is reset so that its attribute type, user-provided name, options, and values are all undefined:

     AttributeBuilder builder = new AttributeBuilder();
     for (int i = 0; i < 10; i++)
     {
       builder.setAttributeType("myAttribute" + i);
       builder.setOption("an-option");
       builder.add("a value");
       Attribute attribute = builder.toAttribute();
       // Do something with attribute...
     }
     

    Implementation Note: this class is optimized for the common case where there is only a single value. By doing so, we avoid using unnecessary storage space and also performing any unnecessary normalization. In addition, this class is optimized for the common cases where there are zero or one attribute type options.

    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean add​(String valueString)
      Adds the specified attribute value to this attribute builder if it is not already present.
      boolean add​(ByteString attributeValue)
      Adds the specified attribute value to this attribute builder if it is not already present.
      boolean addAll​(Collection<ByteString> values)
      Adds the specified attribute values to this attribute builder if they are not already present.
      boolean addAll​(Attribute attribute)
      Adds all the values from the specified attribute to this attribute builder if they are not already present.
      boolean addAllStrings​(Collection<? extends Object> values)
      Adds the specified attribute values to this attribute builder if they are not already present.
      void clear()
      Removes all attribute values from this attribute builder.
      boolean contains​(ByteString value)
      Indicates whether this attribute builder contains the specified value.
      boolean containsAll​(Collection<?> values)
      Indicates whether this attribute builder contains all the values in the collection.
      boolean isEmpty()
      Returns true if this attribute builder contains no attribute values.
      Iterator<ByteString> iterator()
      Returns an iterator over the attribute values in this attribute builder.
      boolean optionsEqual​(AttributeDescription attributeDescription)
      Indicates whether this attribute builder has exactly the specified set of options.
      boolean remove​(String valueString)
      Removes the specified attribute value from this attribute builder if it is present.
      boolean remove​(ByteString value)
      Removes the specified attribute value from this attribute builder if it is present.
      boolean removeAll​(Collection<ByteString> values)
      Removes the specified attribute values from this attribute builder if they are present.
      boolean removeAll​(Attribute attribute)
      Removes all the values from the specified attribute from this attribute builder if they are not already present.
      void replace​(String valueString)
      Replaces all the values in this attribute value with the specified attribute value.
      void replace​(ByteString value)
      Replaces all the values in this attribute value with the specified attribute value.
      void replaceAll​(Collection<ByteString> values)
      Replaces all the values in this attribute value with the specified attribute values.
      void replaceAll​(Attribute attribute)
      Replaces all the values in this attribute value with the attributes from the specified attribute.
      boolean setOption​(String option)
      Adds the specified option to this attribute builder if it is not already present.
      boolean setOptions​(Iterable<String> options)
      Adds the specified options to this attribute builder if they are not already present.
      int size()
      Returns the number of attribute values in this attribute builder.
      Attribute toAttribute()
      Returns an attribute representing the content of this attribute builder.
      List<Attribute> toAttributeList()
      Returns a List with a single attribute representing the content of this attribute builder.
      String toString()  
    • Constructor Detail

      • AttributeBuilder

        public AttributeBuilder​(Attribute attribute)
        Creates a new attribute builder from an existing attribute.

        Modifications to the attribute builder will not impact the provided attribute.

        Parameters:
        attribute - The attribute to be copied.
      • AttributeBuilder

        public AttributeBuilder​(AttributeDescription attributeDescription)
        Creates a new attribute builder with the specified description.
        Parameters:
        attributeDescription - The attribute description for this attribute builder.
      • AttributeBuilder

        public AttributeBuilder​(AttributeType attributeType)
        Creates a new attribute builder with the specified type and no options and no values.
        Parameters:
        attributeType - The attribute type for this attribute builder.
      • AttributeBuilder

        public AttributeBuilder​(AttributeType attributeType,
                                String name)
        Creates a new attribute builder with the specified type and user-provided name and no options and no values.
        Parameters:
        attributeType - The attribute type for this attribute builder.
        name - The user-provided name for this attribute builder.
      • AttributeBuilder

        public AttributeBuilder​(String attributeDescription)
        Creates a new attribute builder with the specified attribute description and no values.

        If the attribute name cannot be found in the schema, a new attribute type is created using the default attribute syntax.

        Parameters:
        attributeDescription - The attribute description for this attribute builder.
    • Method Detail

      • add

        public boolean add​(String valueString)
        Adds the specified attribute value to this attribute builder if it is not already present.
        Parameters:
        valueString - The string representation of the attribute value to be added to this attribute builder.
        Returns:
        true if this attribute builder did not already contain the specified attribute value.
      • add

        public boolean add​(ByteString attributeValue)
        Adds the specified attribute value to this attribute builder if it is not already present.
        Parameters:
        attributeValue - The ByteString representation of the attribute value to be added to this attribute builder.
        Returns:
        true if this attribute builder did not already contain the specified attribute value.
      • addAll

        public boolean addAll​(Attribute attribute)
        Adds all the values from the specified attribute to this attribute builder if they are not already present.
        Parameters:
        attribute - The attribute containing the values to be added to this attribute builder.
        Returns:
        true if this attribute builder was modified.
      • addAll

        public boolean addAll​(Collection<ByteString> values)
        Adds the specified attribute values to this attribute builder if they are not already present.
        Parameters:
        values - The attribute values to be added to this attribute builder.
        Returns:
        true if this attribute builder was modified.
      • addAllStrings

        public boolean addAllStrings​(Collection<? extends Object> values)
        Adds the specified attribute values to this attribute builder if they are not already present.
        Parameters:
        values - The attribute values to be added to this attribute builder.
        Returns:
        true if this attribute builder was modified.
        Throws:
        NullPointerException - if any of the values is null
      • clear

        public void clear()
        Removes all attribute values from this attribute builder.
      • contains

        public boolean contains​(ByteString value)
        Indicates whether this attribute builder contains the specified value.
        Parameters:
        value - The value for which to make the determination.
        Returns:
        true if this attribute builder has the specified value, or false if not.
      • containsAll

        public boolean containsAll​(Collection<?> values)
        Indicates whether this attribute builder contains all the values in the collection.
        Parameters:
        values - The set of values for which to make the determination.
        Returns:
        true if this attribute builder contains all the values in the provided collection, or false if it does not contain at least one of them.
      • isEmpty

        public boolean isEmpty()
        Returns true if this attribute builder contains no attribute values.
        Returns:
        true if this attribute builder contains no attribute values.
      • iterator

        public Iterator<ByteString> iterator()
        Returns an iterator over the attribute values in this attribute builder. The attribute values are returned in the order in which they were added to this attribute builder. The returned iterator supports attribute value removals via its remove method.
        Specified by:
        iterator in interface Iterable<ByteString>
        Returns:
        An iterator over the attribute values in this attribute builder.
      • remove

        public boolean remove​(ByteString value)
        Removes the specified attribute value from this attribute builder if it is present.
        Parameters:
        value - The attribute value to be removed from this attribute builder.
        Returns:
        true if this attribute builder contained the specified attribute value.
      • remove

        public boolean remove​(String valueString)
        Removes the specified attribute value from this attribute builder if it is present.
        Parameters:
        valueString - The string representation of the attribute value to be removed from this attribute builder.
        Returns:
        true if this attribute builder contained the specified attribute value.
      • removeAll

        public boolean removeAll​(Attribute attribute)
        Removes all the values from the specified attribute from this attribute builder if they are not already present.
        Parameters:
        attribute - The attribute containing the values to be removed from this attribute builder.
        Returns:
        true if this attribute builder was modified.
      • removeAll

        public boolean removeAll​(Collection<ByteString> values)
        Removes the specified attribute values from this attribute builder if they are present.
        Parameters:
        values - The attribute values to be removed from this attribute builder.
        Returns:
        true if this attribute builder was modified.
      • replace

        public void replace​(ByteString value)
        Replaces all the values in this attribute value with the specified attribute value.
        Parameters:
        value - The attribute value to replace all existing values.
      • replace

        public void replace​(String valueString)
        Replaces all the values in this attribute value with the specified attribute value.
        Parameters:
        valueString - The string representation of the attribute value to replace all existing values.
      • replaceAll

        public void replaceAll​(Attribute attribute)
        Replaces all the values in this attribute value with the attributes from the specified attribute.
        Parameters:
        attribute - The attribute containing the values to replace all existing values.
      • replaceAll

        public void replaceAll​(Collection<ByteString> values)
        Replaces all the values in this attribute value with the specified attribute values.
        Parameters:
        values - The attribute values to replace all existing values.
      • setOption

        public boolean setOption​(String option)
        Adds the specified option to this attribute builder if it is not already present.
        Parameters:
        option - The option to be added to this attribute builder.
        Returns:
        true if this attribute builder did not already contain the specified option.
      • setOptions

        public boolean setOptions​(Iterable<String> options)
        Adds the specified options to this attribute builder if they are not already present.
        Parameters:
        options - The options to be added to this attribute builder.
        Returns:
        true if this attribute builder was modified.
      • optionsEqual

        public boolean optionsEqual​(AttributeDescription attributeDescription)
        Indicates whether this attribute builder has exactly the specified set of options.
        Parameters:
        attributeDescription - The attribute description containing the set of options for which to make the determination
        Returns:
        true if this attribute builder has exactly the specified set of options.
      • size

        public int size()
        Returns the number of attribute values in this attribute builder.
        Returns:
        The number of attribute values in this attribute builder.
      • toAttribute

        public Attribute toAttribute()
                              throws IllegalStateException
        Returns an attribute representing the content of this attribute builder.

        For efficiency purposes this method resets the content of this attribute builder so that it no longer contains any options or values and its attribute type is null.

        Returns:
        An attribute representing the content of this attribute builder.
        Throws:
        IllegalStateException - If this attribute builder has an undefined attribute type or name.
      • toAttributeList

        public List<Attribute> toAttributeList()
                                        throws IllegalStateException
        Returns a List with a single attribute representing the content of this attribute builder.

        For efficiency purposes this method resets the content of this attribute builder so that it no longer contains any options or values and its attribute type is null.

        Returns:
        A List with a single attribute representing the content of this attribute builder.
        Throws:
        IllegalStateException - If this attribute builder has an undefined attribute type or name.