Project

General

Profile

1
/**
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: jones $'
10
 *     '$Date: 2001-01-18 11:52:00 -0800 (Thu, 18 Jan 2001) $'
11
 * '$Revision: 669 $'
12
 *
13
 * This program is free software; you can redistribute it and/or modify
14
 * it under the terms of the GNU General Public License as published by
15
 * the Free Software Foundation; either version 2 of the License, or
16
 * (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program; if not, write to the Free Software
25
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
 */
27

    
28
package edu.ucsb.nceas.metacat;
29

    
30
import java.util.Comparator;
31

    
32
/**
33
 * A utility class that sorts two node records.  
34
 * <p>
35
 * The order of the records
36
 * determines how the XML document is printed from DocumentImpl.toXml(),
37
 * so it is important that the sort order specified here results in a depth
38
 * first traversal of the nodes in tree.  Currently, the nodes are inserted
39
 * into the database in this depth-forst order, so the nodeid identifiers
40
 * are a good indicator of the proper sort order.
41
 * <p>
42
 * However, if we modify data loading semantics to allow document nodes to
43
 * be rearranged, or otherwise change the nodeindex value, this current
44
 * sort algorithm will fail to work.
45
 */
46
public class NodeComparator implements Comparator {
47

    
48
  static int LESS = -1;
49
  static int EQUALS = 0;
50
  static int GREATER = 1;
51

    
52
  /**
53
   * Constructor
54
   */
55
  public NodeComparator() {
56
  }
57

    
58
  /**
59
   * compare two objects to determine proper sort order -- delegates to 
60
   * the compare(NodeRecord, NodeRecord) method.
61
   */
62
  public int compare(Object o1, Object o2) {
63
    return compare((NodeRecord)o1, (NodeRecord)o2);
64
  }
65

    
66
  /**
67
   * compare two NodeRecord objects to determine proper sort order.  The 
68
   * node records are equal if their nodeid fields are equal.  One is
69
   * less than another if its parentnodeid is less, or if its parentnodeid
70
   * is equal and its nodeindex is less.  One is greater than another if
71
   * its parentnodeid is greater, or if its parentnodeid is equal and
72
   * its nodeindex is greater.
73
   */
74
  public int compare(NodeRecord o1, NodeRecord o2) {
75
    if (o1.nodeid == o2.nodeid) {
76
      return EQUALS;
77
    } else if (o1.nodeid < o2.nodeid) {
78
      return LESS;
79
    } else if (o1.nodeid > o2.nodeid) {
80
      return GREATER;
81

    
82
/*  // This is old code that used to sort the records into breadth-first
83
    // traversal order, based on the parentnodeid and the nodeindex.
84
    //
85
    if (o1.nodeid == o2.nodeid) {
86
      return EQUALS;
87
    } else if (o1.parentnodeid < o2.parentnodeid) {
88
      return LESS;
89
    } else if (o1.parentnodeid > o2.parentnodeid) {
90
      return GREATER;
91
    } else if (o1.parentnodeid == o2.parentnodeid) {
92
      if (o1.nodeindex < o2.nodeindex) {
93
        return LESS;
94
      } else if (o1.nodeindex > o2.nodeindex) {
95
        return GREATER;
96
      } else {
97
        // this should never happen because (parentnodeid,nodeindex) is unique
98
        return EQUALS;
99
      }
100
*/
101
    } else {
102
      // this should never happen because parentnodeid is always <,>, or =
103
      return EQUALS;
104
    }
105
  }
106
}
(33-33/40)