View Javadoc

1   /*
2    *
3    *   Copyright 2005 The Apache Software Foundation.
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
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              //the tt1 and tt2 are parallel, now we have to count chain sizes
64              int tc1 = countChainLength(tt1);
65              int tc2 = countChainLength(tt2);
66              //longer the chain, deeper the node is
67              return tc2 - tc1;
68          }
69      }
70  
71      /***
72       * The &quot;depth&quot; at which this TransitionTarget exists in the
73       * SCXML object model.
74       *
75       * @param tt The TransitionTarget
76       * @return int The &quot;depth&quot;
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