View Javadoc

1   /* ===================================================================
2    * Copyright 2002-04 SRI International.
3    * Released under the MOZILLA PUBLIC LICENSE Version 1.1
4    * which can be obtained at http://www.mozilla.org/MPL/MPL-1.1.html
5    * This software is distributed on an "AS IS"
6    * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
7    * See the License for the specific language governing rights and
8    * limitations under the License.
9    * =================================================================== */
10  package com.sri.emo.dbobj;
11  
12  import com.jcorporate.expresso.core.db.DBException;
13  
14  import java.util.Comparator;
15  import java.util.HashMap;
16  
17  
18  /***
19   * Provides comparison based on the user-specified order,
20   * provided in the constructor
21   *
22   * @author Larry Hamel
23   */
24  public class NodeRowIdOrderingComparator implements Comparator {
25      private HashMap mNodeIdOrderStringHash;
26  
27      public NodeRowIdOrderingComparator(Node[] ordering) {
28          mNodeIdOrderStringHash = new HashMap(ordering.length);
29          for (int i = 0; i < ordering.length; i++) {
30              Node node = ordering[i];
31              try {
32                  mNodeIdOrderStringHash.put(node.getNodeId(), "" + i);
33              } catch (DBException e) {
34                  node.getLogger().error("Cannot get node Id: ", e);
35              }
36          }
37      }
38  
39      /***
40       * Compares its two arguments for order.  Returns a negative integer,
41       * zero, or a positive integer as the first argument is less than, equal
42       * to, or greater than the second.<p>
43       * <p/>
44       * The implementor must ensure that <tt>sgn(compare(x, y)) ==
45       * -sgn(compare(y, x))</tt> for all <tt>x</tt> and <tt>y</tt>.  (This
46       * implies that <tt>compare(x, y)</tt> must throw an exception if and only
47       * if <tt>compare(y, x)</tt> throws an exception.)<p>
48       * <p/>
49       * The implementor must also ensure that the relation is transitive:
50       * <tt>((compare(x, y)&gt;0) &amp;&amp; (compare(y, z)&gt;0))</tt> implies
51       * <tt>compare(x, z)&gt;0</tt>.<p>
52       * <p/>
53       * Finally, the implementer must ensure that <tt>compare(x, y)==0</tt>
54       * implies that <tt>sgn(compare(x, z))==sgn(compare(y, z))</tt> for all
55       * <tt>z</tt>.<p>
56       * <p/>
57       * It is generally the case, but <i>not</i> strictly required that
58       * <tt>(compare(x, y)==0) == (x.equals(y))</tt>.  Generally speaking,
59       * any comparator that violates this condition should clearly indicate
60       * this fact.  The recommended language is "Note: this comparator
61       * imposes orderings that are inconsistent with equals."
62       *
63       * @param o1 the first object to be compared.
64       * @param o2 the second object to be compared.
65       * @return a negative integer, zero, or a positive integer as the
66       *         first argument is less than, equal to, or greater than the
67       *         second.
68       * @throws ClassCastException if the arguments' types prevent them from
69       *                            being compared by this Comparator.
70       */
71      public int compare(Object o1, Object o2) {
72          int result = 0;
73          MatrixCell c1 = (MatrixCell) o1;
74          MatrixCell c2 = (MatrixCell) o2;
75  
76          try {
77              String v1 = (String) mNodeIdOrderStringHash.get(c1.getRowId());
78              String v2 = (String) mNodeIdOrderStringHash.get(c2.getRowId());
79              result = v1.compareTo(v2);
80          } catch (Exception e) {
81              c1.getLogger().error("problem comparing matrix cells: ", e);
82          }
83  
84          return result;
85      }
86  }