Source code: com/phoenixst/plexus/SimpleEdge.java
1 /*
2 * $Id: SimpleEdge.java,v 1.1 2003/11/04 22:33:09 rconner Exp $
3 *
4 * Copyright (C) 1994-2003 by Phoenix Software Technologists,
5 * Inc. and others. All rights reserved.
6 *
7 * THIS PROGRAM AND DOCUMENTATION IS PROVIDED UNDER THE TERMS OF THE
8 * COMMON PUBLIC LICENSE ("AGREEMENT") WHICH ACCOMPANIES IT. ANY
9 * USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM CONSTITUTES
10 * RECIPIENT'S ACCEPTANCE OF THE AGREEMENT.
11 *
12 * The license text can also be found at
13 * http://opensource.org/licenses/cpl.php
14 */
15
16 package com.phoenixst.plexus;
17
18 import com.phoenixst.plexus.*;
19
20
21 /**
22 * A simple <code>Edge</code> implementation which contains no
23 * user-defined object. Because of how {@link #equals(Object)
24 * equals( Object )} is defined, instances of this class may only be
25 * used by simple graphs.
26 *
27 * @version $Revision: 1.1 $
28 * @author Ray A. Conner
29 *
30 * @since 1.0
31 */
32 public class SimpleEdge
33 implements Graph.Edge,
34 java.io.Serializable
35 {
36
37 /**
38 * The graph which created this <code>Edge</code>.
39 */
40 private Graph graph;
41
42 /**
43 * The tail of this <code>Edge</code>.
44 */
45 private Object tail;
46
47 /**
48 * The head of this <code>Edge</code>.
49 */
50 private Object head;
51
52 /**
53 * Whether or not this <code>Edge</code> is directed.
54 */
55 private boolean isDirected;
56
57
58 ////////////////////////////////////////
59 // Constructor
60 ////////////////////////////////////////
61
62
63 /**
64 * Creates a new <code>SimpleEdge</code>.
65 */
66 public SimpleEdge( Graph graph,
67 Object tail, Object head,
68 boolean isDirected )
69 {
70 this.graph = graph;
71 this.tail = tail;
72 this.head = head;
73 this.isDirected = isDirected;
74 }
75
76
77 ////////////////////////////////////////
78 // Edge methods
79 ////////////////////////////////////////
80
81
82 public boolean isDirected()
83 {
84 return isDirected;
85 }
86
87
88 public Object getUserObject()
89 {
90 return null;
91 }
92
93
94 public void setUserObject( Object object )
95 {
96 throw new UnsupportedOperationException();
97 }
98
99
100 public Object getTail()
101 {
102 return tail;
103 }
104
105
106 public Object getHead()
107 {
108 return head;
109 }
110
111
112 public Object getOtherEndpoint( Object node )
113 {
114 if( node == null ) {
115 if( tail == null ) {
116 return head;
117 } else if( head == null ) {
118 return tail;
119 }
120 } else if( node.equals( tail ) ) {
121 return head;
122 } else if( node.equals( head ) ) {
123 return tail;
124 }
125 throw new IllegalArgumentException( "Edge is not incident on the node: " + node );
126 }
127
128
129 ////////////////////////////////////////
130 // Other methods
131 ////////////////////////////////////////
132
133
134 protected static final boolean equals( Object a, Object b )
135 {
136 return (a == null) ? (b == null) : a.equals( b );
137 }
138
139
140 /**
141 * Two <code>SimpleEdges</code> are equal if they have the same
142 * directedness, are from the exact same graph (using
143 * <code>==</code>), and have equal endpoints.
144 */
145 public boolean equals( Object object )
146 {
147 return (object instanceof SimpleEdge)
148 && equals( (SimpleEdge) object );
149 }
150
151
152 /**
153 * Two <code>SimpleEdges</code> are equal if they have the same
154 * directedness, are from the exact same graph (using
155 * <code>==</code>), and have equal endpoints.
156 */
157 public boolean equals( SimpleEdge edge )
158 {
159 if( isDirected ) {
160 return edge.isDirected
161 && graph == edge.graph
162 && equals( tail, edge.tail )
163 && equals( head, edge.head );
164 } else {
165 return !edge.isDirected
166 && graph == edge.graph
167 && ( (equals( tail, edge.tail ) && equals( head, edge.head ))
168 || (equals( tail, edge.head ) && equals( head, edge.tail )) );
169 }
170 }
171
172
173 public int hashCode()
174 {
175 return ((tail == null) ? 0 : tail.hashCode())
176 ^ ((head == null) ? 0 : head.hashCode());
177 }
178
179
180 public String toString()
181 {
182 StringBuffer s = new StringBuffer();
183 s.append( tail );
184 s.append( isDirected ? " -> " : " -- " );
185 s.append( head );
186 return s.toString();
187 }
188
189 }