001: /*
002: * Generic graph library
003: * Copyright (C) 2000,2003,2004,2007 University of Maryland
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019:
020: package edu.umd.cs.findbugs.graph;
021:
022: /**
023: * GraphVertex implementation for use with AbstractGraph.
024: *
025: * @see GraphVertex
026: * @see AbstractGraph
027: * @see AbstractEdge
028: * @author David Hovemeyer
029: */
030: public class AbstractVertex<EdgeType extends AbstractEdge<EdgeType, ActualVertexType>, ActualVertexType extends AbstractVertex<EdgeType, ActualVertexType>>
031: implements GraphVertex<ActualVertexType> {
032:
033: private int label;
034: EdgeType firstIncomingEdge, lastIncomingEdge;
035: EdgeType firstOutgoingEdge, lastOutgoingEdge;
036:
037: public int getLabel() {
038: return label;
039: }
040:
041: public void setLabel(int label) {
042: this .label = label;
043: }
044:
045: @Override
046: public int hashCode() {
047: return label;
048: }
049:
050: @Override
051: public boolean equals(Object o) {
052: if (o == null || o.getClass() != this .getClass()) {
053: return false;
054: }
055: ActualVertexType other = (ActualVertexType) o;
056: return other.getLabel() == this .getLabel();
057: }
058:
059: public int compareTo(ActualVertexType other) {
060: if (this .getLabel() < other.getLabel()) {
061: return -1;
062: } else if (this .getLabel() > other.getLabel()) {
063: return 1;
064: } else {
065: return 0;
066: }
067: }
068:
069: void addOutgoingEdge(EdgeType edge) {
070: if (firstOutgoingEdge == null) {
071: firstOutgoingEdge = lastOutgoingEdge = edge;
072: } else {
073: lastOutgoingEdge.setNextOutgoingEdge(edge);
074: lastOutgoingEdge = edge;
075: }
076: }
077:
078: EdgeType getFirstOutgoingEdge() {
079: return firstOutgoingEdge;
080: }
081:
082: void addIncomingEdge(EdgeType edge) {
083: if (firstIncomingEdge == null) {
084: firstIncomingEdge = lastIncomingEdge = edge;
085: } else {
086: lastIncomingEdge.setNextIncomingEdge(edge);
087: lastIncomingEdge = edge;
088: }
089: }
090:
091: EdgeType getFirstIncomingEdge() {
092: return firstIncomingEdge;
093: }
094:
095: void removeIncomingEdge(EdgeType edge) {
096: EdgeType prev = null, cur = firstIncomingEdge;
097: while (cur != null) {
098: EdgeType next = cur.getNextIncomingEdge();
099: if (cur.equals(edge)) {
100: if (prev != null)
101: prev.setNextIncomingEdge(next);
102: else
103: firstIncomingEdge = next;
104: return;
105: }
106: prev = cur;
107: cur = next;
108: }
109: throw new IllegalArgumentException("removing nonexistent edge!");
110: }
111:
112: void removeOutgoingEdge(EdgeType edge) {
113: EdgeType prev = null, cur = firstOutgoingEdge;
114: while (cur != null) {
115: EdgeType next = cur.getNextOutgoingEdge();
116: if (cur.equals(edge)) {
117: if (prev != null)
118: prev.setNextOutgoingEdge(next);
119: else
120: firstOutgoingEdge = next;
121: return;
122: }
123: prev = cur;
124: cur = cur.getNextOutgoingEdge();
125: }
126: throw new IllegalArgumentException("removing nonexistent edge!");
127: }
128:
129: }
130:
131: // vim:ts=4
|