1 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 inbound = new HashSet(); 42 private Collection outbound = new HashSet(); 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 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 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 object) { 124 int result; 125 126 if (this == object) { 127 result = 0; 128 } else if (object == null || !(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 } 142 | Popular Tags |