##### Coverage Report - org.apache.commons.scxml.semantics.TransitionTargetComparator

Classes in this File | Line Coverage | Branch Coverage | Complexity
TransitionTargetComparator |
100% |
100% |
| 3.3333333333333335;3.333

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 | 43 |
             super();
37 | 43 |
         }
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 | 111 |
             TransitionTarget tt1 = (TransitionTarget) o1;
55 | 111 |
             TransitionTarget tt2 = (TransitionTarget) o2;
56 | 111 |
             if (tt1 == tt2) {
57 | 1 |
                 return 0;
58 | 110 |
             } else if (SCXMLHelper.isDescendant(tt1, tt2)) {
59 | 46 |
                 return -1;
60 | 64 |
             } else if (SCXMLHelper.isDescendant(tt2, tt1)) {
61 | 47 |
                 return 1;
62 |  |
             } else {
63 |  |
                 //the tt1 and tt2 are parallel, now we have to count chain sizes
64 | 17 |
                 int tc1 = countChainLength(tt1);
65 | 17 |
                 int tc2 = countChainLength(tt2);
66 |  |
                 //longer the chain, deeper the node is
67 | 17 |
                 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 | 34 |
             int count = 0;
80 | 34 |
             TransitionTarget parent = tt.getParent();
81 | 112 |
             while (parent != null) {
82 | 78 |
                 count++;
83 | 78 |
                 parent = parent.getParent();
84 |  |
             }
85 | 34 |
             return count;
86 |  |
         }
87 |  |
     }
88 |  |