View Javadoc

    1   /*
    2    * Created on Jun 20, 2004
    3    */
    4   package baseCode.dataStructure.graph;
    5
    6   import java.util.Iterator;
    7   import java.util.LinkedHashMap;
    8   import java.util.Map;
    9   import java.util.Set;
    10
    11  import baseCode.common.Visitable;
    12
    13  /***
    14   * <p>
    15   * Copyright (c) Columbia University
    16   *
    17   * @author Paul Pavlidis
    18   * @version $Id: AbstractGraph.java,v 1.8 2004/07/29 08:38:49 pavlidis Exp $
    19   */
    20  public abstract class AbstractGraph implements Graph {
    21     protected Map items;
    22
    23     public AbstractGraph() {
    24        items = new LinkedHashMap();
    25     }
    26
    27     /***
    28      * Create a new graph from a set of nodes. This allows us to easily make subtrees.
    29      *
    30      * @param nodes Set of AbstractGraphNodes.
    31      */
    32     public AbstractGraph( Set nodes ) {
    33        items = new LinkedHashMap();
    34        for ( Iterator it = nodes.iterator(); it.hasNext(); ) {
    35           GraphNode n = ( GraphNode ) it.next();
    36           this.addNode( n.getKey(), n.getItem() );
    37        }
    38     }
    39
    40     /***
    41      * @param node GraphNode
    42      */
    43     public void addNode( GraphNode node ) {
    44        node.setGraph( this );
    45        items.put( node.getKey(), node );
    46     }
    47
    48     /***
    49      * Retrieve a node by key. To get the contents of a node use getNodeContents(key)
    50      *
    51      * @param key Object
    52      * @see #getNodeContents(Object)
    53      * @return AbstractGraphNode referenced by the key.
    54      */
    55     public GraphNode get( Object key ) {
    56        return ( GraphNode ) items.get( key );
    57     }
    58
    59     /***
    60      * @return Map
    61      */
    62     public Map getItems() {
    63        return items;
    64     }
    65
    66     /***
    67      * Retrieve the contents of a node by key.
    68      *
    69      * @see #get
    70      * @param key Object
    71      * @return The object contained by a node, not the node itself.
    72      */
    73     public Object getNodeContents( Object key ) {
    74        return ( ( GraphNode ) items.get( key ) ).getItem();
    75     }
    76
    77     /***
    78      * @param key Object
    79      * @return true if the graph contains an item referenced by key, false otherwise.
    80      */
    81     public boolean containsKey( Object key ) {
    82        return items.containsKey( key );
    83     }
    84
    85     /***
    86      * Reset the 'visited' marks of the graph to false.
    87      */
    88     public void unmarkAll() {
    89        for ( Iterator it = items.keySet().iterator(); it.hasNext(); ) {
    90           ( ( Visitable ) it.next() ).unMark();
    91        }
    92     }
    93  }