summaryrefslogtreecommitdiff
path: root/projects/net.wotonomy.datastore/src/main/java/net/wotonomy/datastore/DefaultComparator.java
blob: dc49737aa145f2a98e79d6caa357d82e61d69847 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
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.
 *
 *
 */