org.livetribe.slp
Class Attributes

java.lang.Object
  extended by org.livetribe.slp.Attributes

public class Attributes
extends Object

Attributes are key-value pairs that describe a service. The attribute key is called tag, and the attribute value can be non-valued (the tag as no value), single-valued (the tag as only one value) or multi-valued (the tag has more values). Depending on the value(s) of the tag, the attribute has a type; there are four defined types:

Attribute values should be homogeneous: the attribute a=1,true,\FF\00 is illegal, because it's not clear if the type is long, boolean, or opaque.
Attributes can be used by UserAgents during service lookup to select appropriate services that match required conditions.
Attributes can be described using a string representation, for example:
 String attributesString = "(a=1,2),(b=true),(bytes=\FF\CA\FE\BA\BE),(present),(description=Something Interesting)";
 Attributes attributes = new Attributes(attributesString);
 
The example defines 5 attributes:

Version:
$Rev: 258 $ $Date: 2006-08-23 15:45:17 +0200 (Wed, 23 Aug 2006) $

Nested Class Summary
static class Attributes.Entry
          Represent the attribute value within the Attributes class.
 
Constructor Summary
Attributes()
          Creates an empty Attributes object.
Attributes(String attributeList)
          Creates an Attributes object parsing the given attributeList string.
 
Method Summary
 String asString()
          Returns a string representation of this Attributes object, that can be passed to Attributes(String) to be parsed.
static String bytesToOpaque(byte[] bytes)
          Returns an opaque string containing the escaped sequence of the given bytes, including the initial opaque prefix \FF.
 boolean equals(Object obj)
           
 Attributes.Entry getEntry(String tag)
          Returns the Entry for the given tag.
 Object getValue(String tag)
          Returns the value for the given tag.
 Object[] getValues(String tag)
          Returns the values for the given tag.
 int hashCode()
           
 boolean isEmpty()
          Returns true if this Attributes object is empty.
 boolean isTagPresent(String tag)
          Returns true if the given tag is present in this Attributes object.
 Attributes merge(Attributes that)
          Merges the attributes of this Attributes object with the attributes of the given Attributes object into a new Attributes object.
static byte[] opaqueToBytes(String opaqueString)
          Returns a byte array containing the bytes parsed from the given opaque string, except the initial opaque prefix \FF.
 void put(String tag)
          Puts a presence tag.
 void put(String tag, String value)
          Puts a tag with the specified value.
 void put(String tag, String[] values)
          Puts a tag with the specified values
 String toString()
           
 Attributes unmerge(Attributes that)
          Unmerges the attributes of this Attributes object with the attributes of the given Attributes object into a new Attributes object.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Attributes

public Attributes()
Creates an empty Attributes object.


Attributes

public Attributes(String attributeList)
           throws ServiceLocationException
Creates an Attributes object parsing the given attributeList string.

Parameters:
attributeList - The string containing the attributes to parse
Throws:
ServiceLocationException - If the parsing fails
Method Detail

put

public void put(String tag)
Puts a presence tag.

Parameters:
tag - The tag to add

put

public void put(String tag,
                String value)
Puts a tag with the specified value.

Parameters:
tag - The tag to add
value - The value of the tag

put

public void put(String tag,
                String[] values)
         throws ServiceLocationException
Puts a tag with the specified values

Parameters:
tag - The tag to add
values - The values of the tag
Throws:
ServiceLocationException - If the values are not of the same type

getEntry

public Attributes.Entry getEntry(String tag)
Returns the Entry for the given tag.


getValue

public Object getValue(String tag)
Returns the value for the given tag.
Depending on the tag type, it returns:
  • null, if the tag is not present
  • null, if the tag is present and it is a presence tag
  • the tag value, if the tag is present and it is a single valued tag
  • the first tag value, if the tag is present and it is a multi valued tag

See Also:
isTagPresent(String), getValues(String)

getValues

public Object[] getValues(String tag)
Returns the values for the given tag.
Depending on the tag type, it returns:
  • null, if the tag is not present
  • null, if the tag is present and it is a presence tag
  • the tag value, wrapped in an Object[] of length 1, if the tag is present and it is a single valued tag
  • the tag values, if the tag is present and it is a multi valued tag

See Also:
isTagPresent(String), getValue(String)

isEmpty

public boolean isEmpty()
Returns true if this Attributes object is empty.


isTagPresent

public boolean isTagPresent(String tag)
Returns true if the given tag is present in this Attributes object.

See Also:
Attributes.Entry.isPresenceType()

opaqueToBytes

public static byte[] opaqueToBytes(String opaqueString)
                            throws ServiceLocationException
Returns a byte array containing the bytes parsed from the given opaque string, except the initial opaque prefix \FF.

Parameters:
opaqueString - The opaque string containing the bytes to parse
Throws:
ServiceLocationException - If the parsing fails
See Also:
bytesToOpaque(byte[])

bytesToOpaque

public static String bytesToOpaque(byte[] bytes)
Returns an opaque string containing the escaped sequence of the given bytes, including the initial opaque prefix \FF.

Parameters:
bytes - The bytes to escape into an opaque string
See Also:
opaqueToBytes(String)

equals

public boolean equals(Object obj)
Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object

asString

public String asString()
Returns a string representation of this Attributes object, that can be passed to Attributes(String) to be parsed.


toString

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

merge

public Attributes merge(Attributes that)
Merges the attributes of this Attributes object with the attributes of the given Attributes object into a new Attributes object. If the given Attributes is null, a clone of this Attributes object will be returned. If this Attributes contains a tag that exists in the given Attributes, the merged Attributes will contain the entry from the given Attributes object (overwriting the one from this Attributes object).

Parameters:
that - The Attributes to merge with
Returns:
A new Attributes object containing the merged attributes.

unmerge

public Attributes unmerge(Attributes that)
Unmerges the attributes of this Attributes object with the attributes of the given Attributes object into a new Attributes object. If the given Attributes is null, a clone of this Attributes object will be returned. The unmerged Attributes will contain only the attributes present in this Attributes object but not in the given Attributes object.

Parameters:
that - The Attributes to unmerge with
Returns:
A new Attributes object containing the unmerged attributes.


Copyright © 2006-2012 LiveTribe. All Rights Reserved.