1 /*
2 Wotonomy: OpenStep design patterns for pure Java applications.
3 Copyright (C) 2000 Michael Powers
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, see http://www.gnu.org
17 */
18
19 package net.wotonomy.datastore;
20
21 import java.io.Serializable;
22 import java.util.Comparator;
23
24 import net.wotonomy.foundation.internal.ValueConverter;
25
26 /***
27 * DefaultComparator exists to compare basic java
28 * primitive wrappers, since these classes don't
29 * implement Comparable in jdk 1.1.x. Also uses
30 * ValueConverter to try to match types for comparison.
31 */
32 public class DefaultComparator implements Comparator, Serializable
33 {
34 public int compare(Object o1, Object o2)
35 {
36 // System.out.println( "compare: " + o1 + " : " + o1.getClass() + " : " + o2 + " : " + o2.getClass() );
37 /*
38 if ( ( o1 instanceof Comparable ) && ( o2 instanceof Comparable ) )
39 {
40 return ((Comparable)o1).compareTo( o2 );
41 }
42 */
43 if ( ( o1 instanceof Number ) && ( o2 instanceof Number ) )
44 {
45 // TODO: special case for each type would be faster
46 return (int)
47 ( ((Number)o1).doubleValue() - ((Number)o2).doubleValue() );
48 }
49
50 if ( o1 instanceof StringBuffer )
51 {
52 o1 = o1.toString();
53 }
54 if ( o2 instanceof StringBuffer )
55 {
56 o2 = o2.toString();
57 }
58
59 if ( ( o1 instanceof String ) && ( o2 instanceof String ) )
60 {
61 return ((String)o1).compareTo( ((String)o2) );
62 }
63
64 if ( ( o1 instanceof Character ) && ( o2 instanceof Character ) )
65 {
66 return (int)
67 ((Character)o1).charValue() - ((Character)o2).charValue();
68 }
69
70 if ( ( o1 instanceof Byte ) && ( o2 instanceof Byte ) )
71 {
72 return (int)
73 ((Byte)o1).byteValue() - ((Byte)o2).byteValue();
74 }
75
76 if ( ( o1 instanceof Boolean ) && ( o2 instanceof Boolean ) )
77 {
78 if ( o1.equals( o2 ) ) return 0;
79
80 // presumably TRUE is greater than FALSE
81 if ( o1.equals( Boolean.TRUE ) ) return 1;
82 return -1;
83 }
84
85 // handle all NULL cases: NULL is less than anything else.
86 if ( ( o1 == null ) && ( o2 == null ) ) return 0;
87 if ( ( o1 == null ) && ( o2 != null ) ) return -1;
88 if ( ( o2 == null ) && ( o1 != null ) ) return 1;
89
90 if ( o1.getClass() != o2.getClass() )
91 {
92 Object convertedValue;
93
94 if ( ! ( o2 instanceof String ) )
95 // (string should be lowest common demoninator, if possible)
96 {
97 // convert first to second's type
98 convertedValue =
99 ValueConverter.convertObjectToClass( o1, o2.getClass() );
100 if ( convertedValue != null )
101 {
102 return compare( convertedValue, o2 );
103 }
104 }
105
106 // convert second to first's type
107 convertedValue =
108 ValueConverter.convertObjectToClass( o2, o1.getClass() );
109 if ( convertedValue != null )
110 {
111 return -1 * compare( convertedValue, o1 ); // reverse result
112 }
113 }
114
115 // we tried really hard, but these values are incomparable:
116 // we'll consider them equal.
117 return 0;
118 }
119
120 public boolean equals(Object obj)
121 {
122 return (obj == this);
123 }
124 }
125
126 /*
127 * $Log$
128 * Revision 1.2 2006/02/19 16:26:19 cgruber
129 * Move non-unit-test code to tests project
130 * Fix up code to work with proper imports
131 * Fix maven dependencies.
132 *
133 * Revision 1.1 2006/02/16 13:18:56 cgruber
134 * Check in all sources in eclipse-friendly maven-enabled packages.
135 *
136 * Revision 1.1.1.1 2000/12/21 15:47:08 mpowers
137 * Contributing wotonomy.
138 *
139 * Revision 1.3 2000/12/20 16:25:36 michael
140 * Added log to all files.
141 *
142 *
143 */
144
145