1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.scxml.semantics;
19
20 import java.util.Comparator;
21
22 import org.apache.commons.scxml.SCXMLHelper;
23 import org.apache.commons.scxml.model.TransitionTarget;
24
25
26 /***
27 * A comparator for TransitionTarget instances.
28 *
29 */
30 final class TransitionTargetComparator implements Comparator {
31
32 /***
33 * Constructor.
34 */
35 TransitionTargetComparator() {
36 super();
37 }
38
39 /***
40 * Compares two instances of TransitionTarget in terms of the
41 * SCXML tree hierarchy.
42 * <p>Important Remarks:</p> does not fullfill the Comparator contract,
43 * since it returns 0 if o1 == o2 and also if they are not related to each
44 * other and at the same time the chain-to-parent length for o1 is the
45 * same length as for o2 (that is, they are equally deeply nested)
46 *
47 * @param o1 The first TransitionTarget object
48 * @param o2 The second TransitionTarget object
49 * @return int The comparation result
50 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
51 * @see TransitionTarget
52 */
53 public int compare(final Object o1, final Object o2) {
54 TransitionTarget tt1 = (TransitionTarget) o1;
55 TransitionTarget tt2 = (TransitionTarget) o2;
56 if (tt1 == tt2) {
57 return 0;
58 } else if (SCXMLHelper.isDescendant(tt1, tt2)) {
59 return -1;
60 } else if (SCXMLHelper.isDescendant(tt2, tt1)) {
61 return 1;
62 } else {
63
64 int tc1 = countChainLength(tt1);
65 int tc2 = countChainLength(tt2);
66
67 return tc2 - tc1;
68 }
69 }
70
71 /***
72 * The "depth" at which this TransitionTarget exists in the
73 * SCXML object model.
74 *
75 * @param tt The TransitionTarget
76 * @return int The "depth"
77 */
78 private int countChainLength(final TransitionTarget tt) {
79 int count = 0;
80 TransitionTarget parent = tt.getParent();
81 while (parent != null) {
82 count++;
83 parent = parent.getParent();
84 }
85 return count;
86 }
87 }
88