-
Notifications
You must be signed in to change notification settings - Fork 33
/
NumericValue.java
407 lines (359 loc) · 15.1 KB
/
NumericValue.java
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
* Copyright (c) 2000, 2017 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2022 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package com.sun.messaging.jmq.jmsselector;
/**
* Wrapper class for a NumericValue that provides math operations on numbers
*
* This class is a necessary because Java does not support arithmetic operations in java.lang.Number
*/
public class NumericValue {
Number value = null;
String image = null;
int imageType = 0;
private final static int ByteValue = 0;
private final static int ShortValue = 1;
private final static int IntValue = 2;
private final static int FloatValue = 3;
final static int LongValue = 4;
final static int DoubleValue = 5;
private final static int[][] returnTypes = { { ByteValue, ShortValue, IntValue, FloatValue, LongValue, DoubleValue },
{ ShortValue, ShortValue, IntValue, FloatValue, LongValue, DoubleValue }, { IntValue, IntValue, IntValue, FloatValue, LongValue, DoubleValue },
{ FloatValue, FloatValue, FloatValue, FloatValue, FloatValue, DoubleValue },
{ LongValue, LongValue, LongValue, FloatValue, LongValue, DoubleValue },
{ DoubleValue, DoubleValue, DoubleValue, DoubleValue, DoubleValue, DoubleValue } };
/** @throws ClassCastException */
public NumericValue(Object obj) {
// It is OK for a NumericValue to be null. If not null it must be a Number sub-class
if (value != null && !(obj instanceof java.lang.Number)) {
throw new ClassCastException("Cannot make a NumericValue from a " + obj.getClass().getName());
}
if (obj instanceof NumericValue) {
NumericValue n = (NumericValue) obj;
this.value = n.value;
this.image = n.image;
this.imageType = n.imageType;
} else {
this.value = (Number) obj;
}
}
public NumericValue(String image, int imageType) {
this.image = image;
this.imageType = imageType;
this.value = null;
}
public Number getValue() {
if (value == null && image != null) {
switch (imageType) {
case DoubleValue:
value = Double.valueOf(image);
break;
case LongValue:
// XX:JAVA2
value = Long.decode(image);
// XX:JAVA1.1
// value = new Long(image);
break;
default:
}
image = null;
imageType = -1;
}
return value;
}
private int getIndexForType(Number obj) {
int index = -1;
Class objClass = obj.getClass();
if (objClass == Byte.class) {
index = ByteValue;
} else if (objClass == Short.class) {
index = ShortValue;
} else if (objClass == Integer.class) {
index = IntValue;
} else if (objClass == Float.class) {
index = FloatValue;
} else if (objClass == Long.class) {
index = LongValue;
} else if (objClass == Double.class) {
index = DoubleValue;
} else {
System.err.println("Unexpected failure in getIndexForType objClass=" + obj.getClass().getName());
}
return index;
}
int getUnifiedTypeIndex(Number val1, Number val2) {
int index = -1;
if ((val1 != null) && (val2 != null)) {
int index1 = getIndexForType(val1);
int index2 = getIndexForType(val2);
index = returnTypes[index1][index2];
}
return index;
}
private Number convertNumber(Number val, int typeIndex) {
switch (typeIndex) {
case ByteValue:
return Byte.valueOf(val.byteValue());
case ShortValue:
return Short.valueOf(val.shortValue());
case IntValue:
return Integer.valueOf(val.intValue());
case LongValue:
return Long.valueOf(val.longValue());
case DoubleValue:
return Double.valueOf(val.doubleValue());
default:
return null;
}
}
// XX:JAVA2 can be removed in favor of Comparator.compare(obj1, obj2)
public boolean between(NumericValue num1, NumericValue num2) {
boolean result = false;
if (getValue() != null && num1 != null && num2 != null) {
Number value1 = num1.getValue();
Number value2 = num2.getValue();
int typeIndex1 = getUnifiedTypeIndex(value, value1);
int typeIndex2 = getUnifiedTypeIndex(value, value2);
/*
* if (typeIndex1 != -1) { System.err.println("between:num1 is of type" + indexToTypeMap[typeIndex1]); } else {
* System.err.println("between:num1 is undefined"); } if (typeIndex1 != -1) {
* System.err.println("between:num2 is of type" + indexToTypeMap[typeIndex2]); } else {
* System.err.println("between:num2 is undefined"); }
*/
int typeIndex = (typeIndex1 >= typeIndex2 ? typeIndex1 : typeIndex2);
// System.err.println("between:GCType is " + indexToTypeMap[typeIndex]);
Number val = convertNumber(value, typeIndex);
Number val1 = convertNumber(value1, typeIndex);
Number val2 = convertNumber(value2, typeIndex);
// System.err.println("value = " + value + " val1 = " + val1);
// System.err.println("value2 = " + value2 + " val2 = " + val2);
switch (typeIndex) {
case ByteValue:
result = ((val.byteValue() >= val1.byteValue()) && (val.byteValue() <= val2.byteValue())
|| (val.byteValue() <= val1.byteValue()) && (val.byteValue() >= val2.byteValue()));
break;
case ShortValue:
result = ((val.shortValue() >= val1.shortValue()) && (val.shortValue() <= val2.shortValue())
|| (val.shortValue() <= val1.shortValue()) && (val.shortValue() >= val2.shortValue()));
break;
case IntValue:
result = ((val.intValue() >= val1.intValue()) && (val.intValue() <= val2.intValue())
|| (val.intValue() <= val1.intValue()) && (val.intValue() >= val2.intValue()));
break;
case FloatValue:
result = ((val.floatValue() >= val1.floatValue()) && (val.floatValue() <= val2.floatValue())
|| (val.floatValue() <= val1.floatValue()) && (val.floatValue() >= val2.floatValue()));
break;
case LongValue:
result = ((val.longValue() >= val1.longValue()) && (val.longValue() <= val2.longValue())
|| (val.longValue() <= val1.longValue()) && (val.longValue() >= val2.longValue()));
break;
case DoubleValue:
result = ((val.doubleValue() >= val1.doubleValue()) && (val.doubleValue() <= val2.doubleValue())
|| (val.doubleValue() <= val1.doubleValue()) && (val.doubleValue() >= val2.doubleValue()));
break;
default:
// Undefined value
break;
}
}
return result;
}
public Number add(NumericValue num) {
Number result = null;
if (getValue() != null && num != null) {
Number value2 = num.getValue();
int typeIndex = getUnifiedTypeIndex(value, value2);
Number val1 = convertNumber(value, typeIndex);
Number val2 = convertNumber(value2, typeIndex);
switch (typeIndex) {
case ByteValue:
result = Byte.valueOf((byte) (val1.byteValue() + val2.byteValue()));
break;
case ShortValue:
result = Short.valueOf((short) (val1.shortValue() + val2.shortValue()));
break;
case IntValue:
result = Integer.valueOf(val1.intValue() + val2.intValue());
break;
case FloatValue:
result = Float.valueOf(val1.floatValue() + val2.floatValue());
break;
case LongValue:
result = Long.valueOf(val1.longValue() + val2.longValue());
break;
case DoubleValue:
result = Double.valueOf(val1.doubleValue() + val2.doubleValue());
break;
default:
// Undefined value
break;
}
}
return result;
}
public Number subtract(NumericValue num) {
Number result = null;
if (getValue() != null && num != null) {
Number value2 = num.getValue();
int typeIndex = getUnifiedTypeIndex(value, value2);
Number val1 = convertNumber(value, typeIndex);
Number val2 = convertNumber(value2, typeIndex);
switch (typeIndex) {
case ByteValue:
result = Byte.valueOf((byte) (val1.byteValue() - val2.byteValue()));
break;
case ShortValue:
result = Short.valueOf((short) (val1.shortValue() - val2.shortValue()));
break;
case IntValue:
result = Integer.valueOf(val1.intValue() - val2.intValue());
break;
case FloatValue:
result = Float.valueOf(val1.floatValue() - val2.floatValue());
break;
case LongValue:
result = Long.valueOf(val1.longValue() - val2.longValue());
break;
case DoubleValue:
result = Double.valueOf(val1.doubleValue() - val2.doubleValue());
break;
default:
// Undefined value
break;
}
}
return result;
}
public Number multiply(NumericValue num) {
Number result = null;
if (getValue() != null && num != null) {
Number value2 = num.getValue();
int typeIndex = getUnifiedTypeIndex(value, value2);
Number val1 = convertNumber(value, typeIndex);
Number val2 = convertNumber(value2, typeIndex);
switch (typeIndex) {
case ByteValue:
result = Byte.valueOf((byte) (val1.byteValue() * val2.byteValue()));
break;
case ShortValue:
result = Short.valueOf((short) (val1.shortValue() * val2.shortValue()));
break;
case IntValue:
result = Integer.valueOf(val1.intValue() * val2.intValue());
break;
case FloatValue:
result = Float.valueOf(val1.floatValue() * val2.floatValue());
break;
case LongValue:
result = Long.valueOf(val1.longValue() * val2.longValue());
break;
case DoubleValue:
result = Double.valueOf(val1.doubleValue() * val2.doubleValue());
break;
default:
// Undefined value
break;
}
}
return result;
}
public Number divide(NumericValue num) {
Number result = null;
if (getValue() != null && num != null) {
Number value2 = num.getValue();
int typeIndex = getUnifiedTypeIndex(value, value2);
Number val1 = convertNumber(value, typeIndex);
Number val2 = convertNumber(value2, typeIndex);
switch (typeIndex) {
case ByteValue:
result = Byte.valueOf((byte) (val1.byteValue() / val2.byteValue()));
break;
case ShortValue:
result = Short.valueOf((short) (val1.shortValue() / val2.shortValue()));
break;
case IntValue:
result = Integer.valueOf(val1.intValue() / val2.intValue());
break;
case FloatValue:
result = Float.valueOf(val1.floatValue() / val2.floatValue());
break;
case LongValue:
result = Long.valueOf(val1.longValue() / val2.longValue());
break;
case DoubleValue:
result = Double.valueOf(val1.doubleValue() / val2.doubleValue());
break;
default:
// Undefined value
break;
}
}
return result;
}
public Number negate() {
Number result = null;
if (image != null) {
image = "-" + image;
return getValue();
}
if (getValue() != null) {
int typeIndex = getIndexForType(value);
switch (typeIndex) {
case ByteValue:
result = Byte.valueOf((byte) (-value.byteValue()));
break;
case ShortValue:
result = Short.valueOf((short) (-value.shortValue()));
break;
case IntValue:
result = Integer.valueOf(-value.intValue());
break;
case FloatValue:
result = Float.valueOf(-value.floatValue());
break;
case LongValue:
result = Long.valueOf(-value.longValue());
break;
case DoubleValue:
result = Double.valueOf(-value.doubleValue());
break;
default:
// Undefined value
break;
}
}
return result;
}
@Override
public String toString() {
String str = "null";
if (getValue() != null) {
str = value.toString();
}
return str;
}
/*
* public static void main(String[] args) { . NumericValue num1 = new NumericValue(new Float(3.65)); . NumericValue num2
* = new NumericValue(new Long(100L)); . System.err.println(num1 + "+" + num2 + " = " + num1.add(num2)); .
* System.err.println(num2 + "+" + num1 + " = " + num2.add(num1)); . . System.err.println(num1 + "-" + num2 + " = " +
* num1.subtract(num2)); . System.err.println(num2 + "-" + num1 + " = " + num2.subtract(num1)); . .
* System.err.println(num1 + "*" + num2 + " = " + num1.multiply(num2)); . System.err.println(num2 + "*" + num1 + " = " +
* num2.multiply(num1)); . . System.err.println(num1 + "/" + num2 + " = " + num1.divide(num2)); .
* System.err.println(num2 + "/" + num1 + " = " + num2.divide(num1)); . . System.err.println("-" + num1 + " = " +
* num1.negate()); . System.err.println("-" + num2 + " = " + num2.negate()); . . . System.exit(0); . }
*/
}