-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValue.java
More file actions
386 lines (327 loc) · 12.7 KB
/
Value.java
File metadata and controls
386 lines (327 loc) · 12.7 KB
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
package javaxt.utils;
//******************************************************************************
//** Value Class
//******************************************************************************
/**
* A general purpose wrapper for Objects. The value can be converted into a
* number of Java primitives including strings, integers, doubles, booleans,
* etc.
*
******************************************************************************/
public class Value {
private Object value = null;
//**************************************************************************
//** Constructor
//**************************************************************************
/** Creates a new instance of this class.
*/
public Value(Object value){
this.value = value;
}
//**************************************************************************
//** toObject
//**************************************************************************
/** Returns the object used to instantiate this class.
*/
public Object toObject(){
return value;
}
//**************************************************************************
//** toInteger
//**************************************************************************
/** Returns the value as an integer. Returns a null if there was a problem
* converting the value to an integer or if the value is null.
*/
public Integer toInteger(){
if (value==null) return null;
if (value instanceof Integer) return (Integer) value;
String val = null;
try{
val = prepNumber(value+"");
return Integer.valueOf(val);
}
catch(Exception e){
try{
return (int) Math.round(Double.valueOf(val));
}
catch(Exception ex){
return null;
}
}
}
//**************************************************************************
//** toShort
//**************************************************************************
/** Returns the value as a short. Returns a null if there was a problem
* converting the value to a short or if the value is null.
*/
public Short toShort(){
if (value==null) return null;
if (value instanceof Short) return (Short) value;
String val = null;
try{
val = prepNumber(value+"");
return Short.valueOf(val);
}
catch(Exception e){
try{
return new Integer((int) Math.round(Double.valueOf(val))).shortValue();
}
catch(Exception ex){
return null;
}
}
}
//**************************************************************************
//** toDouble
//**************************************************************************
/** Returns the value as a double. Returns a null if there was a problem
* converting the value to a double or if the value is null.
*/
public Double toDouble(){
if (value==null) return null;
if (value instanceof Double) return (Double) value;
if (value instanceof Integer) return ((Integer) value).doubleValue();
try{
return Double.valueOf(prepNumber(value+""));
}
catch(Exception e){
return null;
}
}
//**************************************************************************
//** toLong
//**************************************************************************
/** Returns the value as a long. Returns a null if there was a problem
* converting the value to a long or if the value is null.
*/
public Long toLong(){
if (value==null) return null;
if (value instanceof Long) return (Long) value;
if (value instanceof Integer) return ((Integer) value).longValue();
String val = null;
try{
val = prepNumber(value+"");
return Long.valueOf(val);
}
catch(Exception e){
try{
return Math.round(Double.valueOf(val));
}
catch(Exception ex){
return null;
}
}
}
//**************************************************************************
//** toBigDecimal
//**************************************************************************
/** Returns the value as a BigDecimal. Returns a null if there was a problem
* converting the value to a BigDecimal or if the value is null.
*/
public java.math.BigDecimal toBigDecimal(){
if (value==null) return null;
if (value instanceof java.math.BigDecimal) return (java.math.BigDecimal) value;
try{
return java.math.BigDecimal.valueOf(toDouble());
}
catch(Exception e){
return null;
}
}
//**************************************************************************
//** toFloat
//**************************************************************************
/** Returns the value as a float. Returns a null if there was a problem
* converting the value to a float or if the value is null.
*/
public Float toFloat(){
if (value==null) return null;
if (value instanceof Float) return (Float) value;
try{
return Float.valueOf(prepNumber(value+""));
}
catch(Exception e){
return null;
}
}
//**************************************************************************
//** toDate
//**************************************************************************
/** Returns the value as a Date. Returns a null if there was a problem
* converting the value to a Date or if the value is null.
*/
public javaxt.utils.Date toDate(){
if (value!=null){
if (value instanceof javaxt.utils.Date){
return (javaxt.utils.Date) value;
}
else if (value instanceof java.sql.Timestamp){
java.sql.Timestamp ts = (java.sql.Timestamp) value;
return new javaxt.utils.Date(ts.getTime());
}
else if (value instanceof java.util.Date){
return new javaxt.utils.Date((java.util.Date) value);
}
else if (value instanceof java.util.Calendar){
return new javaxt.utils.Date((java.util.Calendar) value);
}
else{
try{
return new javaxt.utils.Date(value.toString());
}
catch(Exception e){
}
}
}
return null;
}
//**************************************************************************
//** toByteArray
//**************************************************************************
/** Returns the value as a byte array. Returns a null if the value is null
* or if there was a problem converting the value to a byte array. Note
* that if the underlying object is a String, and if the String appears to
* represent a Base64 encoded byte array, then an attempt is made to decode
* the Base64 String. Otherwise, this method will simply write the value to
* a ByteArrayOutputStream and return the raw bytes.
*/
public byte[] toByteArray(){
if (value==null) return null;
if (value instanceof byte[]) return (byte[]) value;
//Check if string is Base64 encoded
if (value instanceof String){
String data = (String) value;
if (data.startsWith("data:") && data.contains(";base64,")){
String type = data.substring(data.indexOf(":")+1, data.indexOf(";"));
data = data.substring(("data:" + type + ";base64,").length());
byte[] b = Base64.decode(data);
if (b!=null) return b;
}
}
java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
try (java.io.ObjectOutputStream out = new java.io.ObjectOutputStream(bos)){
out.writeObject(value);
out.flush();
bos.close();
return bos.toByteArray();
}
catch (Exception ex) {
return null;
}
}
//**************************************************************************
//** toBoolean
//**************************************************************************
/** Returns the value as a Boolean. Returns null if the value is null or
* cannot be converted to a Boolean.
*/
public Boolean toBoolean(){
if (value==null) return null;
if (value instanceof Boolean) return (Boolean) value;
String value = this.value.toString().toLowerCase().trim();
if (value.equals("true")) return true;
if (value.equals("false")) return false;
if (value.equals("yes")) return true;
if (value.equals("no")) return false;
if (value.equals("y")) return true;
if (value.equals("n")) return false;
if (value.equals("t")) return true;
if (value.equals("f")) return false;
if (value.equals("1")) return true;
if (value.equals("0")) return false;
return null;
}
//**************************************************************************
//** isNumeric
//**************************************************************************
/** Returns true if the value is numeric.
*/
public boolean isNumeric(){
return (toDouble()!=null);
}
//**************************************************************************
//** isArray
//**************************************************************************
/** Returns true is the value is an array.
*/
public boolean isArray(){
return value!=null && value.getClass().isArray();
}
//**************************************************************************
//** isNull
//**************************************************************************
/** Returns true if the value is null.
*/
public boolean isNull(){
return value==null;
}
//**************************************************************************
//** toString
//**************************************************************************
/** Returns a string representation of the object by calling the object's
* native toString() method. Returns a null if the object itself is null.
*/
public String toString(){
if (value==null) return null;
else return value.toString();
}
//**************************************************************************
//** equals
//**************************************************************************
/** Used to compare values. Accepts any object.
*/
public boolean equals(Object obj){
if (obj instanceof Value) obj = ((Value) obj).toObject();
if (obj==null) {
if (value==null) return true;
else return false;
}
else{
if (value==null) return false;
}
//Special case for BigDecimal. BigDecimal objects equal only if they are
//equal in value and scale. Thus 2.0 is not equal to 2.00 when compared
//using the equals() method. The following code removes this ambiguity.
if (!obj.equals(value)){
if (obj.getClass().equals(value.getClass())){
if (obj.getClass().equals(java.math.BigDecimal.class)){
java.math.BigDecimal bd1 = (java.math.BigDecimal) obj;
java.math.BigDecimal bd2 = (java.math.BigDecimal) value;
return bd1.stripTrailingZeros().equals(bd2.stripTrailingZeros());
}
}
}
return obj.equals(value);
}
//**************************************************************************
//** prepNumber
//**************************************************************************
private String prepNumber(String value){
value = value.trim();
//Replace currency or any other unexpected characters at the start of the string
char a = value.charAt(0);
boolean isNumeric = false;
for (char c : chars){
if (c==a){
isNumeric = true;
break;
}
}
if (!isNumeric) value = value.substring(1).trim();
//Replace percentages or any other unexpected characters at the end of the string
a = value.charAt(value.length()-1);
isNumeric = false;
for (char c : chars){
if (c==a){
isNumeric = true;
break;
}
}
if (!isNumeric) value = value.substring(0, value.length()-1).trim();
value = value.replace(",", "");
return value;
}
private static final char[] chars =
new char[]{'-','.','0','1','2','3','4','5','6','7','8','9'};
}