KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > dependency > Node


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.jeantessier.dependency;
34
35 import java.util.*;
36
37 public abstract class Node implements Comparable JavaDoc {
38     private String JavaDoc name = "";
39     private boolean confirmed = false;
40     
41     private Collection inbound = new HashSet();
42     private Collection outbound = new HashSet();
43
44     public Node(String JavaDoc name, boolean confirmed) {
45         this.name = name;
46         this.confirmed = confirmed;
47     }
48
49     public String JavaDoc getName() {
50         return name;
51     }
52
53     public boolean isConfirmed() {
54         return confirmed;
55     }
56
57     // Only to be used by NodeFactory and DeletingVisitor
58
void setConfirmed(boolean confirmed) {
59         this.confirmed = confirmed;
60     }
61     
62     public boolean canAddDependencyTo(Node node) {
63         return !equals(node);
64     }
65     
66     public void addDependency(Node node) {
67         if (canAddDependencyTo(node) && node.canAddDependencyTo(this)) {
68             outbound.add(node);
69             node.inbound.add(this);
70         }
71     }
72
73     public void addDependencies(Collection nodes) {
74         Iterator i = nodes.iterator();
75         while (i.hasNext()) {
76             addDependency((Node) i.next());
77         }
78     }
79
80     public void removeDependency(Node node) {
81         outbound.remove(node);
82         node.inbound.remove(this);
83     }
84
85     public void removeDependencies(Collection nodes) {
86         Iterator i = nodes.iterator();
87         while (i.hasNext()) {
88             removeDependency((Node) i.next());
89         }
90     }
91
92     public Collection getInboundDependencies() {
93         return Collections.unmodifiableCollection(inbound);
94     }
95
96     public Collection getOutboundDependencies() {
97         return Collections.unmodifiableCollection(outbound);
98     }
99
100     public abstract void accept(Visitor visitor);
101     public abstract void acceptInbound(Visitor visitor);
102     public abstract void acceptOutbound(Visitor visitor);
103
104     public int hashCode() {
105         return getName().hashCode();
106     }
107
108     public boolean equals(Object JavaDoc 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 = getName().equals(other.getName());
118         }
119
120         return result;
121     }
122
123     public int compareTo(Object JavaDoc object) {
124         int result;
125
126         if (this == object) {
127             result = 0;
128         } else if (object == null || !(object instanceof Node)) {
129             throw new ClassCastException JavaDoc("compareTo: expected a " + getClass().getName() + " but got a " + object.getClass().getName());
130         } else {
131             Node other = (Node) object;
132             result = getName().compareTo(other.getName());
133         }
134
135         return result;
136     }
137
138     public String JavaDoc toString() {
139         return getName();
140     }
141 }
142
Popular Tags