KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > imageio > spi > DigraphNode


1 /*
2  * @(#)DigraphNode.java 1.7 03/01/23
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.imageio.spi;
9
10 import java.io.Serializable JavaDoc;
11 import java.util.HashSet JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.Set JavaDoc;
14
15 /**
16  * A node in a directed graph. In addition to an arbitrary
17  * <code>Object</code> containing user data associated with the node,
18  * each node maintains a <code>Set</code>s of nodes which are pointed
19  * to by the current node (available from <code>getOutNodes</code>).
20  * The in-degree of the node (that is, number of nodes that point to
21  * the current node) may be queried.
22  *
23  * @version 0.5
24  */

25 class DigraphNode implements Cloneable JavaDoc, Serializable JavaDoc {
26
27     /** The data associated with this node. */
28     protected Object JavaDoc data;
29
30     /**
31      * A <code>Set</code> of neighboring nodes pointed to by this
32      * node.
33      */

34     protected Set JavaDoc outNodes = new HashSet JavaDoc();
35     
36     /** The in-degree of the node. */
37     protected int inDegree = 0;
38
39     /**
40      * A <code>Set</code> of neighboring nodes that point to this
41      * node.
42      */

43     private Set JavaDoc inNodes = new HashSet JavaDoc();
44
45     public DigraphNode(Object JavaDoc data) {
46         this.data = data;
47     }
48     
49     /** Returns the <code>Object</code> referenced by this node. */
50     public Object JavaDoc getData() {
51         return data;
52     }
53     
54     /**
55      * Returns an <code>Iterator</code> containing the nodes pointed
56      * to by this node.
57      */

58     public Iterator JavaDoc getOutNodes() {
59         return outNodes.iterator();
60     }
61
62     /**
63      * Adds a directed edge to the graph. The outNodes list of this
64      * node is updated and the in-degree of the other node is incremented.
65      *
66      * @param node a <code>DigraphNode</code>.
67      *
68      * @return <code>true</code> if the node was not previously the
69      * target of an edge.
70      */

71     public boolean addEdge(DigraphNode JavaDoc node) {
72         if (outNodes.contains(node)) {
73             return false;
74         }
75
76         outNodes.add(node);
77         node.inNodes.add(this);
78         node.incrementInDegree();
79         return true;
80     }
81
82     /**
83      * Returns <code>true</code> if an edge exists between this node
84      * and the given node.
85      *
86      * @param node a <code>DigraphNode</code>.
87      *
88      * @return <code>true</code> if the node is the target of an edge.
89      */

90     public boolean hasEdge(DigraphNode JavaDoc node) {
91         return outNodes.contains(node);
92     }
93     
94     /**
95      * Removes a directed edge from the graph. The outNodes list of this
96      * node is updated and the in-degree of the other node is decremented.
97      *
98      * @return <code>true</code> if the node was previously the target
99      * of an edge.
100      */

101     public boolean removeEdge(DigraphNode JavaDoc node) {
102         if (!outNodes.contains(node)) {
103             return false;
104         }
105
106         outNodes.remove(node);
107         node.inNodes.remove(this);
108         node.decrementInDegree();
109         return true;
110     }
111
112     /**
113      * Removes this node from the graph, updating neighboring nodes
114      * appropriately.
115      */

116     public void dispose() {
117         Object JavaDoc[] inNodesArray = inNodes.toArray();
118         for(int i=0; i<inNodesArray.length; i++) {
119             DigraphNode JavaDoc node = (DigraphNode JavaDoc) inNodesArray[i];
120             node.removeEdge(this);
121         }
122
123         Object JavaDoc[] outNodesArray = outNodes.toArray();
124         for(int i=0; i<outNodesArray.length; i++) {
125             DigraphNode JavaDoc node = (DigraphNode JavaDoc) outNodesArray[i];
126             removeEdge(node);
127         }
128     }
129     
130     /** Returns the in-degree of this node. */
131     public int getInDegree() {
132         return inDegree;
133     }
134
135     /** Increments the in-degree of this node. */
136     private void incrementInDegree() {
137         ++inDegree;
138     }
139
140     /** Decrements the in-degree of this node. */
141     private void decrementInDegree() {
142         --inDegree;
143     }
144 }
145
Popular Tags