com.ideanest.attributes.impl
Class SimpleBundle

java.lang.Object
  extended bycom.ideanest.attributes.impl.SimpleBundle
All Implemented Interfaces:
Bundle, java.io.Externalizable, java.io.Serializable

public class SimpleBundle
extends java.lang.Object
implements Bundle, java.io.Externalizable

A simple ordered bundle of attributes. This implementation keeps the attributes in an object array, and uses linear search to find entries. Normally, there's so few attributes in a bundle that the running time of a linear search should be comparable to more efficient methods (e.g. hash map). The array also allows duplicate keys, keeps the order of all the attributes, and should save memory.

See Also:
Serialized Form

Constructor Summary
SimpleBundle()
           
 
Method Summary
 void add(java.lang.Object attribute)
          Add the given attribute to this bundle.
 void add(java.lang.Object attribute, Creator creator)
          Add the given non-serializable attribute to this bundle along with a serializable creator that can be used to recreate it.
 void add(java.lang.String key, java.lang.String value)
          Add a simple attribute with the given key and value.
 void clear()
          Clear the contents of this bundle, removing all attributes.
 java.lang.Object get(java.lang.Class klass)
          Get an object attribute whose type is assignable to the given type.
 java.lang.String get(java.lang.String key)
          Get the string value matching the given key.
 boolean has(java.lang.Class klass)
          Return whether this bundle has any object attributes whose type is assignable to the given type.
 boolean has(java.lang.String key)
          Return whether this bundle has any string attributes for the given key.
 Bundle includingInherited()
          Return a composite bundle that includes all attributes inherited from supertypes by this element.
 java.util.Iterator iterator()
          An iterator over all the attributes in this bundle, in declaration order.
 java.util.Iterator iterator(java.lang.Class klass)
          Get an iterator over all object attributes whose type is assignable to the given type.
 java.util.Iterator iterator(java.lang.String key)
          Get an iterator over the values of all the simple attributes with the given key.
 void readExternal(java.io.ObjectInput in)
           
 int size()
          Return the number of attributes in this bundle.
 java.lang.Object[] toArray()
          Return a new array with all the attributes from this bundle.
 void writeExternal(java.io.ObjectOutput out)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SimpleBundle

public SimpleBundle()
Method Detail

add

public void add(java.lang.Object attribute)
Description copied from interface: Bundle
Add the given attribute to this bundle. If the attribute is not serializable, attempting to save this bundle will fail.

Specified by:
add in interface Bundle
Parameters:
attribute - the attribute to be added

add

public void add(java.lang.Object attribute,
                Creator creator)
Description copied from interface: Bundle
Add the given non-serializable attribute to this bundle along with a serializable creator that can be used to recreate it. If this bundle is saved, the creator will be serialized and used to reconstruct the attribute when the bundle is read back in.

Specified by:
add in interface Bundle
Parameters:
attribute - the attribute to be added
creator - a creator that can be used to re-create the attribute

add

public void add(java.lang.String key,
                java.lang.String value)
Description copied from interface: Bundle
Add a simple attribute with the given key and value.

Specified by:
add in interface Bundle
Parameters:
key - the key of the attribute to add
value - the value of the attribute to add

clear

public void clear()
Description copied from interface: Bundle
Clear the contents of this bundle, removing all attributes.

Specified by:
clear in interface Bundle

get

public java.lang.Object get(java.lang.Class klass)
Description copied from interface: Bundle
Get an object attribute whose type is assignable to the given type.

Specified by:
get in interface Bundle
Parameters:
klass - a supertype of the desired attribute type
Returns:
the attribute value, or null if none

get

public java.lang.String get(java.lang.String key)
Description copied from interface: Bundle
Get the string value matching the given key. If the key was defined with no value, return the empty string.

Specified by:
get in interface Bundle
Parameters:
key - the desired attribute's key
Returns:
the value for the given key, or null if the key is not present in this bundle

has

public boolean has(java.lang.Class klass)
Description copied from interface: Bundle
Return whether this bundle has any object attributes whose type is assignable to the given type.

Specified by:
has in interface Bundle
Parameters:
klass - a supertype of the desired attribute type
Returns:
true if there is at least one object attribute whose type is assignable to the given type, false otherwise

has

public boolean has(java.lang.String key)
Description copied from interface: Bundle
Return whether this bundle has any string attributes for the given key.

Specified by:
has in interface Bundle
Parameters:
key - the desired attribute key
Returns:
true if there is at least one string attribute for the given key, false otherwise

includingInherited

public Bundle includingInherited()
Description copied from interface: Bundle
Return a composite bundle that includes all attributes inherited from supertypes by this element. All elements contained in this bundle will also be included in the returned bundle. //TODO: explain attribute inheritance rules

Specified by:
includingInherited in interface Bundle
Returns:
a bundle that includes all inherited attributes for this element

iterator

public java.util.Iterator iterator()
Description copied from interface: Bundle
An iterator over all the attributes in this bundle, in declaration order. This iterator also provides the only way to selectively remove attributes from a bundle, though this functionality may not be supported by any given implementation or for specific bundles.

Specified by:
iterator in interface Bundle
Returns:
a new iterator over all attributes in this bundle

iterator

public java.util.Iterator iterator(java.lang.Class klass)
Description copied from interface: Bundle
Get an iterator over all object attributes whose type is assignable to the given type.

Specified by:
iterator in interface Bundle
Parameters:
klass - a supertype of the desired attribute type
Returns:
the array of attribute values, or an empty array if none

iterator

public java.util.Iterator iterator(java.lang.String key)
Description copied from interface: Bundle
Get an iterator over the values of all the simple attributes with the given key.

Specified by:
iterator in interface Bundle
Parameters:
key - the desired attributes' key
Returns:
the array of attribute values, or an empty array if none

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

size

public int size()
Description copied from interface: Bundle
Return the number of attributes in this bundle.

Specified by:
size in interface Bundle
Returns:
the number of attributes in this bundle

toArray

public java.lang.Object[] toArray()
Description copied from interface: Bundle
Return a new array with all the attributes from this bundle.

Specified by:
toArray in interface Bundle
Returns:
an array of the attributes in this bundle; changing it will not change the bundle's contents

writeExternal

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