001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.dependency;
034:
035: import java.util.*;
036:
037: public abstract class Node implements Comparable {
038: private String name = "";
039: private boolean confirmed = false;
040:
041: private Collection<Node> inbound = new HashSet<Node>();
042: private Collection<Node> outbound = new HashSet<Node>();
043:
044: public Node(String name, boolean confirmed) {
045: this .name = name;
046: this .confirmed = confirmed;
047: }
048:
049: public String getName() {
050: return name;
051: }
052:
053: public boolean isConfirmed() {
054: return confirmed;
055: }
056:
057: // Only to be used by NodeFactory and DeletingVisitor
058: void setConfirmed(boolean confirmed) {
059: this .confirmed = confirmed;
060: }
061:
062: public boolean canAddDependencyTo(Node node) {
063: return !equals(node);
064: }
065:
066: public void addDependency(Node node) {
067: if (canAddDependencyTo(node) && node.canAddDependencyTo(this )) {
068: outbound.add(node);
069: node.inbound.add(this );
070: }
071: }
072:
073: public void addDependencies(Collection<Node> nodes) {
074: for (Node node : nodes) {
075: addDependency(node);
076: }
077: }
078:
079: public void removeDependency(Node node) {
080: outbound.remove(node);
081: node.inbound.remove(this );
082: }
083:
084: public void removeDependencies(Collection<? extends Node> nodes) {
085: for (Node node : nodes) {
086: removeDependency(node);
087: }
088: }
089:
090: public Collection<Node> getInboundDependencies() {
091: return Collections.unmodifiableCollection(inbound);
092: }
093:
094: public Collection<Node> getOutboundDependencies() {
095: return Collections.unmodifiableCollection(outbound);
096: }
097:
098: public abstract void accept(Visitor visitor);
099:
100: public abstract void acceptInbound(Visitor visitor);
101:
102: public abstract void acceptOutbound(Visitor visitor);
103:
104: public int hashCode() {
105: return getName().hashCode();
106: }
107:
108: public boolean equals(Object object) {
109: boolean result;
110:
111: if (this == object) {
112: result = true;
113: } else if (object == null || getClass() != object.getClass()) {
114: result = false;
115: } else {
116: Node other = (Node) object;
117: result = compareTo(other) == 0;
118: }
119:
120: return result;
121: }
122:
123: public int compareTo(Object object) {
124: int result;
125:
126: if (this == object) {
127: result = 0;
128: } else if (object == null) {
129: throw new ClassCastException("compareTo: expected a "
130: + getClass().getName() + " but got null");
131: } else if (!(object instanceof Node)) {
132: throw new ClassCastException("compareTo: expected a "
133: + getClass().getName() + " but got a "
134: + object.getClass().getName());
135: } else {
136: Node other = (Node) object;
137: result = getName().compareTo(other.getName());
138: }
139:
140: return result;
141: }
142:
143: public String toString() {
144: return getName();
145: }
146: }
|