KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > module > core > MMContainerCompare


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.module.core;
12
13 import java.util.*;
14 import org.mmbase.util.*;
15
16 import org.mmbase.util.logging.Logger;
17 import org.mmbase.util.logging.Logging;
18
19 /**
20  * Class to compare two MMObjectNodes, used by SortedVector.
21  * @see org.mmbase.util.SortedVector
22  * @see org.mmbase.util.CompareInterface
23  *
24  * @author Rico Jansen
25  * @version $Id: MMContainerCompare.java,v 1.7 2005/05/14 14:04:45 nico Exp $
26  * @todo Should implement java.util.Comparator. Perhaps should be named MMObjectNodeComparator. Btw, this class is not used.
27  *
28  */

29 public class MMContainerCompare implements CompareInterface {
30
31     private static Logger log = Logging.getLoggerInstance(MMContainerCompare.class);
32
33     public final static boolean ASC=true;
34     public final static boolean DESC=false;
35     private Vector orderfields;
36     private Vector orderdirections;
37     private Hashtable dataCompare=new Hashtable();
38     private StringCompare strcompare=new StringCompare();
39     private IntegerCompare intcompare=new IntegerCompare();
40
41     public MMContainerCompare() {
42         orderfields=new Vector();
43         orderdirections=new Vector();
44     }
45
46     public MMContainerCompare(Vector orderfields) {
47         orderdirections=new Vector();
48         this.orderfields=orderfields;
49         for (Enumeration e=orderfields.elements();e.hasMoreElements();) {
50             e.nextElement();
51             orderdirections.addElement(Boolean.valueOf(ASC));
52         }
53     }
54
55     public MMContainerCompare(Vector orderfields,Vector orderdirections) {
56         this.orderfields=orderfields;
57         this.orderdirections=orderdirections;
58     }
59
60     public void setCompare(String JavaDoc field,CompareInterface handler) {
61         dataCompare.put(field,handler);
62     }
63
64     public void addField(String JavaDoc field) {
65         addField(field,ASC);
66     }
67
68     public void addField(String JavaDoc field,boolean direction) {
69         orderfields.addElement(field);
70         orderdirections.addElement(Boolean.valueOf(direction));
71     }
72
73     public boolean setField(String JavaDoc field,boolean direction) {
74         int i;
75         boolean rtn=false;
76
77         i=orderfields.indexOf(field);
78         if (i!=-1) {
79             orderdirections.setElementAt(Boolean.valueOf(direction),i);
80             rtn=true;
81         } else {
82             rtn=false;
83         }
84         return rtn;
85     }
86
87     public void setOrderFields(Vector orderfields) {
88         this.orderfields=orderfields;
89     }
90
91     public void setOrderDirections(Vector orderdirections) {
92         this.orderdirections=orderdirections;
93     }
94
95     public void setOrder(Vector orderfields,Vector orderdirections) {
96         this.orderfields=orderfields;
97         this.orderdirections=orderdirections;
98     }
99
100
101     /**
102      * The compare function called by SortedVector to sort things.
103      * @see org.mmbase.util.SortedVector
104      * @see org.mmbase.util.CompareInterface
105      */

106     public int compare(Object JavaDoc thisone,Object JavaDoc other) {
107         MMObjectNode thisnode,othernode;
108         int rtn=0;
109         int i;
110         String JavaDoc field;
111         Object JavaDoc thisdata;
112         Object JavaDoc otherdata;
113         CompareInterface comp;
114
115         thisnode=(MMObjectNode)thisone;
116         othernode=(MMObjectNode)other;
117
118         i=0;
119         for (Enumeration e=orderfields.elements();e.hasMoreElements();i++) {
120             field=(String JavaDoc)e.nextElement();
121             thisdata=thisnode.getValue(field);
122             otherdata=othernode.getValue(field);
123             // Handle null data cases
124
if (thisdata!=null) {
125                 if (otherdata!=null) {
126                     // data ? data
127
comp=(CompareInterface)dataCompare.get(field);
128                     if (comp==null) {
129                         if (thisdata instanceof String JavaDoc) {
130                             comp=strcompare;
131                         } else if (thisdata instanceof Integer JavaDoc) {
132                             comp=intcompare;
133                         } else {
134                             log.error("no handler for field " + field);
135                             comp=null;
136                             rtn=0;
137                         }
138                     }
139                     if (comp!=null) {
140                         rtn=comp.compare(thisdata,otherdata);
141                     }
142                 } else {
143                     // data < null
144
rtn=-1;
145                 }
146             } else {
147                 if (otherdata!=null) {
148                     // null > data
149
rtn=1;
150                 } else {
151                     // null == null
152
rtn=0;
153                 }
154             }
155             if (((Boolean JavaDoc)orderdirections.elementAt(i)).booleanValue()==DESC) {
156                 rtn=0-rtn;
157             }
158             // No need to compare further if they are unequal
159
if (rtn!=0) break;
160         }
161         return rtn;
162     }
163 }
164
Popular Tags