01: package net.sf.crispy.util;
02:
03: import java.util.HashMap;
04: import java.util.Hashtable;
05: import java.util.Iterator;
06: import java.util.Map;
07:
08: public class Graph { // extends Hashtable {
09:
10: private static final long serialVersionUID = 7523758252776765521L;
11: private Map vertexs = new HashMap();
12: private Map edges = new HashMap();
13:
14: /** All edges, map key is the from vertex */
15: private Map fromEdge = new Hashtable();
16: /** All edges, map key is the to vertex */
17: private Map toEdge = new Hashtable();
18:
19: public Object put(Object pvKey, Object pvValue) {
20: Edge lvEdge = (Edge) pvKey;
21: fromEdge.put(lvEdge.getFrom(), lvEdge);
22: toEdge.put(lvEdge.getTo(), lvEdge);
23: // return super.put(pvKey, pvValue);
24: return edges.put(pvKey, pvValue);
25: }
26:
27: public void addVertex(Object pvVertexObj) {
28: vertexs.put(pvVertexObj, Integer.valueOf("1"));
29: }
30:
31: public boolean getVertexCount(Object pvVertexObj) {
32: if (pvVertexObj == null) {
33: return false;
34: }
35: return (vertexs.get(pvVertexObj) == null ? false : true);
36: }
37:
38: public Edge isToContains(Object pvObject) {
39: if (pvObject == null) {
40: return null;
41: }
42: return (Edge) toEdge.get(pvObject);
43: }
44:
45: public Edge isFromContains(Object pvObject) {
46: if (pvObject == null) {
47: return null;
48: }
49:
50: // System.out.println("- " + pvObject + " " + pvObject.getClass().getName());
51: // Iterator it = fromEdge.keySet().iterator();
52: // while (it.hasNext()) {
53: // System.out.println("" + it.next());
54: // }
55: Edge lvFindEdgeFrom = (Edge) fromEdge.get(pvObject);
56: // System.out.println("--- > " + lvFindEdgeFrom);
57:
58: return lvFindEdgeFrom;
59: }
60:
61: public Edge getProblemEdge(Object pvObject) {
62: if (pvObject == null) {
63: return null;
64: }
65: // Enumeration lvEnumeration = this.keys();
66: Iterator it = edges.keySet().iterator();
67: // while (lvEnumeration.hasMoreElements()) {
68: while (it.hasNext()) {
69: // Edge lvEdge = (Edge) lvEnumeration.nextElement();
70: Edge lvEdge = (Edge) it.next();
71: if ((lvEdge.getTo() != null)
72: && (lvEdge.getTo().equals(pvObject))
73: && (lvEdge.getMethodName() != null)) {
74: return lvEdge;
75: }
76: }
77: return null;
78: }
79:
80: public synchronized boolean containsKey(Object pvKey) {
81: // boolean b = super.contains(pvKey);
82: boolean b = edges.containsKey(pvKey);
83: return b;
84: }
85:
86: }
|