/* Wotonomy: OpenStep design patterns for pure Java applications. Copyright (C) 2000 Michael Powers This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, see http://www.gnu.org */ package net.wotonomy.datastore; import java.io.Serializable; import java.util.Comparator; import net.wotonomy.foundation.internal.ValueConverter; /** * DefaultComparator exists to compare basic java primitive wrappers, since * these classes don't implement Comparable in jdk 1.1.x. Also uses * ValueConverter to try to match types for comparison. */ public class DefaultComparator implements Comparator, Serializable { public int compare(Object o1, Object o2) { // System.out.println( "compare: " + o1 + " : " + o1.getClass() + " : " + o2 + " : " + o2.getClass() ); /* * if ( ( o1 instanceof Comparable ) && ( o2 instanceof Comparable ) ) { return * ((Comparable)o1).compareTo( o2 ); } */ if ((o1 instanceof Number) && (o2 instanceof Number)) { // TODO: special case for each type would be faster return (int) (((Number) o1).doubleValue() - ((Number) o2).doubleValue()); } if (o1 instanceof StringBuffer) { o1 = o1.toString(); } if (o2 instanceof StringBuffer) { o2 = o2.toString(); } if ((o1 instanceof String) && (o2 instanceof String)) { return ((String) o1).compareTo(((String) o2)); } if ((o1 instanceof Character) && (o2 instanceof Character)) { return (int) ((Character) o1).charValue() - ((Character) o2).charValue(); } if ((o1 instanceof Byte) && (o2 instanceof Byte)) { return (int) ((Byte) o1).byteValue() - ((Byte) o2).byteValue(); } if ((o1 instanceof Boolean) && (o2 instanceof Boolean)) { if (o1.equals(o2)) return 0; // presumably TRUE is greater than FALSE if (o1.equals(Boolean.TRUE)) return 1; return -1; } // handle all NULL cases: NULL is less than anything else. if ((o1 == null) && (o2 == null)) return 0; if ((o1 == null) && (o2 != null)) return -1; if ((o2 == null) && (o1 != null)) return 1; if (o1.getClass() != o2.getClass()) { Object convertedValue; if (!(o2 instanceof String)) // (string should be lowest common demoninator, if possible) { // convert first to second's type convertedValue = ValueConverter.convertObjectToClass(o1, o2.getClass()); if (convertedValue != null) { return compare(convertedValue, o2); } } // convert second to first's type convertedValue = ValueConverter.convertObjectToClass(o2, o1.getClass()); if (convertedValue != null) { return -1 * compare(convertedValue, o1); // reverse result } } // we tried really hard, but these values are incomparable: // we'll consider them equal. return 0; } public boolean equals(Object obj) { return (obj == this); } } /* * $Log$ Revision 1.2 2006/02/19 16:26:19 cgruber Move non-unit-test code to * tests project Fix up code to work with proper imports Fix maven dependencies. * * Revision 1.1 2006/02/16 13:18:56 cgruber Check in all sources in * eclipse-friendly maven-enabled packages. * * Revision 1.1.1.1 2000/12/21 15:47:08 mpowers Contributing wotonomy. * * Revision 1.3 2000/12/20 16:25:36 michael Added log to all files. * * */