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

    
27
package edu.ucsb.nceas.metacat;
28

    
29
import java.util.Comparator;
30

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

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

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

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

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

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