KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > classycle > graph > Vertex


1 /*
2  * Copyright (c) 2003-2006, Franz-Josef Elmer, All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * - Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * - Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
14  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
15  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
23  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */

25 package classycle.graph;
26
27 import java.util.Vector JavaDoc;
28
29 /**
30  * The base class for any type of vertex in a directed graph.
31  * <p>
32  * A <tt>Vertex</tt> holds an {@link Attributes} object which encapsulates
33  * all properties of the vertex which are not necessary to know for
34  * parsing a graph in a {@link GraphProcessor}. Only the visited flag will
35  * be manipulated during parsing.
36  * <p>
37  * A <tt>Vertex</tt> knows the head and tail vertices of all its outgoing
38  * and incoming arcs. When a head vertex is added by the method
39  * {@link #addOutgoingArcTo} also the corresponding incoming arc is built
40  * in the head vertex. The same is true the other way around. Note,
41  * that multi-arcs are not possible. That is, adding an already added
42  * head/tail vertex again as a head/tail vertex will be ignored.
43  *
44  * @author Franz-Josef Elmer
45  */

46 public class Vertex implements Comparable JavaDoc
47 {
48   private final Vector JavaDoc _heads = new Vector JavaDoc();
49   private final Vector JavaDoc _tails = new Vector JavaDoc();
50   private final Attributes _attributes;
51   private boolean _visited;
52
53   /** Create a new instance for the specified attributes. */
54   public Vertex(Attributes attributes)
55   {
56     _attributes = attributes;
57   }
58
59   /** Returns the attributes. */
60   public Attributes getAttributes()
61   {
62     return _attributes;
63   }
64
65   /**
66    * Returns the number of outgoing arcs. This is equivalent to the number
67    * of head vertices.
68    */

69   public int getNumberOfOutgoingArcs()
70   {
71     return _heads.size();
72   }
73
74   /** Returns the head vertex of the specified outgoing arc. */
75   public Vertex getHeadVertex(int index)
76   {
77     return (Vertex) _heads.elementAt(index);
78   }
79
80   /**
81    * Adds an outgoing arc to the specified vertex. Also calls
82    * {@link #addIncomingArcTo} for <tt>headVertex</tt> with <tt>this</tt> as
83    * the argument. Does nothing if <tt>headVertex</tt> is the head
84    * vertex of an already existing outgoing arc.
85    * @param headVertex Head vertex to be added to establish a new outgoing arc.
86    * <tt>Null</tt> is not allowed.
87    */

88   public void addOutgoingArcTo(Vertex headVertex)
89   {
90     if (!_heads.contains(headVertex))
91     {
92       _heads.addElement(headVertex);
93       headVertex.addIncomingArcTo(this);
94     }
95   }
96
97   /**
98    * Returns the number of incoming arcs. This is equivalent to the number
99    * of tail vertices.
100    */

101   public int getNumberOfIncomingArcs()
102   {
103     return _tails.size();
104   }
105
106   /** Returns the tail vertex of the specified outgoing arc. */
107   public Vertex getTailVertex(int index)
108   {
109     return (Vertex) _tails.elementAt(index);
110   }
111
112   /**
113    * Adds an incoming arc to the specified vertex. Also calls
114    * {@link #addOutgoingArcTo} for <tt>tailVertex</tt> with <tt>this</tt> as
115    * the argument. Does nothing if <tt>tailVertex</tt> is the
116    * tail vertex of an already existing incoming arc.
117    * @param tailVertex Tail vertex to be added to establish a new incoming arc.
118    * <tt>Null</tt> is not allowed.
119    */

120   public void addIncomingArcTo(Vertex tailVertex)
121   {
122     if (!_tails.contains(tailVertex))
123     {
124       _tails.addElement(tailVertex);
125       tailVertex.addOutgoingArcTo(this);
126     }
127   }
128
129   /** Reset this vertex. That is, the visited flag is set to <tt>false</tt>. */
130   public void reset()
131   {
132     _visited = false;
133   }
134
135   /**
136    * Marks this instance as visited.
137    * That is, the visited flag becomes <tt>true</tt>.
138    */

139   public void visit()
140   {
141     _visited = true;
142   }
143
144   /** Returns the visited flag. */
145   public boolean isVisited()
146   {
147     return _visited;
148   }
149
150   /**
151    * Returns <tt>toString()</tt> of the attributes and the number of
152    * incoming and outgoing arcs.
153    */

154   public String JavaDoc toString()
155   {
156     StringBuffer JavaDoc result = new StringBuffer JavaDoc();
157     result.append(getAttributes() == null ? super.toString()
158                                           : getAttributes().toString())
159           .append(": ").append(getNumberOfIncomingArcs())
160           .append(" incoming arc(s), ").append(getNumberOfOutgoingArcs())
161           .append(" outgoing arc(s).");
162     return new String JavaDoc(result);
163   }
164
165   public int compareTo(Object JavaDoc object)
166   {
167     int result = 1;
168     if (object instanceof Vertex && _attributes != null)
169     {
170       result = _attributes.compareTo(((Vertex) object)._attributes);
171     }
172     return result;
173   }
174
175 } //class
Popular Tags