KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > NodeComparator


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10
11 package org.mmbase.util;
12
13 import java.util.*;
14 import org.mmbase.module.core.*;
15
16 /**
17  * This class implements the Comparator interface for comparing MMObjectNodes.
18  * At forhand you specify in which fields a specified nodes should be compared,
19  * these fields may not have a null value.
20  *
21  * @application Tools
22  * @author Pierre van Rooden
23  * @version $Id: NodeComparator.java,v 1.6 2004/10/12 11:17:44 pierre Exp $
24  */

25 public class NodeComparator implements Comparator {
26
27     public final static String JavaDoc UP = "UP";
28     public final static String JavaDoc DOWN = "DOWN";
29
30     /**
31      * @todo Should be List, not Vector
32      */

33     private Vector fields;
34     /**
35      * @todo Should be List, not Vector
36      */

37     private Vector sortDirs;
38
39     /**
40      * Simple constructor that uses the default sort order (UP).
41      * @param fields the fields on which the message nodes get compared.
42      */

43     public NodeComparator(Vector fields) {
44         this.fields = fields;
45         sortDirs = new Vector(fields.size());
46     }
47
48     /**
49      * Constructor in which you spercify the sort order (UP or DOWN) per field.
50      * @param fields the fields on which the message nodes get compared.
51      * @param sortDirs the sort directions (UP or DOWN) for each field.
52      */

53     public NodeComparator(Vector fields, Vector sortDirs) {
54         this.fields = fields;
55         this.sortDirs = sortDirs;
56         for (int i = sortDirs.size(); i < fields.size(); i++) {
57             sortDirs.add(UP);
58         }
59     }
60
61     /**
62      * The two message nodes will be compared using the compare function of
63      * the values of their fields.
64      * Only Comparable values can be used (String, Numbers, Date), as well as
65      * Boolean values.
66      * In other cases it's assumed that the values cannot be ordered.
67      * <br />
68      * Note: this class assumes that values in fields are of similar types
69      * (comparable to each other).
70      *
71      * @param o1 the first object to compare
72      * @param o2 the second object to compare
73      * @return 0 if both objects are equal, -1 if object 1 is 'less than'
74      * object 2, and +1 if object 1 is 'greater than' object 2.
75      */

76     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
77         Object JavaDoc f1, f2;
78         int result=0;
79         int fieldnr = 0;
80         String JavaDoc field;
81         while ((result == 0) && (fieldnr < fields.size())) {
82             field =(String JavaDoc)fields.elementAt(fieldnr);
83             f1 = ((MMObjectNode)o1).getValue(field);
84             f2 = ((MMObjectNode)o2).getValue(field);
85             if (f1 instanceof Comparable JavaDoc) {
86                 try {
87                     result=((Comparable JavaDoc)f1).compareTo(f2);
88                 } catch (ClassCastException JavaDoc e) {
89                     // types do not compare -
90
// possibly the in-memory value type differs from the
91
// database value type (this can occur if you use setValue
92
// with a deviating type).
93
// Solving this coukld bring this compare to a crawl, so we
94
// don't. Just edit stuff the right way.
95
}
96             } else if (!f1.equals(f2)) {
97                 if (f1 instanceof Boolean JavaDoc) {
98                     result=((Boolean JavaDoc)f1).booleanValue() ? 1 : -1;
99                 }
100             }
101             fieldnr++;
102         }
103         if ((fieldnr>0) &&
104             (fieldnr<=sortDirs.size()) &&
105             ((String JavaDoc)sortDirs.elementAt(fieldnr-1)).equals(DOWN)) {
106             result=-result;
107         }
108         return result;
109     }
110
111     /**
112      * Returns whether another object is equal to this comparator (that is,
113      * compare the same fields in the same order).
114      * @param obj the object to compare
115      * @return <code>true</code> if the objects are equal
116      * @throws ClassCastException
117      */

118     public boolean equals(Object JavaDoc obj) {
119         if (obj instanceof NodeComparator) {
120             return (obj.hashCode()==hashCode());
121         } else {
122             throw new ClassCastException JavaDoc();
123         }
124     }
125
126     /**
127      * Returns the comparator's hash code.
128      */

129     public int hashCode() {
130         return fields.hashCode()^sortDirs.hashCode();
131     }
132 }
133
Popular Tags