KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > graph > AbstractVertex


1 /*
2  * Generic graph library
3  * Copyright (C) 2000,2003,2004 University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package edu.umd.cs.findbugs.graph;
21
22 /**
23  * GraphVertex implementation for use with AbstractGraph.
24  *
25  * @see GraphVertex
26  * @see AbstractGraph
27  * @see AbstractEdge
28  * @author David Hovemeyer
29  */

30 public class AbstractVertex <
31         EdgeType extends AbstractEdge<EdgeType, ActualVertexType>,
32         ActualVertexType extends AbstractVertex<EdgeType, ActualVertexType>
33         > implements GraphVertex<ActualVertexType> {
34
35     private int id;
36     private int label;
37     EdgeType firstIncomingEdge, lastIncomingEdge;
38     EdgeType firstOutgoingEdge, lastOutgoingEdge;
39
40     void setId(int id) {
41         this.id = id;
42     }
43
44     int getId() {
45         return id;
46     }
47
48     public int getLabel() {
49         return label;
50     }
51
52     public void setLabel(int label) {
53         this.label = label;
54     }
55
56     public int hashCode() {
57         return id;
58     }
59     public boolean equals(Object JavaDoc o) {
60         if (o instanceof AbstractVertex)
61             return id == ((AbstractVertex)o).id;
62         return false;
63     }
64     public int compareTo(ActualVertexType other) {
65         return id - other.id;
66     }
67
68     void addOutgoingEdge(EdgeType edge) {
69         if (firstOutgoingEdge == null) {
70             firstOutgoingEdge = lastOutgoingEdge = edge;
71         } else {
72             lastOutgoingEdge.setNextOutgoingEdge(edge);
73             lastOutgoingEdge = edge;
74         }
75     }
76
77     EdgeType getFirstOutgoingEdge() {
78         return firstOutgoingEdge;
79     }
80
81     void addIncomingEdge(EdgeType edge) {
82         if (firstIncomingEdge == null) {
83             firstIncomingEdge = lastIncomingEdge = edge;
84         } else {
85             lastIncomingEdge.setNextIncomingEdge(edge);
86             lastIncomingEdge = edge;
87         }
88     }
89
90     EdgeType getFirstIncomingEdge() {
91         return firstIncomingEdge;
92     }
93
94     void removeIncomingEdge(EdgeType edge) {
95         EdgeType prev = null, cur = firstIncomingEdge;
96         while (cur != null) {
97             EdgeType next = cur.getNextIncomingEdge();
98             if (cur.equals(edge)) {
99                 if (prev != null)
100                     prev.setNextIncomingEdge(next);
101                 else
102                     firstIncomingEdge = next;
103                 return;
104             }
105             prev = cur;
106             cur = next;
107         }
108         throw new IllegalArgumentException JavaDoc("removing nonexistent edge!");
109     }
110
111     void removeOutgoingEdge(EdgeType edge) {
112         EdgeType prev = null, cur = firstOutgoingEdge;
113         while (cur != null) {
114             EdgeType next = cur.getNextOutgoingEdge();
115             if (cur.equals(edge)) {
116                 if (prev != null)
117                     prev.setNextOutgoingEdge(next);
118                 else
119                     firstOutgoingEdge = next;
120                 return;
121             }
122             prev = cur;
123             cur = cur.getNextOutgoingEdge();
124         }
125         throw new IllegalArgumentException JavaDoc("removing nonexistent edge!");
126     }
127
128
129 }
130
131 // vim:ts=4
132
Popular Tags