ptolemy.graph
Class ElementList

java.lang.Object
  extended by ptolemy.graph.LabeledList
      extended by ptolemy.graph.ElementList
All Implemented Interfaces:
java.lang.Iterable, java.util.Collection, java.util.List

public class ElementList
extends LabeledList

A list of graph elements. This class manages the storage and weight information associated with a list of unique graph elements. This class is normally for use internally within graph classes.

Since:
Ptolemy II 2.0
Version:
$Id: ElementList.java 57040 2010-01-27 20:52:32Z cxh $
Author:
Shuvra S. Bhattacharyya
Accepted Rating:
Red (cxh)
Proposed Rating:
Red (cxh)

Field Summary
private  java.lang.String _descriptor
           
private static java.util.Collection _emptyCollection
           
private  Graph _graph
           
private  java.util.HashSet _unweightedSet
           
private  java.util.HashMap _weightMap
           
 
Constructor Summary
ElementList(java.lang.String descriptor, Graph graph)
          Construct an empty element list.
ElementList(java.lang.String descriptor, Graph graph, int elementCount)
          Construct an empty element list with enough storage allocated for the specified number of elements.
 
Method Summary
 boolean cancelWeight(Element element)
          Disassociate the given element from its weight information.
 boolean changeWeight(Element element)
          Given an element in this list, check if the weight has changed (since the element was added to the graph or was last validated, whichever is more recent), and if so, change the current mapping of a weight to the element or remove the element from the set of unweighted elements.
 void clear()
          Clear all of the elements in this list.
 boolean containsWeight(java.lang.Object weight)
          Test if the specified object is an element weight in this list.
 Element element(java.lang.Object weight)
          Return an element in this list that has a specified weight.
 java.util.Collection elements()
          Return all the elements in this list in the form of an unmodifiable collection.
 java.util.Collection elements(java.lang.Object weight)
          Return all the elements in this graph that have a specified weight.
 void registerWeight(Element element)
          Associate a graph element to its weight given the relevant mapping of weights to elements, and the set of unweighted elements of the same type (nodes or edges).
 boolean remove(Element element)
          Remove an element from this list if it exists in the list.
 boolean validateWeight(Element element, java.lang.Object oldWeight)
          Validate the weight of a given graph element, given the previous weight of that element.
 
Methods inherited from class ptolemy.graph.LabeledList
add, add, addAll, addAll, contains, containsAll, equals, get, hashCode, indexOf, isEmpty, iterator, label, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, retainAll, set, size, subList, toArray, toArray, toString, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

_descriptor

private java.lang.String _descriptor

_graph

private Graph _graph

_emptyCollection

private static final java.util.Collection _emptyCollection

_unweightedSet

private java.util.HashSet _unweightedSet

_weightMap

private java.util.HashMap _weightMap
Constructor Detail

ElementList

public ElementList(java.lang.String descriptor,
                   Graph graph)
Construct an empty element list.

Parameters:
descriptor - A one-word description of the type of elements that are to be stored in this list.
graph - The graph associated with this element list.

ElementList

public ElementList(java.lang.String descriptor,
                   Graph graph,
                   int elementCount)
Construct an empty element list with enough storage allocated for the specified number of elements. Memory management is more efficient with this constructor if the number of elements is known.

Parameters:
descriptor - A one-word description of the type of elements that are to be stored in this list.
graph - The graph associated with this element list.
elementCount - The number of elements.
Method Detail

cancelWeight

public boolean cancelWeight(Element element)
Disassociate the given element from its weight information.

Parameters:
element - The element.
Returns:
True if the weight information was disassociated.

changeWeight

public boolean changeWeight(Element element)
Given an element in this list, check if the weight has changed (since the element was added to the graph or was last validated, whichever is more recent), and if so, change the current mapping of a weight to the element or remove the element from the set of unweighted elements.

Parameters:
element - The graph element.
Returns:
True if the weight associated with the element has changed as determined by the equals method.

clear

public void clear()
Clear all of the elements in this list.

Specified by:
clear in interface java.util.Collection
Specified by:
clear in interface java.util.List
Overrides:
clear in class LabeledList

containsWeight

public boolean containsWeight(java.lang.Object weight)
Test if the specified object is an element weight in this list. Equality is determined by the equals method. If the specified weight is null, return false.

Parameters:
weight - The element weight to be tested.
Returns:
True if the specified object is an element weight in this list.

element

public Element element(java.lang.Object weight)
Return an element in this list that has a specified weight. If multiple elements have the specified weight, then return one of them arbitrarily. If the specified weight is null, return an unweighted element (again arbitrarily chosen if there are multiple unweighted elements).

Parameters:
weight - The specified weight.
Returns:
An element that has this weight.
Throws:
GraphWeightException - If the specified weight is not an element weight in this list or if the specified weight is null but the list does not contain any unweighted edges.

elements

public java.util.Collection elements()
Return all the elements in this list in the form of an unmodifiable collection.

Returns:
All the elements in this list.

elements

public java.util.Collection elements(java.lang.Object weight)
Return all the elements in this graph that have a specified weight. The elements are returned in the form of an unmodifiable collection. If the specified weight is null, return all the unweighted elements. If no elements have the specified weight (or if the argument is null and there are no unweighted elements), return an empty collection. Each element in the returned collection is an instance of Element.

Parameters:
weight - The specified weight.
Returns:
The elements in this graph that have the specified weight.

registerWeight

public void registerWeight(Element element)
Associate a graph element to its weight given the relevant mapping of weights to elements, and the set of unweighted elements of the same type (nodes or edges). If the element is unweighted, add it to the set of unweighted elements.

Parameters:
element - The element.

remove

public boolean remove(Element element)
Remove an element from this list if it exists in the list. This is an O(1) operation.

Parameters:
element - The element to be removed.
Returns:
True if the element was removed.

validateWeight

public boolean validateWeight(Element element,
                              java.lang.Object oldWeight)
Validate the weight of a given graph element, given the previous weight of that element.

Parameters:
element - The element.
oldWeight - The previous weight (null if the element was previously unweighted).
Returns:
True if the weight is valid.