1
2
3
4
5
6
7
8
9
10 package com.sri.emo.dbobj;
11
12 import com.jcorporate.expresso.core.db.DBException;
13 import org.apache.log4j.Category;
14 import org.apache.log4j.Logger;
15
16 import java.util.Comparator;
17
18
19 /***
20 * Compare nodes by indent attribute, and failing that, by title.
21 *
22 * @author Larry Hamel
23 */
24 public class IndentComparator implements Comparator {
25 private Logger mLog;
26
27 /***
28 * Compares its two arguments for order. Returns a negative integer,
29 * zero, or a positive integer as the first argument is less than, equal
30 * to, or greater than the second.<p>
31 * <p/>
32 * The implementor must ensure that <tt>sgn(compare(x, y)) ==
33 * -sgn(compare(y, x))</tt> for all <tt>x</tt> and <tt>y</tt>. (This
34 * implies that <tt>compare(x, y)</tt> must throw an exception if and only
35 * if <tt>compare(y, x)</tt> throws an exception.)<p>
36 * <p/>
37 * The implementor must also ensure that the relation is transitive:
38 * <tt>((compare(x, y)>0) && (compare(y, z)>0))</tt> implies
39 * <tt>compare(x, z)>0</tt>.<p>
40 * <p/>
41 * Finally, the implementer must ensure that <tt>compare(x, y)==0</tt>
42 * implies that <tt>sgn(compare(x, z))==sgn(compare(y, z))</tt> for all
43 * <tt>z</tt>.<p>
44 * <p/>
45 * It is generally the case, but <i>not</i> strictly required that
46 * <tt>(compare(x, y)==0) == (x.equals(y))</tt>. Generally speaking,
47 * any comparator that violates this condition should clearly indicate
48 * this fact. The recommended language is "Note: this comparator
49 * imposes orderings that are inconsistent with equals."
50 *
51 * @param o1 the first object to be compared.
52 * @param o2 the second object to be compared.
53 * @return a negative integer, zero, or a positive integer as the
54 * first argument is less than, equal to, or greater than the
55 * second.
56 * @throws ClassCastException if the arguments' types prevent them from
57 * being compared by this Comparator.
58 */
59 public int compare(Object o1, Object o2) {
60 int result = 0;
61 Node n1 = (Node) o1;
62 Node n2 = (Node) o2;
63
64 Integer i1 = (Integer) n1.getAttribute(Node.INDENT);
65
66 if (i1 != null) {
67 Integer i2 = (Integer) n2.getAttribute(Node.INDENT);
68
69 if (i2 != null) {
70 result = i1.compareTo(i2);
71 }
72 }
73
74 if (result == 0) {
75 try {
76 result = n1.getNodeTitle().compareTo(n2.getNodeTitle());
77 } catch (DBException e) {
78 getLogger().error(e);
79 }
80 }
81
82 return result;
83 }
84
85 protected synchronized void setupSubclassLog() {
86 if (mLog == null) {
87 mLog = Logger.getLogger(getClass());
88 }
89 }
90
91 public Category getLogger() {
92 if (mLog == null) {
93 setupSubclassLog();
94 }
95
96 return mLog;
97 }
98 }