1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
package com.jeantessier.dependency; |
34 | |
|
35 | |
import java.util.*; |
36 | |
|
37 | |
public abstract class Node implements Comparable { |
38 | 7185 | private String name = ""; |
39 | 7185 | private boolean confirmed = false; |
40 | |
|
41 | 7185 | private Collection<Node> inbound = new HashSet<Node>(); |
42 | 7185 | private Collection<Node> outbound = new HashSet<Node>(); |
43 | |
|
44 | 7185 | public Node(String name, boolean confirmed) { |
45 | 7185 | this.name = name; |
46 | 7185 | this.confirmed = confirmed; |
47 | 7185 | } |
48 | |
|
49 | |
public String getName() { |
50 | 58899 | return name; |
51 | |
} |
52 | |
|
53 | |
public boolean isConfirmed() { |
54 | 7593 | return confirmed; |
55 | |
} |
56 | |
|
57 | |
|
58 | |
void setConfirmed(boolean confirmed) { |
59 | 180 | this.confirmed = confirmed; |
60 | 180 | } |
61 | |
|
62 | |
public boolean canAddDependencyTo(Node node) { |
63 | 14311 | return !equals(node); |
64 | |
} |
65 | |
|
66 | |
public void addDependency(Node node) { |
67 | 3226 | if (canAddDependencyTo(node) && node.canAddDependencyTo(this)) { |
68 | 3222 | outbound.add(node); |
69 | 3222 | node.inbound.add(this); |
70 | |
} |
71 | 3226 | } |
72 | |
|
73 | |
public void addDependencies(Collection<Node> nodes) { |
74 | 0 | for (Node node : nodes) { |
75 | 0 | addDependency(node); |
76 | |
} |
77 | 0 | } |
78 | |
|
79 | |
public void removeDependency(Node node) { |
80 | 559 | outbound.remove(node); |
81 | 559 | node.inbound.remove(this); |
82 | 559 | } |
83 | |
|
84 | |
public void removeDependencies(Collection<? extends Node> nodes) { |
85 | 1 | for (Node node : nodes) { |
86 | 2 | removeDependency(node); |
87 | |
} |
88 | 1 | } |
89 | |
|
90 | |
public Collection<Node> getInboundDependencies() { |
91 | 5180 | return Collections.unmodifiableCollection(inbound); |
92 | |
} |
93 | |
|
94 | |
public Collection<Node> getOutboundDependencies() { |
95 | 6409 | 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 | 13633 | return getName().hashCode(); |
104 | |
} |
105 | |
|
106 | |
public boolean equals(Object object) { |
107 | |
boolean result; |
108 | |
|
109 | 21615 | if (this == object) { |
110 | 2325 | result = true; |
111 | 19290 | } else if (object == null || getClass() != object.getClass()) { |
112 | 9422 | result = false; |
113 | |
} else { |
114 | 9868 | Node other = (Node) object; |
115 | 9868 | result = compareTo(other) == 0; |
116 | |
} |
117 | |
|
118 | 21615 | return result; |
119 | |
} |
120 | |
|
121 | |
public int compareTo(Object object) { |
122 | |
int result; |
123 | |
|
124 | 11797 | if (this == object) { |
125 | 512 | result = 0; |
126 | 11285 | } else if (object == null) { |
127 | 0 | throw new ClassCastException("compareTo: expected a " + getClass().getName() + " but got null"); |
128 | 11285 | } else if (!(object instanceof Node)) { |
129 | 0 | throw new ClassCastException("compareTo: expected a " + getClass().getName() + " but got a " + object.getClass().getName()); |
130 | |
} else { |
131 | 11285 | Node other = (Node) object; |
132 | 11285 | result = getName().compareTo(other.getName()); |
133 | |
} |
134 | |
|
135 | 11797 | return result; |
136 | |
} |
137 | |
|
138 | |
public String toString() { |
139 | 14603 | return getName(); |
140 | |
} |
141 | |
} |