KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > graph > Vertex


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.util.graph;
23
24 import java.util.ArrayList JavaDoc;
25
26 /**
27  * @author Scott.Stark@jboss.org
28  * @version $Revision$
29  */

30 public class Vertex<T>
31 {
32    private ArrayList JavaDoc<Edge<T>> incomingEdges;
33    private ArrayList JavaDoc<Edge<T>> outgoingEdges;
34    private String JavaDoc name;
35    private boolean mark;
36    private int markState;
37    private T data;
38
39    /**
40     * Calls this(null, null).
41     */

42    public Vertex()
43    {
44       this(null, null);
45    }
46    /**
47     * Create a vertex with the given name
48     * @param n
49     */

50    public Vertex(String JavaDoc n)
51    {
52       this(null, null);
53    }
54    public Vertex(String JavaDoc n, T data)
55    {
56       incomingEdges = new ArrayList JavaDoc<Edge<T>>();
57       outgoingEdges = new ArrayList JavaDoc<Edge<T>>();
58       name = new String JavaDoc(n);
59       mark = false;
60       this.data = data;
61    }
62
63    public String JavaDoc getName()
64    {
65       return name;
66    }
67
68    /**
69     * @return Returns the data.
70     */

71    public T getData()
72    {
73       return this.data;
74    }
75    /**
76     * @param data The data to set.
77     */

78    public void setData(T data)
79    {
80       this.data = data;
81    }
82
83    /**
84     *
85     * @param e
86     * @return true if the edge was added, false otherwise
87     */

88    public boolean addEdge(Edge<T> e)
89    {
90       if (e.getFrom() == this)
91          outgoingEdges.add(e);
92       else if (e.getTo() == this)
93          incomingEdges.add(e);
94       else
95          return false;
96       return true;
97    }
98    
99    /**
100     *
101     * @param e
102     * @return
103     */

104    public boolean hasEdge(Edge<T> e)
105    {
106       if (e.getFrom() == this)
107          return incomingEdges.contains(e);
108       else if (e.getTo() == this)
109          return outgoingEdges.contains(e);
110       else
111          return false;
112    }
113    
114    // Remove an edge from this vertex
115
public boolean remove(Edge<T> e)
116    {
117       if (e.getFrom() == this)
118          incomingEdges.remove(e);
119       else if (e.getTo() == this)
120          outgoingEdges.remove(e);
121       else
122          return false;
123       return true;
124    }
125    
126    public int getIncomingEdgeCount()
127    {
128       return incomingEdges.size();
129    }
130    
131    public Edge<T> getIncomingEdge(int i)
132    {
133       Edge<T> e = incomingEdges.get(i);
134       return e;
135    }
136
137    public int getOutgoingEdgeCount()
138    {
139       return outgoingEdges.size();
140    }
141    public Edge<T> getOutgoingEdge(int i)
142    {
143       Edge<T> e = outgoingEdges.get(i);
144       return e;
145    }
146    
147    // Do we have an edge that goes to dest?
148
public Edge<T> findEdge(Vertex<T> dest)
149    {
150       for (int i = 0; i < incomingEdges.size(); i++)
151       {
152          Edge<T> e = incomingEdges.get(i);
153          if (e.getTo() == dest)
154             return e;
155       }
156       return null;
157    }
158    
159    // Do we have the edge e? Only looks at sucessors
160
public Edge<T> findEdge(Edge<T> e)
161    {
162       if (incomingEdges.contains(e))
163          return e;
164       else
165          return null;
166    }
167
168    /**
169     * What is the cost to this vertex.
170     * Return Integer.MAX_VALUE if we have no edge to dest
171     * @param dest
172     * @return
173     */

174    public int cost(Vertex<T> dest)
175    {
176       if (dest == this)
177          return 0;
178          
179       Edge<T> e = findEdge(dest);
180       if (e != null)
181          return e.getCost();
182       else
183          return Integer.MAX_VALUE;
184    }
185       
186    // Do we have an edge to dest?
187
public boolean hasEdge(Vertex<T> dest)
188    {
189       return (findEdge(dest) != null);
190    }
191    
192    // Have we been here before?
193
public boolean visited()
194    {
195       return mark;
196    }
197    
198    public void mark()
199    {
200       mark = true;
201    }
202    public void setMarkState(int state)
203    {
204       markState = state;
205    }
206    public int getMarkState()
207    {
208       return markState;
209    }
210
211    public void visit()
212    {
213       mark();
214    }
215    
216    // Clear the mark
217
public void clearMark()
218    {
219       mark = false;
220    }
221    
222    public String JavaDoc toString()
223    {
224       StringBuffer JavaDoc tmp = new StringBuffer JavaDoc("Vertex(");
225       tmp.append(name);
226       tmp.append("), in:[");
227       for (int i = 0; i < incomingEdges.size(); i++)
228       {
229          Edge<T> e = incomingEdges.get(i);
230          if( i > 0 )
231             tmp.append(',');
232          tmp.append('{');
233          tmp.append(e.getFrom().name);
234          tmp.append(',');
235          tmp.append(e.getCost());
236          tmp.append('}');
237       }
238       tmp.append("], out:[");
239       for (int i = 0; i < outgoingEdges.size(); i++)
240       {
241          Edge<T> e = outgoingEdges.get(i);
242          if( i > 0 )
243             tmp.append(',');
244          tmp.append('{');
245          tmp.append(e.getTo().name);
246          tmp.append(',');
247          tmp.append(e.getCost());
248          tmp.append('}');
249       }
250       tmp.append(']');
251       return tmp.toString();
252    }
253 }
254
Popular Tags