KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > languages > parser > DG


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.languages.parser;
21
22 import java.util.*;
23
24
25 /**
26  * Directed Graph implementation.
27  *
28  * @author Jan Jancura
29  */

30 public class DG<N,E,K,V> {
31
32     
33     static <N,E,K,V> DG<N,E,K,V> createDG (N node) {
34         return new DG<N,E,K,V> (node);
35     }
36     
37     static <N,E,K,V> DG<N,E,K,V> createDG () {
38         return new DG<N,E,K,V> ();
39     }
40
41     
42     private Map<N,Node<N,E,K,V>> idToNode = new HashMap<N,Node<N,E,K,V>> ();
43     private Map<Node<N,E,K,V>,N> nodeToId = new HashMap<Node<N,E,K,V>,N> ();
44     private N start;
45     private Set<N> ends = new HashSet<N> ();
46     
47     private DG () {
48     }
49     
50     private DG (N node) {
51         start = node;
52         Node<N,E,K,V> n = new Node<N,E,K,V> ();
53         idToNode.put (node, n);
54         nodeToId.put (n, node);
55         ends.add (node);
56     }
57     
58     N getStartNode () {
59         return start;
60     }
61     
62     void setStart (N node) {
63         if (idToNode.get (node) == null) new NullPointerException JavaDoc ();
64         start = node;
65     }
66     
67     Set<N> getEnds () {
68         return Collections.<N>unmodifiableSet (ends);
69     }
70     
71     void setEnds (Set<N> ends) {
72         this.ends = new HashSet<N> (ends);
73     }
74     
75     void addEnd (N end) {
76         assert (end != null);
77         ends.add (end);
78     }
79     
80     void removeEnd (N end) {
81         ends.remove (end);
82     }
83     
84     void addNode (N node) {
85         assert (node != null);
86         if (idToNode.containsKey (node)) throw new IllegalArgumentException JavaDoc ();
87         Node<N,E,K,V> n = new Node<N,E,K,V> ();
88         idToNode.put (node, n);
89         nodeToId.put (n, node);
90     }
91     
92     void removeNode (N node) {
93         Node<N,E,K,V> n = idToNode.remove (node);
94         nodeToId.remove (n);
95     }
96     
97     boolean containsNode (N node) {
98         return idToNode.containsKey (node);
99     }
100     
101     Set<N> getNodes () {
102         return Collections.<N>unmodifiableSet (idToNode.keySet ());
103     }
104     
105     N getNode (N node, E edge) {
106         Node<N,E,K,V> s = idToNode.get (node);
107         Node<N,E,K,V> e = s.getNode (edge);
108         return nodeToId.get (e);
109     }
110     
111     void addEdge (
112         N startNode,
113         N endNode,
114         E edge
115     ) {
116         assert (startNode != null);
117         assert (endNode != null);
118         assert (edge != null);
119         Node<N,E,K,V> s = idToNode.get (startNode);
120         Node<N,E,K,V> e = idToNode.get (endNode);
121         assert (s != null);
122         assert (e != null);
123         s.addEdge (edge, e);
124     }
125     
126     Set<E> getEdges (N node) {
127         Node<N,E,K,V> n = idToNode.get (node);
128         return n.edges ();
129     }
130     
131     E getEdge (N node, E edge) {
132         Node<N,E,K,V> n = idToNode.get (node);
133         return n.getEdge (edge);
134     }
135
136     V getProperty (N node, K key) {
137         Node<N,E,K,V> n = idToNode.get (node);
138         return n.getProperty (key);
139     }
140     
141     Map<K,V> getProperties (N node) {
142         if (node == null)
143             System.out.println("!");
144         Node<N,E,K,V> n = idToNode.get (node);
145         if (n == null)
146             System.out.println(node);
147         if (n.properties == null) return Collections.<K,V>emptyMap ();
148         return Collections.<K,V>unmodifiableMap (n.properties);
149     }
150     
151     void putAllProperties (N node, Map<K,V> properties) {
152         if (properties.size () == 0) return;
153         Node<N,E,K,V> n = idToNode.get (node);
154         if (n.properties == null) n.properties = new HashMap<K,V> ();
155         n.properties.putAll (properties);
156     }
157
158     void setProperty (N node, K key, V value) {
159         Node<N,E,K,V> n = idToNode.get (node);
160         n.setProperty (key, value);
161     }
162
163     V getProperty (N node, E edge, K key) {
164         Node<N,E,K,V> n = idToNode.get (node);
165         return n.getEdgeProperty (edge, key);
166     }
167
168     Map<K,V> getProperties (N node, E edge) {
169         Node<N,E,K,V> n = idToNode.get (node);
170         if (n.idToProperties == null ||
171             n.idToProperties.get (edge) == null
172         ) return Collections.<K,V>emptyMap ();
173         return Collections.<K,V>unmodifiableMap (n.idToProperties.get (edge));
174     }
175
176     void putAllProperties (N node, E edge, Map<K,V> properties) {
177         if (properties.size () == 0) return;
178         Node<N,E,K,V> n = idToNode.get (node);
179         if (n.idToProperties == null) n.idToProperties = new HashMap<E,Map<K,V>> ();
180         if (n.idToProperties.get (edge) == null)
181             n.idToProperties.put (edge, new HashMap<K,V> ());
182         n.idToProperties.get (edge).putAll (properties);
183     }
184     
185     void setProperty (N node, E edge, K key, V value) {
186         Node<N,E,K,V> n = idToNode.get (node);
187         n.setEdgeProperty (edge, key, value);
188     }
189     
190     void changeKey (N oldNode, N newNode) {
191         Node<N,E,K,V> n = idToNode.get (oldNode);
192         idToNode.remove (oldNode);
193         idToNode.put (newNode, n);
194         nodeToId.put (n, newNode);
195     }
196     
197     public String JavaDoc toString () {
198         StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
199
200         sb.append (" start: ").append (getStartNode ()).append (" end: ");
201         Iterator<N> it = getEnds ().iterator ();
202         while (it.hasNext ()) {
203             N end = it.next ();
204             sb.append (end);
205             if (it.hasNext ()) sb.append (',');
206         }
207         sb.append ('\n');
208         
209         it = getNodes ().iterator ();
210         while (it.hasNext ()) {
211             N node = it.next ();
212             sb.append (node).append ('(');
213             Iterator<E> it2 = getEdges (node).iterator ();
214             while (it2.hasNext ()) {
215                 E edge = it2.next ();
216                 N end = getNode (node, edge);
217                 sb.append (convert (edge)).append (end);
218                 if (it2.hasNext ()) sb.append (',');
219             }
220             sb.append (')');
221             sb.append ('\n');
222         }
223         
224         it = getNodes ().iterator ();
225         while (it.hasNext ()) {
226             N node = it.next ();
227             Node<N,E,K,V> n = idToNode.get (node);
228             sb.append (" ").append (node).append (": ");
229             if (n.properties != null)
230                 sb.append (n.properties);
231             sb.append ('\n');
232             if (n.idToProperties != null) {
233                 Iterator<E> it2 = n.idToProperties.keySet ().iterator ();
234                 while (it2.hasNext ()) {
235                     E edge = it2.next ();
236                     Map<K,V> m = n.idToProperties.get (edge);
237                     sb.append (" ").append (convert (edge)).append (": ").append (m).append ('\n');
238                 }
239             }
240         }
241         return sb.toString ();
242     }
243     
244     private static Character JavaDoc NN = new Character JavaDoc ('\n');
245     private static Character JavaDoc NR = new Character JavaDoc ('\n');
246     private static Character JavaDoc NT = new Character JavaDoc ('\n');
247     private static Character JavaDoc NS = new Character JavaDoc ('\n');
248     
249     private static final Character JavaDoc STAR = new Character JavaDoc ((char)0);
250     
251     private String JavaDoc convert (E edge) {
252         if (STAR.equals (edge)) return ".";
253         if (NN.equals (edge)) return "\\n";
254         if (NR.equals (edge)) return "\\r";
255         if (NT.equals (edge)) return "\\t";
256         if (NS.equals (edge)) return "' '";
257         return edge.toString ();
258     }
259     
260     private static class Node<N,E,K,V> {
261
262         private Map<K,V> properties;
263         private Map<E,Map<K,V>> idToProperties;
264         private Map<E,Node<N,E,K,V>> edgeToNode;
265         private Map<E,E> edges;
266
267
268         V getProperty (K key) {
269             if (properties == null) return null;
270             return properties.get (key);
271         }
272         
273         void setProperty (K key, V value) {
274             if (properties == null) properties = new HashMap<K,V> ();
275             properties.put (key, value);
276         }
277         
278         Node<N,E,K,V> getNode (E edge) {
279             if (edgeToNode == null) return null;
280             return edgeToNode.get (edge);
281         }
282
283         void addEdge (E edge, Node<N,E,K,V> node) {
284             if (edgeToNode == null) edgeToNode = new HashMap<E,Node<N,E,K,V>> ();
285             if (edges == null) edges = new HashMap<E,E> ();
286             edgeToNode.put (edge, node);
287             edges.put (edge, edge);
288         }
289
290         E getEdge (E edge) {
291             if (edges == null) return null;
292             return edges.get (edge);
293         }
294
295         Set<E> edges () {
296             if (edgeToNode == null) return Collections.<E>emptySet ();
297             return edgeToNode.keySet ();
298         }
299         
300         V getEdgeProperty (E edge, K key) {
301             if (idToProperties == null) return null;
302             if (idToProperties.get (edge) == null) return null;
303             return idToProperties.get (edge).get (key);
304         }
305
306         void setEdgeProperty (E edge, K key, V value) {
307             if (idToProperties == null) idToProperties = new HashMap<E,Map<K,V>> ();
308             Map<K,V> m = idToProperties.get (edge);
309             if (m == null) {
310                 m = new HashMap<K,V> ();
311                 idToProperties.put (edge, m);
312             }
313             m.put (key, value);
314         }
315     }
316 }
317
Popular Tags