001: /*
002: * $Id: MapComparator.java,v 1.2 2003/11/30 18:02:16 jonesde Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.base.util;
025:
026: import java.sql.Timestamp;
027: import java.util.Comparator;
028: import java.util.Iterator;
029: import java.util.List;
030: import java.util.Map;
031:
032: /**
033: * MapComparator.java
034: *
035: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
036: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
037: * @version $Revision: 1.2 $
038: * @since 2.0
039: */
040: public class MapComparator implements Comparator {
041:
042: public static final String module = MapComparator.class.getName();
043:
044: private List keys;
045:
046: /**
047: * Method MapComparator.
048: * @param keys List of Map keys to sort on
049: */
050: public MapComparator(List keys) {
051: this .keys = keys;
052: }
053:
054: /**
055: * @see java.lang.Object#equals(java.lang.Object)
056: */
057: public boolean equals(Object obj) {
058: return obj.equals(this );
059: }
060:
061: /**
062: * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
063: */
064: public int compare(Object obj1, Object obj2) {
065: Map map1, map2;
066: try {
067: map1 = (Map) obj1;
068: map2 = (Map) obj2;
069: } catch (ClassCastException e) {
070: throw new IllegalArgumentException(
071: "Objects not from the Map interface");
072: }
073:
074: if (keys == null || keys.size() < 1) {
075: throw new IllegalArgumentException("No sort fields defined");
076: }
077:
078: Iterator i = keys.iterator();
079: while (i.hasNext()) {
080: // if false will be descending, ie reverse order
081: boolean ascending = true;
082: Object key = i.next();
083: if (key instanceof String) {
084: String keyStr = (String) key;
085: if (keyStr.charAt(0) == '-') {
086: ascending = false;
087: key = keyStr.substring(1);
088: } else if (keyStr.charAt(0) == '+') {
089: ascending = true;
090: key = keyStr.substring(1);
091: }
092: }
093:
094: Object o1 = map1.get(key);
095: Object o2 = map2.get(key);
096: if (o1 == null && o2 == null) {
097: continue;
098: }
099:
100: int compareResult = 0;
101: if (o1 != null && o2 == null) {
102: compareResult = -1;
103: }
104: if (o1 == null && o2 != null) {
105: compareResult = 1;
106: }
107:
108: if (compareResult == 0) {
109: try {
110: // the map values in question MUST implement the Comparable interface, if not we'll throw an exception
111: Comparable comp1 = (Comparable) o1;
112: Comparable comp2 = (Comparable) o2;
113: compareResult = comp1.compareTo(comp2);
114: } catch (Exception e) {
115: String errorMessage = "Error sorting list of Maps: "
116: + e.toString();
117: Debug.logError(e, errorMessage, module);
118: throw new RuntimeException(errorMessage);
119: }
120: }
121:
122: // if zero they are equal, so we carry on to the next key to try and find a difference
123: if (compareResult != 0) {
124: if (ascending) {
125: return compareResult;
126: } else {
127: return -compareResult;
128: }
129: }
130: }
131:
132: // none of them were different, so they are equal
133: return 0;
134: }
135: }
|