1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
37
38
39
40
41
42
43 if ( ( o1 instanceof Number ) && ( o2 instanceof Number ) )
44 {
45
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
81 if ( o1.equals( Boolean.TRUE ) ) return 1;
82 return -1;
83 }
84
85
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
96 {
97
98 convertedValue =
99 ValueConverter.convertObjectToClass( o1, o2.getClass() );
100 if ( convertedValue != null )
101 {
102 return compare( convertedValue, o2 );
103 }
104 }
105
106
107 convertedValue =
108 ValueConverter.convertObjectToClass( o2, o1.getClass() );
109 if ( convertedValue != null )
110 {
111 return -1 * compare( convertedValue, o1 );
112 }
113 }
114
115
116
117 return 0;
118 }
119
120 public boolean equals(Object obj)
121 {
122 return (obj == this);
123 }
124 }
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145