Source code: com/port80/graph/impl/Edge.java
1 //
2 // Copyright(c) 2002, Chris Leung
3 //
4
5 package com.port80.graph.impl;
6
7 import java.util.ArrayList;
8 import java.util.HashSet;
9 import java.util.Iterator;
10 import java.util.List;
11
12 import com.port80.graph.IArrow;
13 import com.port80.graph.IEdge;
14 import com.port80.graph.IGraph;
15 import com.port80.graph.IVertex;
16 import com.port80.graph.IVertexPort;
17 import com.port80.util.Debug;
18
19 /** Graph edge interface.
20 *
21 * @see com.port80.graph.IEdge
22 */
23 public class Edge extends GraphElement implements IEdge {
24
25 // Static fields ///////////////////////////////////////////////////////
26 //
27
28 private static final String NAME = "Edge";
29 private static final String PACKAGENAME = "com.port80.graph.impl";
30 private static final String CLASSNAME = PACKAGENAME + "." + NAME;
31 private static final int VERSION = 0x0001;
32 private static final String VERSIONNAME = "0.1";
33 static {
34 Debug.add(CLASSNAME);
35 }
36 private static boolean DEBUG = false;
37 private static int anonymousCount = 0;
38
39 // Instance fields /////////////////////////////////////////////////////
40 //
41
42 protected IGraph parent;
43 protected IVertex head;
44 protected IVertex tail;
45 protected IVertexPort headPort;
46 protected IVertexPort tailPort;
47
48 // Constructors ////////////////////////////////////////////////////////
49 //
50
51 /**
52 * An edge is typically created through a graph.newEdge() method
53 * instead of calling this constructor directly.
54 */
55 public Edge(IVertex tail, IVertex head, String name, Object data, IGraph parent) {
56 super(parent == null ? null : parent.getEdgeAttrTable());
57 this.head = head;
58 this.tail = tail;
59 if (name == null)
60 name = tail.getName() + "->" + head.getName() + "#" + anonymousCount++;
61 else
62 name += "#" + anonymousCount++;
63 this.fName = name;
64 this.fData = data;
65 }
66
67 /*
68 public Edge(IVertex head,IVertex tail,String name) {
69 this.head=head;
70 this.tail=tail;
71 this.name=name;
72 }
73 */
74
75 // Instance methods ////////////////////////////////////////////////////
76 //
77
78 public String toString() {
79 return getName();
80 }
81
82 // IEdge interface /////////////////////////////////////////////////////
83 //
84
85 public IVertex getHead() {
86 return head;
87 }
88 public IVertex getTail() {
89 return tail;
90 }
91 public IVertexPort getHeadPort() {
92 return headPort;
93 }
94 public IVertexPort getTailPort() {
95 return tailPort;
96 }
97
98 public IVertex getOpposite(IVertex v) {
99 if (head == v)
100 return head;
101 if (tail == v)
102 return tail;
103 return null;
104 }
105
106 public double getArrowSize() {
107 return getAttrDouble("arrowsize");
108 }
109 public IArrow getTailArrow() {
110 return (Arrow) getAttr("arrowtail");
111 }
112 public IArrow getHeadArrow() {
113 return (Arrow) getAttr("arrowhead");
114 }
115
116 /**
117 * @return The all edges in the reverse direction if one exists.
118 * If 'ret' is null, and no edge is found, null is returned.
119 */
120 public List findReverseEdges(List ret) {
121 IEdge[] outs = head.outs();
122 for (int i = 0; i < outs.length; ++i) {
123 if (outs[i].getHead() == tail) {
124 if (ret == null)
125 ret = new ArrayList();
126 ret.add(outs[i]);
127 }
128 }
129 return ret;
130 }
131
132 public void clearLayout() {
133 for (Iterator it = new HashSet(attrKeySet()).iterator(); it.hasNext();) {
134 String attr = (String) it.next();
135 if (attr.startsWith("-") || attr.equals("pos"))
136 removeAttr(attr);
137 }
138 }
139
140 // IGraphElement interface //////////////////////////////////////////////
141 //
142 public String getElementTypeName() {
143 return NAME;
144 }
145
146 ////////////////////////////////////////////////////////////////////////
147
148 // public int hashCode() {
149 // return fName.hashCode();
150 // }
151 //
152 // public boolean equals(Object e) {
153 // if (!(e instanceof Edge))
154 // return false;
155 // Edge ee = (Edge) e;
156 // if (parent == null && ee.parent != null)
157 // return false;
158 // if (parent != null && !parent.equals(ee.parent))
159 // return false;
160 // return fName.equals(ee.fName);
161 // }
162
163 // Comparable interface /////////////////////////////////////////////////
164 // public int compareTo(Object e) {
165 // int ret = fName.compareTo(((IEdge) e).getName());
166 // return ret;
167 // }
168
169 ////////////////////////////////////////////////////////////////////////
170
171 }