01: package org.obe.engine.util;
02:
03: import java.util.Comparator;
04:
05: /**
06: * A comparator that can compare any classes, regardless of type or
07: * comparability.
08: *
09: * @author Adrian Price
10: */
11: public class UniversalComparator implements Comparator {
12: public UniversalComparator() {
13: }
14:
15: public int compare(Object o1, Object o2) {
16: if (o1 == o2)
17: return 0;
18: if (o1 == null)
19: return -1;
20: if (o2 == null)
21: return +1;
22: // TODO: improve the type coercion.
23: if (!(o1 instanceof Comparable)) {
24: o1 = o1.toString();
25: o2 = o2.toString();
26: }
27: // TODO: Use a CollatedComparator for string comparisons.
28: // N.B. This will only work if o1 & o2 are already mutually comparable.
29: return ((Comparable) o1).compareTo(o2);
30: }
31: }
|