1 | /* |
2 | * Copyright (c) 2001-2009, 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 { |
38 | private String name = ""; |
39 | private boolean confirmed = false; |
40 | |
41 | private Collection<Node> inbound = new HashSet<Node>(); |
42 | private Collection<Node> outbound = new HashSet<Node>(); |
43 | |
44 | public Node(String name, boolean confirmed) { |
45 | this.name = name; |
46 | this.confirmed = confirmed; |
47 | } |
48 | |
49 | public String 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<Node> nodes) { |
74 | for (Node node : nodes) { |
75 | addDependency(node); |
76 | } |
77 | } |
78 | |
79 | public void removeDependency(Node node) { |
80 | outbound.remove(node); |
81 | node.inbound.remove(this); |
82 | } |
83 | |
84 | public void removeDependencies(Collection<? extends Node> nodes) { |
85 | for (Node node : nodes) { |
86 | removeDependency(node); |
87 | } |
88 | } |
89 | |
90 | public Collection<Node> getInboundDependencies() { |
91 | return Collections.unmodifiableCollection(inbound); |
92 | } |
93 | |
94 | public Collection<Node> getOutboundDependencies() { |
95 | return Collections.unmodifiableCollection(outbound); |
96 | } |
97 | |
98 | public abstract void accept(Visitor visitor); |
99 | public abstract void acceptInbound(Visitor visitor); |
100 | public abstract void acceptOutbound(Visitor visitor); |
101 | |
102 | public int hashCode() { |
103 | return getName().hashCode(); |
104 | } |
105 | |
106 | public boolean equals(Object object) { |
107 | boolean result; |
108 | |
109 | if (this == object) { |
110 | result = true; |
111 | } else if (object == null || getClass() != object.getClass()) { |
112 | result = false; |
113 | } else { |
114 | Node other = (Node) object; |
115 | result = compareTo(other) == 0; |
116 | } |
117 | |
118 | return result; |
119 | } |
120 | |
121 | public int compareTo(Object object) { |
122 | int result; |
123 | |
124 | if (this == object) { |
125 | result = 0; |
126 | } else if (object == null) { |
127 | throw new ClassCastException("compareTo: expected a " + getClass().getName() + " but got null"); |
128 | } else if (!(object instanceof Node)) { |
129 | throw new ClassCastException("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 toString() { |
139 | return getName(); |
140 | } |
141 | } |