1:
31: package ;
32:
33: import ;
34:
35: import ;
36: import ;
37:
38:
43: public class DefaultComparator implements ExtendedComparator
44: {
45: private FormulaContext context;
46:
47: public static final Integer LESS = new Integer(-1);
48:
49: public static final Integer EQUAL = new Integer(0);
50:
51: private static final Integer MORE = new Integer(1);
52:
53: public DefaultComparator()
54: {
55: }
56:
57: public void inititalize(final FormulaContext context)
58: {
59: this.context = context;
60: }
61:
62: public boolean isEqual(final Type type1, final Object value1,
63: final Type type2, final Object value2)
64: {
65:
66:
67:
68: final TypeRegistry typeRegistry = context.getTypeRegistry();
69: try
70: {
71: final Number number1 = typeRegistry.convertToNumber(type1, value1);
72: final Number number2 = typeRegistry.convertToNumber(type2, value2);
73: final BigDecimal bd1 = new BigDecimal(number1.toString());
74: final BigDecimal bd2 = new BigDecimal(number2.toString());
75: if (bd1.signum() != bd2.signum())
76: {
77: return false;
78: }
79:
80: final BigDecimal result = bd1.subtract(bd2);
81: return (result.signum() == 0);
82: }
83: catch (TypeConversionException nfe)
84: {
85:
86: }
87:
88: if (type1.isFlagSet(Type.TEXT_TYPE) || type2.isFlagSet(Type.TEXT_TYPE))
89: {
90: String text1 = null;
91: String text2 = null;
92: try
93: {
94:
95: text1 = typeRegistry.convertToText(type1, value1);
96: text2 = typeRegistry.convertToText(type2, value2);
97: }
98: catch (TypeConversionException nfe)
99: {
100:
101: }
102:
103: if (text1 == null && text2 == null)
104: {
105: return true;
106: }
107: if (text1 == null || text2 == null)
108: {
109: return false;
110: }
111: return ObjectUtilities.equal(text1, text2);
112:
113: }
114:
115:
116: return (ObjectUtilities.equal(value1, value2));
117: }
118:
119:
129: public Integer compare(final Type type1, final Object value1,
130: final Type type2, final Object value2)
131: {
132:
133:
134: if (value1 == null && value2 == null)
135: {
136: return DefaultComparator.EQUAL;
137: }
138: if (value1 == null)
139: {
140: return DefaultComparator.LESS;
141: }
142: if (value2 == null)
143: {
144: return DefaultComparator.MORE;
145: }
146:
147:
148:
149:
150: if (type1.isFlagSet(Type.SCALAR_TYPE) && type2.isFlagSet(Type.SCALAR_TYPE))
151: {
152:
153: if (value1 instanceof Comparable && value2 instanceof Comparable)
154: {
155: final Comparable c1 = (Comparable) value1;
156: try
157: {
158: final int result = c1.compareTo(value2);
159: if (result == 0)
160: {
161: return DefaultComparator.EQUAL;
162: }
163: else if (result > 0)
164: {
165: return DefaultComparator.MORE;
166: }
167: else
168: {
169: return DefaultComparator.LESS;
170: }
171: }
172: catch (Exception e)
173: {
174:
175: }
176: }
177: }
178:
179:
180: final TypeRegistry typeRegistry = context.getTypeRegistry();
181: try
182: {
183: final Number number1 = typeRegistry.convertToNumber(type1, value1);
184: final Number number2 = typeRegistry.convertToNumber(type2, value2);
185: final BigDecimal bd1 = new BigDecimal(number1.toString());
186: final BigDecimal bd2 = new BigDecimal(number2.toString());
187:
188: if (bd1.signum() != bd2.signum())
189: {
190: if (bd1.signum() < 0)
191: {
192: return DefaultComparator.LESS;
193: }
194: else if (bd1.signum() > 0)
195: {
196: return DefaultComparator.MORE;
197: }
198: }
199:
200: final BigDecimal result = bd1.subtract(bd2);
201: if (result.signum() == 0)
202: {
203: return DefaultComparator.EQUAL;
204: }
205: if (result.signum() > 0)
206: {
207: return DefaultComparator.MORE;
208: }
209: return DefaultComparator.LESS;
210: }
211: catch (TypeConversionException nfe)
212: {
213:
214: }
215:
216:
217:
218: String text1 = null;
219: String text2 = null;
220: try
221: {
222: text1 = typeRegistry.convertToText(type1, value1);
223: text2 = typeRegistry.convertToText(type2, value2);
224: }
225: catch (TypeConversionException e)
226: {
227:
228: }
229:
230: if (text1 == null && text2 == null)
231: {
232: return DefaultComparator.EQUAL;
233: }
234: if (text1 == null)
235: {
236: return DefaultComparator.LESS;
237: }
238: if (text2 == null)
239: {
240: return DefaultComparator.MORE;
241: }
242:
243: final int result = text1.compareTo(text2);
244: if (result == 0)
245: {
246: return DefaultComparator.EQUAL;
247: }
248: else if (result > 0)
249: {
250: return DefaultComparator.MORE;
251: }
252: else
253: {
254: return DefaultComparator.LESS;
255: }
256: }
257: }