KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > content > ObjectKeyComparator


1 package org.jahia.content;
2
3 import java.util.Comparator JavaDoc;
4
5 import org.jahia.utils.JahiaConsole;
6 import java.io.Serializable JavaDoc;
7
8 /**
9  * Title: Jahia
10  * Description: This class is used to define a comparator to be used in all
11  * sorts of sorted Java collections such as TreeSets, SortedLists, etc...
12  * Copyright: Copyright (c) 2002
13  * Company: Jahia Ltd
14  * @author Serge Huber
15  * @version 1.0
16  */

17
18 public class ObjectKeyComparator implements Comparator JavaDoc, Serializable JavaDoc {
19
20     public ObjectKeyComparator() {
21     }
22
23     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
24         if ((o1 instanceof ObjectKey) && (o2 instanceof ObjectKey)) {
25             ObjectKey key1 = (ObjectKey) o1;
26             ObjectKey key2 = (ObjectKey) o2;
27             return key1.getKey().compareTo(key2.getKey());
28         } else {
29             JahiaConsole.println("ObjectKeyComparator.compare",
30                                  "Comparator called on non ObjectKey objects, returning -1...");
31             return -1; // default is keep order
32
}
33     }
34
35     /**
36      * Must only return true if the passed object is a comparator and has the
37      * same order as this one, so we simply test if it is an instance of this
38      * comparator class...
39      */

40     public boolean equals(Object JavaDoc obj) {
41         if (obj instanceof ObjectKeyComparator) {
42             return true;
43         } else {
44             return false;
45         }
46     }
47 }
48
Popular Tags