Project

General

Profile

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 429 jones
 * A utility class that sorts two node records.
20
 * <p>
21
 * The order of the records
22
 * determines how the XML document is printed from DocumentImpl.toXml(),
23
 * so it is important that the sort order specified here results in a depth
24
 * first traversal of the nodes in tree.  Currently, the nodes are inserted
25
 * into the database in this depth-forst order, so the nodeid identifiers
26
 * are a good indicator of the proper sort order.
27
 * <p>
28
 * However, if we modify data loading semantics to allow document nodes to
29
 * be rearranged, or otherwise change the nodeindex value, this current
30
 * sort algorithm will fail to work.
31 388 jones
 */
32
public class NodeComparator implements Comparator {
33
34
  static int LESS = -1;
35
  static int EQUALS = 0;
36
  static int GREATER = 1;
37
38
  /**
39
   * Constructor
40
   */
41
  public NodeComparator() {
42
  }
43
44
  /**
45 389 jones
   * compare two objects to determine proper sort order -- delegates to
46
   * the compare(NodeRecord, NodeRecord) method.
47 388 jones
   */
48
  public int compare(Object o1, Object o2) {
49
    return compare((NodeRecord)o1, (NodeRecord)o2);
50
  }
51
52
  /**
53
   * compare two NodeRecord objects to determine proper sort order.  The
54
   * node records are equal if their nodeid fields are equal.  One is
55
   * less than another if its parentnodeid is less, or if its parentnodeid
56
   * is equal and its nodeindex is less.  One is greater than another if
57
   * its parentnodeid is greater, or if its parentnodeid is equal and
58
   * its nodeindex is greater.
59
   */
60 389 jones
  public int compare(NodeRecord o1, NodeRecord o2) {
61 388 jones
    if (o1.nodeid == o2.nodeid) {
62
      return EQUALS;
63 429 jones
    } else if (o1.nodeid < o2.nodeid) {
64
      return LESS;
65
    } else if (o1.nodeid > o2.nodeid) {
66
      return GREATER;
67
68
/*  // This is old code that used to sort the records into breadth-first
69
    // traversal order, based on the parentnodeid and the nodeindex.
70
    //
71
    if (o1.nodeid == o2.nodeid) {
72
      return EQUALS;
73 388 jones
    } else if (o1.parentnodeid < o2.parentnodeid) {
74
      return LESS;
75
    } else if (o1.parentnodeid > o2.parentnodeid) {
76
      return GREATER;
77
    } else if (o1.parentnodeid == o2.parentnodeid) {
78
      if (o1.nodeindex < o2.nodeindex) {
79
        return LESS;
80
      } else if (o1.nodeindex > o2.nodeindex) {
81
        return GREATER;
82
      } else {
83
        // this should never happen because (parentnodeid,nodeindex) is unique
84
        return EQUALS;
85
      }
86 429 jones
*/
87 388 jones
    } else {
88
      // this should never happen because parentnodeid is always <,>, or =
89
      return EQUALS;
90
    }
91
  }
92
}