01: package com.jeantessier.dependency;
02:
03: import java.util.*;
04:
05: /**
06: * TODO Class comment
07: */
08: public class Cycle implements Comparable {
09: private List<Node> path;
10:
11: public Cycle(List<Node> path) {
12: Set<Node> nodes = new TreeSet<Node>(path);
13: Node first = nodes.iterator().next();
14: LinkedList<Node> rawPath = new LinkedList<Node>(path);
15: while (!rawPath.getFirst().equals(first)) {
16: rawPath.addLast(rawPath.removeFirst());
17: }
18:
19: this .path = rawPath;
20: }
21:
22: public List<Node> getPath() {
23: return Collections.unmodifiableList(path);
24: }
25:
26: public int getLength() {
27: return getPath().size();
28: }
29:
30: public boolean equals(Object object) {
31: boolean result;
32:
33: if (this == object) {
34: result = true;
35: } else if (object == null || getClass() != object.getClass()) {
36: result = false;
37: } else {
38: Cycle other = (Cycle) object;
39: result = compareTo(other) == 0;
40: }
41:
42: return result;
43: }
44:
45: public int hashCode() {
46: return getPath().hashCode();
47: }
48:
49: public int compareTo(Object object) {
50: int result;
51:
52: if (this == object) {
53: result = 0;
54: } else if (object == null) {
55: throw new ClassCastException("compareTo: expected a "
56: + getClass().getName() + " but got null");
57: } else if (!(object instanceof Cycle)) {
58: throw new ClassCastException("compareTo: expected a "
59: + getClass().getName() + " but got a "
60: + object.getClass().getName());
61: } else {
62: Cycle other = (Cycle) object;
63:
64: result = getLength() - other.getLength();
65: Iterator<Node> theseNodes = getPath().iterator();
66: Iterator<Node> otherNodes = other.getPath().iterator();
67: while (result == 0 && theseNodes.hasNext()
68: && otherNodes.hasNext()) {
69: result = theseNodes.next().compareTo(otherNodes.next());
70: }
71: }
72:
73: return result;
74: }
75:
76: public String toString() {
77: return getPath().toString();
78: }
79: }
|