public abstract class AbstractElement extends Object implements Element
This class is the Base class for Node
,
Edge
and Graph
.
An element is made of an unique and arbitrary identifier that identifies it,
and a set of attributes.
Modifier and Type | Class and Description |
---|---|
static class |
AbstractElement.AttributeChangeEvent |
Modifier and Type | Field and Description |
---|---|
protected HashMap<String,Object> |
attributes
Attributes map.
|
protected ArrayList<String> |
attributesBeingRemoved
Vector used when removing attributes to avoid recursive removing.
|
protected String |
id
Tag of this element.
|
private int |
index
The index of this element.
|
Constructor and Description |
---|
AbstractElement(String id)
New element.
|
Modifier and Type | Method and Description |
---|---|
void |
addAttribute(String attribute,
Object... values)
Add or replace the value of an attribute.
|
void |
addAttributes(Map<String,Object> attributes)
Add or replace each attribute found in attributes.
|
protected abstract void |
attributeChanged(AbstractElement.AttributeChangeEvent event,
String attribute,
Object oldValue,
Object newValue)
Called for each change in the attribute set.
|
void |
changeAttribute(String attribute,
Object... values)
Like
Element.addAttribute(String, Object...) but for consistency. |
void |
clearAttributes()
Remove all registered attributes.
|
protected void |
clearAttributesWithNoEvent() |
Object[] |
getArray(String key)
Get the array of objects bound to key.
|
<T> T |
getAttribute(String key)
Get the attribute object bound to the given key.
|
<T> T |
getAttribute(String key,
Class<T> clazz)
Get the attribute object bound to the given key if it is an instance of
the given class.
|
int |
getAttributeCount()
Number of attributes stored in this element.
|
Iterator<String> |
getAttributeKeyIterator()
Iterator on all attributes keys.
|
Collection<String> |
getAttributeKeySet()
An unmodifiable view on the set of attribute keys.
|
Iterable<String> |
getEachAttributeKey()
An iterable view on the set of attribute keys usable within a for-each
loop.
|
<T> T |
getFirstAttributeOf(Class<T> clazz,
String... keys)
Like
Element.getAttribute(String, Class) , but returns the first existing
attribute in a list of keys, instead of only one key. |
<T> T |
getFirstAttributeOf(String... keys)
Like
Element.getAttribute(String) , but returns the first existing
attribute in a list of keys, instead of only one key. |
HashMap<?,?> |
getHash(String key)
Get the hash bound to key.
|
String |
getId()
Unique identifier of this element.
|
int |
getIndex()
The current index of this element
|
CharSequence |
getLabel(String key)
Get the label string bound to the given key key.
|
double |
getNumber(String key)
Get the number bound to key.
|
ArrayList<? extends Number> |
getVector(String key)
Get the vector of number bound to key.
|
boolean |
hasArray(String key)
Does this element store an array value for the given key?
|
boolean |
hasAttribute(String key)
Does this element store a value for the given attribute key?
|
boolean |
hasAttribute(String key,
Class<?> clazz)
Does this element store a value for the given attribute key and this
value is an instance of the given class?
|
boolean |
hasHash(String key)
Does this element store a hash value for the given key?
|
boolean |
hasLabel(String key)
Does this element store a label value for the given key?
|
boolean |
hasNumber(String key)
Does this element store a number for the given key?
|
boolean |
hasVector(String key)
Does this element store a vector value for the given key?
|
protected abstract boolean |
nullAttributesAreErrors() |
void |
removeAttribute(String attribute)
Remove an attribute.
|
void |
setAttribute(String attribute,
Object... values)
Like
Element.addAttribute(String, Object...) but for consistency. |
protected void |
setIndex(int index)
Used by subclasses to change the index of an element
|
String |
toString()
Override the Object method
|
private int index
protected HashMap<String,Object> attributes
protected ArrayList<String> attributesBeingRemoved
public AbstractElement(String id)
id
- The unique identifier of this element.public String getId()
Element
public int getIndex()
Element
protected void setIndex(int index)
index
- the new indexprotected abstract boolean nullAttributesAreErrors()
protected abstract void attributeChanged(AbstractElement.AttributeChangeEvent event, String attribute, Object oldValue, Object newValue)
sourceId
- The source of the change.timeId
- The source time of the change, for synchronization.attribute
- The attribute name that changed.event
- The type of event among ADD, CHANGE and REMOVE.oldValue
- The old value of the attribute, null if the attribute was
added.newValue
- The new value of the attribute, null if the attribute is about
to be removed.public <T> T getAttribute(String key)
Element
getAttribute
in interface Element
key
- Name of the attribute to search.public <T> T getFirstAttributeOf(String... keys)
Element
Element.getAttribute(String)
, but returns the first existing
attribute in a list of keys, instead of only one key. The key list order
matters.getFirstAttributeOf
in interface Element
keys
- Several strings naming attributes.public <T> T getAttribute(String key, Class<T> clazz)
Element
getAttribute
in interface Element
key
- The attribute name to search.clazz
- The expected attribute class.public <T> T getFirstAttributeOf(Class<T> clazz, String... keys)
Element
Element.getAttribute(String, Class)
, but returns the first existing
attribute in a list of keys, instead of only one key. The key list order
matters.getFirstAttributeOf
in interface Element
clazz
- The class the attribute must be instance of.keys
- Several string naming attributes.public CharSequence getLabel(String key)
Element
public double getNumber(String key)
Element
public ArrayList<? extends Number> getVector(String key)
Element
public Object[] getArray(String key)
Element
public HashMap<?,?> getHash(String key)
Element
CompoundAttribute
interface are considered like hashes since they
can be transformed to a hash. If an attribute with the same name exists
but is not a hash, null is returned. We cannot enforce the type of the
key. It is considered a string and you should use "Object.toString()" to
get it.public boolean hasAttribute(String key)
Element
hasAttribute
in interface Element
key
- The name of the attribute to search.public boolean hasAttribute(String key, Class<?> clazz)
Element
hasAttribute
in interface Element
key
- The name of the attribute to search.clazz
- The expected class of the attribute value.public boolean hasLabel(String key)
Element
public boolean hasNumber(String key)
Element
public boolean hasVector(String key)
Element
public boolean hasArray(String key)
Element
public boolean hasHash(String key)
Element
CompoundAttribute
class.public Iterator<String> getAttributeKeyIterator()
Element
getAttributeKeyIterator
in interface Element
public Iterable<String> getEachAttributeKey()
Element
getEachAttributeKey
in interface Element
public Collection<String> getAttributeKeySet()
Element
getAttributeKeySet
in interface Element
public int getAttributeCount()
Element
getAttributeCount
in interface Element
public void clearAttributes()
Element
clearAttributes
in interface Element
protected void clearAttributesWithNoEvent()
public void addAttribute(String attribute, Object... values)
Element
addAttribute
in interface Element
attribute
- The attribute name.values
- The attribute value or set of values.public void changeAttribute(String attribute, Object... values)
Element
Element.addAttribute(String, Object...)
but for consistency.changeAttribute
in interface Element
attribute
- The attribute name.values
- The attribute value or array of values.Element.addAttribute(String, Object...)
public void setAttribute(String attribute, Object... values)
Element
Element.addAttribute(String, Object...)
but for consistency.setAttribute
in interface Element
attribute
- The attribute name.values
- The attribute value or array of values.Element.addAttribute(String, Object...)
public void addAttributes(Map<String,Object> attributes)
Element
addAttributes
in interface Element
attributes
- A set of (key,value) pairs.public void removeAttribute(String attribute)
Element
removeAttribute
in interface Element
attribute
- Name of the attribute to remove.WebARTS Library Licensed Under the GNU - General Public License. Other Libraries licensed under their respective Open Source Licenses