1 |
388
|
jones
|
/**
|
2 |
|
|
* '$RCSfile$'
|
3 |
|
|
* Purpose: A Class that sorts two NodeRecords
|
4 |
|
|
* Copyright: 2000 Regents of the University of California and the
|
5 |
|
|
* National Center for Ecological Analysis and Synthesis
|
6 |
|
|
* Authors: Matt Jones
|
7 |
|
|
* Release: @release@
|
8 |
|
|
*
|
9 |
|
|
* '$Author$'
|
10 |
|
|
* '$Date$'
|
11 |
|
|
* '$Revision$'
|
12 |
|
|
*/
|
13 |
|
|
|
14 |
|
|
package edu.ucsb.nceas.metacat;
|
15 |
|
|
|
16 |
|
|
import java.util.Comparator;
|
17 |
|
|
|
18 |
|
|
/**
|
19 |
|
|
* A utility class that sorts two node records
|
20 |
|
|
*/
|
21 |
|
|
public class NodeComparator implements Comparator {
|
22 |
|
|
|
23 |
|
|
static int LESS = -1;
|
24 |
|
|
static int EQUALS = 0;
|
25 |
|
|
static int GREATER = 1;
|
26 |
|
|
|
27 |
|
|
/**
|
28 |
|
|
* Constructor
|
29 |
|
|
*/
|
30 |
|
|
public NodeComparator() {
|
31 |
|
|
}
|
32 |
|
|
|
33 |
|
|
/**
|
34 |
389
|
jones
|
* compare two objects to determine proper sort order -- delegates to
|
35 |
|
|
* the compare(NodeRecord, NodeRecord) method.
|
36 |
388
|
jones
|
*/
|
37 |
|
|
public int compare(Object o1, Object o2) {
|
38 |
|
|
return compare((NodeRecord)o1, (NodeRecord)o2);
|
39 |
|
|
}
|
40 |
|
|
|
41 |
|
|
/**
|
42 |
|
|
* compare two NodeRecord objects to determine proper sort order. The
|
43 |
|
|
* node records are equal if their nodeid fields are equal. One is
|
44 |
|
|
* less than another if its parentnodeid is less, or if its parentnodeid
|
45 |
|
|
* is equal and its nodeindex is less. One is greater than another if
|
46 |
|
|
* its parentnodeid is greater, or if its parentnodeid is equal and
|
47 |
|
|
* its nodeindex is greater.
|
48 |
|
|
*/
|
49 |
389
|
jones
|
public int compare(NodeRecord o1, NodeRecord o2) {
|
50 |
388
|
jones
|
if (o1.nodeid == o2.nodeid) {
|
51 |
|
|
return EQUALS;
|
52 |
|
|
} else if (o1.parentnodeid < o2.parentnodeid) {
|
53 |
|
|
return LESS;
|
54 |
|
|
} else if (o1.parentnodeid > o2.parentnodeid) {
|
55 |
|
|
return GREATER;
|
56 |
|
|
} else if (o1.parentnodeid == o2.parentnodeid) {
|
57 |
|
|
if (o1.nodeindex < o2.nodeindex) {
|
58 |
|
|
return LESS;
|
59 |
|
|
} else if (o1.nodeindex > o2.nodeindex) {
|
60 |
|
|
return GREATER;
|
61 |
|
|
} else {
|
62 |
|
|
// this should never happen because (parentnodeid,nodeindex) is unique
|
63 |
|
|
return EQUALS;
|
64 |
|
|
}
|
65 |
|
|
} else {
|
66 |
|
|
// this should never happen because parentnodeid is always <,>, or =
|
67 |
|
|
return EQUALS;
|
68 |
|
|
}
|
69 |
|
|
}
|
70 |
|
|
}
|