Source code: com/phoenixst/plexus/DefaultEdge.java
1 /*
2 * $Id: DefaultEdge.java,v 1.6 2003/11/06 21:29:40 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
19 /**
20 * A default {@link Graph.Edge} implementation in which the
21 * user-defined object must be <code>null</code>. The {@link #equals
22 * equals()} method is inherited from <code>Object</code> and uses
23 * reference equality. This class should only be used by
24 * <code>Graphs</code> which create edges once and store them.
25 *
26 * @version $Revision: 1.6 $
27 * @author Ray A. Conner
28 *
29 * @since 1.0
30 */
31 public class DefaultEdge
32 implements Graph.Edge,
33 java.io.Serializable
34 {
35
36 /**
37 * The tail of this <code>Edge</code>.
38 */
39 protected Object tail;
40
41 /**
42 * The head of this <code>Edge</code>.
43 */
44 protected Object head;
45
46 /**
47 * Whether or not this <code>Edge</code> is directed.
48 */
49 protected boolean isDirected;
50
51
52 ////////////////////////////////////////
53 // Constructor
54 ////////////////////////////////////////
55
56
57 /**
58 * Creates a new <code>DefaultEdge</code>.
59 */
60 public DefaultEdge( Object tail, Object head, boolean isDirected )
61 {
62 this.tail = tail;
63 this.head = head;
64 this.isDirected = isDirected;
65 }
66
67
68 ////////////////////////////////////////
69 // Edge methods
70 ////////////////////////////////////////
71
72
73 public boolean isDirected()
74 {
75 return isDirected;
76 }
77
78
79 public Object getUserObject()
80 {
81 return null;
82 }
83
84
85 public void setUserObject( Object object )
86 {
87 throw new UnsupportedOperationException();
88 }
89
90
91 public Object getTail()
92 {
93 return tail;
94 }
95
96
97 public Object getHead()
98 {
99 return head;
100 }
101
102
103 public Object getOtherEndpoint( Object node )
104 {
105 if( equals( tail, node ) ) {
106 return head;
107 } else if( equals( head, node ) ) {
108 return tail;
109 } else {
110 throw new IllegalArgumentException( "Edge is not incident on the node: " + node );
111 }
112 }
113
114
115 ////////////////////////////////////////
116 // Other methods
117 ////////////////////////////////////////
118
119
120 protected static final boolean equals( Object a, Object b )
121 {
122 return (a == null) ? (b == null) : a.equals( b );
123 }
124
125
126 public String toString()
127 {
128 StringBuffer s = new StringBuffer();
129 s.append( "(" );
130 s.append( getTail() );
131 s.append( isDirected() ? ") -> (" : ") -- (" );
132 s.append( getHead() );
133 s.append( ")" );
134 return s.toString();
135 }
136
137 }