KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > base > util > collections > MapComparator


1 /*
2  * $Id: MapComparator.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.base.util.collections;
25
26 import java.util.Comparator JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import org.ofbiz.base.util.Debug;
32
33 /**
34  * MapComparator.java
35  *
36  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
37  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
38  * @version $Rev: 5462 $
39  * @since 2.0
40  */

41 public class MapComparator implements Comparator JavaDoc {
42     
43     public static final String JavaDoc module = MapComparator.class.getName();
44     
45     private List JavaDoc keys;
46
47     /**
48      * Method MapComparator.
49      * @param keys List of Map keys to sort on
50      */

51     public MapComparator(List JavaDoc keys) {
52         this.keys = keys;
53     }
54
55     /**
56      * @see java.lang.Object#equals(java.lang.Object)
57      */

58     public boolean equals(Object JavaDoc obj) {
59         return obj.equals(this);
60     }
61
62     /**
63      * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
64      */

65     public int compare(Object JavaDoc obj1, Object JavaDoc obj2) {
66         Map JavaDoc map1, map2;
67         try {
68             map1 = (Map JavaDoc) obj1;
69             map2 = (Map JavaDoc) obj2;
70         } catch (ClassCastException JavaDoc e) {
71             throw new IllegalArgumentException JavaDoc("Objects not from the Map interface");
72         }
73
74         if (keys == null || keys.size() < 1) {
75             throw new IllegalArgumentException JavaDoc("No sort fields defined");
76         }
77
78         Iterator JavaDoc i = keys.iterator();
79         while (i.hasNext()) {
80             Object JavaDoc key = i.next();
81             // if false will be descending, ie reverse order
82
boolean ascending = true;
83
84             Object JavaDoc o1 = null;
85             Object JavaDoc o2 = null;
86
87             if (key instanceof FlexibleMapAccessor) {
88                 FlexibleMapAccessor fmaKey = (FlexibleMapAccessor) key;
89                 ascending = fmaKey.getIsAscending();
90                 
91                 //Debug.logInfo("Doing compare with a FlexibleMapAccessor [" + fmaKey.getOriginalName() + "] ascending [" + ascending + "]", module);
92

93                 o1 = fmaKey.get(map1);
94                 o2 = fmaKey.get(map2);
95             } else {
96                 if (key instanceof String JavaDoc) {
97                     String JavaDoc keyStr = (String JavaDoc) key;
98                     if (keyStr.charAt(0) == '-') {
99                         ascending = false;
100                         key = keyStr.substring(1);
101                     } else if (keyStr.charAt(0) == '+') {
102                         ascending = true;
103                         key = keyStr.substring(1);
104                     }
105                 }
106                 
107                 o1 = map1.get(key);
108                 o2 = map2.get(key);
109             }
110
111             if (o1 == null && o2 == null) {
112                 continue;
113             }
114             
115             int compareResult = 0;
116             if (o1 != null && o2 == null) {
117                 compareResult = -1;
118             }
119             if (o1 == null && o2 != null) {
120                 compareResult = 1;
121             }
122             
123             if (compareResult == 0) {
124                 try {
125                     // the map values in question MUST implement the Comparable interface, if not we'll throw an exception
126
Comparable JavaDoc comp1 = (Comparable JavaDoc) o1;
127                     Comparable JavaDoc comp2 = (Comparable JavaDoc) o2;
128                     compareResult = comp1.compareTo(comp2);
129                 } catch (Exception JavaDoc e) {
130                     String JavaDoc errorMessage = "Error sorting list of Maps: " + e.toString();
131                     Debug.logError(e, errorMessage, module);
132                     throw new RuntimeException JavaDoc(errorMessage);
133                 }
134             }
135
136             // if zero they are equal, so we carry on to the next key to try and find a difference
137
if (compareResult != 0) {
138                 if (ascending) {
139                     return compareResult;
140                 } else {
141                     return -compareResult;
142                 }
143             }
144         }
145         
146         // none of them were different, so they are equal
147
return 0;
148     }
149 }
150
Popular Tags