KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > applications > community > modules > CompareMessages


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
12 package org.mmbase.applications.community.modules;
13
14 import org.mmbase.util.*;
15 import org.mmbase.module.core.*;
16 import java.util.Vector JavaDoc;
17
18 /**
19  * @author Dirk-Jan Hoekstra
20  * @version $Id: CompareMessages.java,v 1.4 2003/06/18 20:03:55 michiel Exp $
21  *
22  * CompareMessages implements the CompareInterface used by SortedVector.
23  * At forhand you've to specify on which fields the message nodes should be compared,
24  * these fields may not have a null value.
25  * @deprecated use mmbase.util.NodeComparator instead
26  */

27 public class CompareMessages implements CompareInterface {
28
29     public final static String JavaDoc UP = "UP";
30     public final static String JavaDoc DOWN = "DOWN";
31
32     private Vector JavaDoc fields;
33     private Vector JavaDoc sortDirs;
34
35     public CompareMessages(Vector JavaDoc fields) {
36         this.fields = fields;
37         sortDirs = new Vector JavaDoc(fields.size());
38         for (int i = 0; i < fields.size(); i++) {
39             sortDirs.add(UP);
40         }
41     }
42
43     /**
44      * Fields are the fields on which the message nodes get compared.
45      * Use UP and DOWN in the sortDirs vector to specifie the sort directions.
46      */

47     public CompareMessages(Vector JavaDoc fields, Vector JavaDoc sortDirs) {
48         this.fields = fields;
49         this.sortDirs = sortDirs;
50         for (int i = sortDirs.size(); i < fields.size(); i++) {
51             sortDirs.add(UP);
52         }
53     }
54
55     /**
56      * The two message nodes will be compared using the compare function of the values out of the fields.
57      * Only String and Integer values can be used, in other cases it's assumed that the values are equal.
58      */

59     public int compare(Object JavaDoc thisone, Object JavaDoc other) {
60         MMObjectNode n1 = (MMObjectNode)thisone;
61         MMObjectNode n2 = (MMObjectNode)other;
62         Object JavaDoc o1, o2;
63         int result;
64         int fieldnr = 0;
65         do {
66             o1 = n1.getValue((String JavaDoc)fields.elementAt(fieldnr));
67             o2 = n2.getValue((String JavaDoc)fields.elementAt(fieldnr));
68             if (o1 instanceof Integer JavaDoc) {
69                 if (((String JavaDoc)sortDirs.elementAt(fieldnr)).equals(UP))
70                     result = (((Integer JavaDoc)o1).compareTo((Integer JavaDoc)o2));
71                 else
72                     result = -(((Integer JavaDoc)o1).compareTo((Integer JavaDoc)o2));
73             } else if (o1 instanceof String JavaDoc) {
74                 if (((String JavaDoc)sortDirs.elementAt(fieldnr)).equals(UP))
75                     result = (((String JavaDoc)o1).compareToIgnoreCase((String JavaDoc)o2));
76                 else
77                     result = -(((String JavaDoc)o1).compareToIgnoreCase((String JavaDoc)o2));
78             } else {
79                 result = 0;
80             }
81             fieldnr++;
82         } while ((result == 0) && (fieldnr < fields.size()));
83         return result;
84     }
85 }
86
Popular Tags