edu.byu.ece.graph
Class NodeEdgeMap

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by java.util.HashMap<K,V>
          extended by java.util.LinkedHashMap
              extended by edu.byu.ece.graph.NodeEdgeMap
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.util.Map

public class NodeEdgeMap
extends java.util.LinkedHashMap
implements java.lang.Cloneable

Represents a Map between node objects within a DirectedGraph object and a Collection of Edge objects. The node objects can be a class of any type while the edge objects within the Collections must be an Edge object.

The Map used between nodes and edges will correspond to either the source nodes of the edge or the sink nodes of the edge. If it uses the source node, this class is "source" mapped. This means that the key of the Map is a source node for all Edge objects in the associated Collection.

If it uses the sink node, this class is "sink" mapped. This means that the key of the Map is a sink node for all Edge objects in the associated Collection.

The mapping must be determined during construction and cannot be changed.

WARNING: If the hash code of a key (node) to this Map changes during run time, the Map will fail to operate correctly. Make sure that the hash code of any node Object cannot change over time.

See Also:
Serialized Form

Field Summary
protected  boolean _sourceMapped
           
 
Constructor Summary
NodeEdgeMap(boolean sourceMapped)
          Construct a new object.
NodeEdgeMap(int size, boolean sourceMapped)
          Construct a new object with an initial capacity.
NodeEdgeMap(NodeEdgeMap map)
          Construct a new object based on the parameter (copy constructor).
 
Method Summary
 boolean addEdge(Edge edge)
          Add an edge to the Map.
 java.lang.Object clone()
          Clone this object.
 java.util.Collection<Edge> getEdges()
          Returns all of the Edges of this Map.
 java.util.Collection getEdges(java.lang.Object o)
          Helper function that returns a non-null Collection object that is the "value" of the given object in the map.
 java.util.Collection getNodes()
          Return all of the Nodes in this Map.
 NodeEdgeMap invert()
          Create a new NodeEdgeMap such that the edges are all inverted and the sourceMapped tag is flipped.
 boolean removeEdge(Edge edge)
          Remove the given edge from the Map.
 void retainEdgesForNodes(java.util.Collection nodes)
          Removes all Edges from the given Map (node Object -> Collection of Edges) that do not attach to any of the node Objects passed in.
 void trimToSize()
           
 
Methods inherited from class java.util.LinkedHashMap
clear, containsValue, get, removeEldestEntry
 
Methods inherited from class java.util.HashMap
containsKey, entrySet, isEmpty, keySet, put, putAll, remove, size, values
 
Methods inherited from class java.util.AbstractMap
equals, hashCode, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
containsKey, entrySet, equals, hashCode, isEmpty, keySet, put, putAll, remove, size, values
 

Field Detail

_sourceMapped

protected boolean _sourceMapped
Constructor Detail

NodeEdgeMap

public NodeEdgeMap(int size,
                   boolean sourceMapped)
Construct a new object with an initial capacity.

Parameters:
size - The initial capacity of the Map
sourceMapped - The mapping mode of the object. true indicates a "source" mapped mapping while false indicates a "sink" mapped mapping.

NodeEdgeMap

public NodeEdgeMap(boolean sourceMapped)
Construct a new object.

Parameters:
sourceMapped - The mapping mode of the object. true indicates a "source" mapped mapping while false indicates a "sink" mapped mapping.

NodeEdgeMap

public NodeEdgeMap(NodeEdgeMap map)
Construct a new object based on the parameter (copy constructor).

Parameters:
map - The NodeEdgeMap to copy
Method Detail

clone

public java.lang.Object clone()
Clone this object. Overrides Object.clone()

Overrides:
clone in class java.util.HashMap

addEdge

public boolean addEdge(Edge edge)
Add an edge to the Map. This method map this Edge to the source node object if this object is "source" mapped or to the sink node object if this object is "sink" mapped.

Parameters:
edge - The edge to add.

removeEdge

public boolean removeEdge(Edge edge)
Remove the given edge from the Map. Returns a true if the Edge was found in the map and false otherwise.

Parameters:
edge - The edge to remove
Returns:
true if the edge was removed from the Map and false if the edge was not removed from the Map.

getEdges

public java.util.Collection<Edge> getEdges()
Returns all of the Edges of this Map. The returned Collection is guaranteed not to be null.

Returns:
A Collection of the Edges in this map. If the Map is empty, this method will return an empty but non-null Collection.

getNodes

public java.util.Collection getNodes()
Return all of the Nodes in this Map. This is the same as the keySet of the Map.

Returns:
A Collection of all Nodes in this Map.

retainEdgesForNodes

public void retainEdgesForNodes(java.util.Collection nodes)
Removes all Edges from the given Map (node Object -> Collection of Edges) that do not attach to any of the node Objects passed in.

If the parameter checkSource is true, only the source of each Edge is examined, if false, only the sink is examined.


invert

public NodeEdgeMap invert()
Create a new NodeEdgeMap such that the edges are all inverted and the sourceMapped tag is flipped.

TODO: get rid of this function?

Returns:
The NodeEdgeMap with all the edges inverted and the opposite sourceMapped property.

getEdges

public java.util.Collection getEdges(java.lang.Object o)
Helper function that returns a non-null Collection object that is the "value" of the given object in the map. If the map returns a null, this method will return an empty but non-null Collection.

Parameters:
o - The given object
Returns:
A Collection that is the "value" of the given object in the map. If the map returns a null, this method will return an empty but non-null Collection.

trimToSize

public void trimToSize()