1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.jexl2;
18
19 import java.lang.reflect.Array;
20 import java.lang.reflect.Field;
21 import java.math.BigDecimal;
22 import java.math.BigInteger;
23 import java.math.MathContext;
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 public class JexlArithmetic {
47
48 protected static final BigDecimal BIGD_DOUBLE_MAX_VALUE = BigDecimal.valueOf(Double.MAX_VALUE);
49
50 protected static final BigDecimal BIGD_DOUBLE_MIN_VALUE = BigDecimal.valueOf(Double.MIN_VALUE);
51
52 protected static final BigInteger BIGI_LONG_MAX_VALUE = BigInteger.valueOf(Long.MAX_VALUE);
53
54 protected static final BigInteger BIGI_LONG_MIN_VALUE = BigInteger.valueOf(Long.MIN_VALUE);
55
56
57
58
59 protected static final int BIGD_SCALE = -1;
60
61
62
63 private volatile boolean strict;
64
65
66
67
68 protected final MathContext mathContext;
69
70
71
72
73 protected final int mathScale;
74
75
76
77
78
79 public JexlArithmetic(boolean lenient) {
80 this(lenient, MathContext.DECIMAL128, BIGD_SCALE);
81 }
82
83
84
85
86
87
88
89
90 public JexlArithmetic(boolean lenient, MathContext bigdContext, int bigdScale) {
91 this.strict = !lenient;
92 this.mathContext = bigdContext;
93 this.mathScale = bigdScale;
94 }
95
96
97
98
99
100
101
102
103
104
105
106
107
108 @Deprecated
109 void setLenient(boolean flag) {
110 this.strict = !flag;
111 }
112
113
114
115
116
117
118 public boolean isLenient() {
119 return !this.strict;
120 }
121
122
123
124
125
126
127 public MathContext getMathContext() {
128 return mathContext;
129 }
130
131
132
133
134
135
136 public int getMathScale() {
137 return mathScale;
138 }
139
140
141
142
143
144
145
146 public BigDecimal roundBigDecimal(final BigDecimal number) {
147 int mscale = getMathScale();
148 if (mscale >= 0) {
149 return number.setScale(mscale, getMathContext().getRoundingMode());
150 } else {
151 return number;
152 }
153 }
154
155
156
157
158
159
160 protected Object controlNullNullOperands() {
161 if (!isLenient()) {
162 throw new ArithmeticException(JexlException.NULL_OPERAND);
163 }
164 return Integer.valueOf(0);
165 }
166
167
168
169
170
171 protected void controlNullOperand() {
172 if (!isLenient()) {
173 throw new ArithmeticException(JexlException.NULL_OPERAND);
174 }
175 }
176
177
178
179
180
181
182
183 protected boolean isFloatingPointType(Object left, Object right) {
184 return left instanceof Float || left instanceof Double || right instanceof Float || right instanceof Double;
185 }
186
187
188
189
190
191
192
193
194 protected boolean isFloatingPointNumber(Object val) {
195 if (val instanceof Float || val instanceof Double) {
196 return true;
197 }
198 if (val instanceof String) {
199 String string = (String) val;
200 return string.indexOf('.') != -1 || string.indexOf('e') != -1 || string.indexOf('E') != -1;
201 }
202 return false;
203 }
204
205
206
207
208
209
210
211 protected boolean isFloatingPoint(final Object o) {
212 return o instanceof Float || o instanceof Double;
213 }
214
215
216
217
218
219
220
221 protected boolean isNumberable(final Object o) {
222 return o instanceof Integer
223 || o instanceof Long
224 || o instanceof Byte
225 || o instanceof Short
226 || o instanceof Character;
227 }
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242 protected Number narrowBigInteger(Object lhs, Object rhs, BigInteger bigi) {
243
244 if (!(lhs instanceof BigInteger || rhs instanceof BigInteger)
245 && bigi.compareTo(BIGI_LONG_MAX_VALUE) <= 0
246 && bigi.compareTo(BIGI_LONG_MIN_VALUE) >= 0) {
247
248 long l = bigi.longValue();
249
250 if (!(lhs instanceof Long || rhs instanceof Long)
251 && l <= Integer.MAX_VALUE
252 && l >= Integer.MIN_VALUE) {
253 return Integer.valueOf((int) l);
254 }
255 return Long.valueOf(l);
256 }
257 return bigi;
258 }
259
260
261
262
263
264
265
266
267
268
269
270 protected Number narrowBigDecimal(Object lhs, Object rhs, BigDecimal bigd) {
271 if (isNumberable(lhs) || isNumberable(rhs)) {
272 try {
273 long l = bigd.longValueExact();
274
275 if (l <= Integer.MAX_VALUE && l >= Integer.MIN_VALUE) {
276 return Integer.valueOf((int) l);
277 } else {
278 return Long.valueOf(l);
279 }
280 } catch (ArithmeticException xa) {
281
282 }
283 }
284 return bigd;
285 }
286
287
288
289
290
291
292
293
294
295
296
297
298 protected Object narrowArrayType(Object[] untyped) {
299 final int size = untyped.length;
300 Class<?> commonClass = null;
301 if (size > 0) {
302 boolean isNumber = true;
303
304 for (int u = 0; u < size && !Object.class.equals(commonClass); ++u) {
305 if (untyped[u] != null) {
306 Class<?> eclass = untyped[u].getClass();
307
308 if (commonClass == null) {
309 commonClass = eclass;
310 isNumber &= Number.class.isAssignableFrom(commonClass);
311 } else if (!commonClass.equals(eclass)) {
312
313 if (isNumber && Number.class.isAssignableFrom(eclass)) {
314 commonClass = Number.class;
315 } else {
316
317 do {
318 eclass = eclass.getSuperclass();
319 if (eclass == null) {
320 commonClass = Object.class;
321 break;
322 }
323 } while (!commonClass.isAssignableFrom(eclass));
324 }
325 }
326 } else {
327 isNumber = false;
328 }
329 }
330
331 if (commonClass != null && !Object.class.equals(commonClass)) {
332
333 if (isNumber) {
334 try {
335 final Field type = commonClass.getField("TYPE");
336 commonClass = (Class<?>) type.get(null);
337 } catch (Exception xany) {
338
339 }
340 }
341
342 Object typed = Array.newInstance(commonClass, size);
343 for (int i = 0; i < size; ++i) {
344 Array.set(typed, i, untyped[i]);
345 }
346 return typed;
347 }
348 }
349 return untyped;
350 }
351
352
353
354
355
356
357
358 protected boolean narrowArguments(Object[] args) {
359 boolean narrowed = false;
360 for (int a = 0; a < args.length; ++a) {
361 Object arg = args[a];
362 if (arg instanceof Number) {
363 Object narg = narrow((Number) arg);
364 if (narg != arg) {
365 narrowed = true;
366 }
367 args[a] = narg;
368 }
369 }
370 return narrowed;
371 }
372
373
374
375
376
377
378
379
380
381
382
383 public Object add(Object left, Object right) {
384 if (left == null && right == null) {
385 return controlNullNullOperands();
386 }
387
388 try {
389
390 if (isFloatingPointNumber(left) || isFloatingPointNumber(right)) {
391 double l = toDouble(left);
392 double r = toDouble(right);
393 return new Double(l + r);
394 }
395
396
397 if (left instanceof BigDecimal || right instanceof BigDecimal) {
398 BigDecimal l = toBigDecimal(left);
399 BigDecimal r = toBigDecimal(right);
400 BigDecimal result = l.add(r, getMathContext());
401 return narrowBigDecimal(left, right, result);
402 }
403
404
405 BigInteger l = toBigInteger(left);
406 BigInteger r = toBigInteger(right);
407 BigInteger result = l.add(r);
408 return narrowBigInteger(left, right, result);
409 } catch (java.lang.NumberFormatException nfe) {
410
411 return toString(left).concat(toString(right));
412 }
413 }
414
415
416
417
418
419
420
421
422 public Object divide(Object left, Object right) {
423 if (left == null && right == null) {
424 return controlNullNullOperands();
425 }
426
427
428 if (isFloatingPointNumber(left) || isFloatingPointNumber(right)) {
429 double l = toDouble(left);
430 double r = toDouble(right);
431 if (r == 0.0) {
432 throw new ArithmeticException("/");
433 }
434 return new Double(l / r);
435 }
436
437
438 if (left instanceof BigDecimal || right instanceof BigDecimal) {
439 BigDecimal l = toBigDecimal(left);
440 BigDecimal r = toBigDecimal(right);
441 if (BigDecimal.ZERO.equals(r)) {
442 throw new ArithmeticException("/");
443 }
444 BigDecimal result = l.divide(r, getMathContext());
445 return narrowBigDecimal(left, right, result);
446 }
447
448
449 BigInteger l = toBigInteger(left);
450 BigInteger r = toBigInteger(right);
451 if (BigInteger.ZERO.equals(r)) {
452 throw new ArithmeticException("/");
453 }
454 BigInteger result = l.divide(r);
455 return narrowBigInteger(left, right, result);
456 }
457
458
459
460
461
462
463
464
465 public Object mod(Object left, Object right) {
466 if (left == null && right == null) {
467 return controlNullNullOperands();
468 }
469
470
471 if (isFloatingPointNumber(left) || isFloatingPointNumber(right)) {
472 double l = toDouble(left);
473 double r = toDouble(right);
474 if (r == 0.0) {
475 throw new ArithmeticException("%");
476 }
477 return new Double(l % r);
478 }
479
480
481 if (left instanceof BigDecimal || right instanceof BigDecimal) {
482 BigDecimal l = toBigDecimal(left);
483 BigDecimal r = toBigDecimal(right);
484 if (BigDecimal.ZERO.equals(r)) {
485 throw new ArithmeticException("%");
486 }
487 BigDecimal remainder = l.remainder(r, getMathContext());
488 return narrowBigDecimal(left, right, remainder);
489 }
490
491
492 BigInteger l = toBigInteger(left);
493 BigInteger r = toBigInteger(right);
494 BigInteger result = l.mod(r);
495 if (BigInteger.ZERO.equals(r)) {
496 throw new ArithmeticException("%");
497 }
498 return narrowBigInteger(left, right, result);
499 }
500
501
502
503
504
505
506
507 public Object multiply(Object left, Object right) {
508 if (left == null && right == null) {
509 return controlNullNullOperands();
510 }
511
512
513 if (isFloatingPointNumber(left) || isFloatingPointNumber(right)) {
514 double l = toDouble(left);
515 double r = toDouble(right);
516 return new Double(l * r);
517 }
518
519
520 if (left instanceof BigDecimal || right instanceof BigDecimal) {
521 BigDecimal l = toBigDecimal(left);
522 BigDecimal r = toBigDecimal(right);
523 BigDecimal result = l.multiply(r, getMathContext());
524 return narrowBigDecimal(left, right, result);
525 }
526
527
528 BigInteger l = toBigInteger(left);
529 BigInteger r = toBigInteger(right);
530 BigInteger result = l.multiply(r);
531 return narrowBigInteger(left, right, result);
532 }
533
534
535
536
537
538
539
540 public Object subtract(Object left, Object right) {
541 if (left == null && right == null) {
542 return controlNullNullOperands();
543 }
544
545
546 if (isFloatingPointNumber(left) || isFloatingPointNumber(right)) {
547 double l = toDouble(left);
548 double r = toDouble(right);
549 return new Double(l - r);
550 }
551
552
553 if (left instanceof BigDecimal || right instanceof BigDecimal) {
554 BigDecimal l = toBigDecimal(left);
555 BigDecimal r = toBigDecimal(right);
556 BigDecimal result = l.subtract(r, getMathContext());
557 return narrowBigDecimal(left, right, result);
558 }
559
560
561 BigInteger l = toBigInteger(left);
562 BigInteger r = toBigInteger(right);
563 BigInteger result = l.subtract(r);
564 return narrowBigInteger(left, right, result);
565 }
566
567
568
569
570
571
572
573 public Object negate(Object val) {
574 if (val instanceof Integer) {
575 int valueAsInt = ((Integer) val).intValue();
576 return Integer.valueOf(-valueAsInt);
577 } else if (val instanceof Double) {
578 double valueAsDouble = ((Double) val).doubleValue();
579 return new Double(-valueAsDouble);
580 } else if (val instanceof Long) {
581 long valueAsLong = -((Long) val).longValue();
582 return Long.valueOf(valueAsLong);
583 } else if (val instanceof BigDecimal) {
584 BigDecimal valueAsBigD = (BigDecimal) val;
585 return valueAsBigD.negate();
586 } else if (val instanceof BigInteger) {
587 BigInteger valueAsBigI = (BigInteger) val;
588 return valueAsBigI.negate();
589 } else if (val instanceof Float) {
590 float valueAsFloat = ((Float) val).floatValue();
591 return new Float(-valueAsFloat);
592 } else if (val instanceof Short) {
593 short valueAsShort = ((Short) val).shortValue();
594 return Short.valueOf((short) -valueAsShort);
595 } else if (val instanceof Byte) {
596 byte valueAsByte = ((Byte) val).byteValue();
597 return Byte.valueOf((byte) -valueAsByte);
598 } else if (val instanceof Boolean) {
599 return ((Boolean) val).booleanValue() ? Boolean.FALSE : Boolean.TRUE;
600 }
601 throw new ArithmeticException("Object negation:(" + val + ")");
602 }
603
604
605
606
607
608
609
610
611
612 public boolean matches(Object left, Object right) {
613 if (left == null && right == null) {
614
615 return true;
616 }
617 if (left == null || right == null) {
618
619 return false;
620 }
621 final String arg = left.toString();
622 if (right instanceof java.util.regex.Pattern) {
623 return ((java.util.regex.Pattern) right).matcher(arg).matches();
624 } else {
625 return arg.matches(right.toString());
626 }
627 }
628
629
630
631
632
633
634
635
636 public Object bitwiseAnd(Object left, Object right) {
637 long l = toLong(left);
638 long r = toLong(right);
639 return Long.valueOf(l & r);
640 }
641
642
643
644
645
646
647
648
649 public Object bitwiseOr(Object left, Object right) {
650 long l = toLong(left);
651 long r = toLong(right);
652 return Long.valueOf(l | r);
653 }
654
655
656
657
658
659
660
661
662 public Object bitwiseXor(Object left, Object right) {
663 long l = toLong(left);
664 long r = toLong(right);
665 return Long.valueOf(l ^ r);
666 }
667
668
669
670
671
672
673
674 public Object bitwiseComplement(Object val) {
675 long l = toLong(val);
676 return Long.valueOf(~l);
677 }
678
679
680
681
682
683
684
685
686
687
688 protected int compare(Object left, Object right, String operator) {
689 if (left != null && right != null) {
690 if (left instanceof BigDecimal || right instanceof BigDecimal) {
691 BigDecimal l = toBigDecimal(left);
692 BigDecimal r = toBigDecimal(right);
693 return l.compareTo(r);
694 } else if (left instanceof BigInteger || right instanceof BigInteger) {
695 BigInteger l = toBigInteger(left);
696 BigInteger r = toBigInteger(right);
697 return l.compareTo(r);
698 } else if (isFloatingPoint(left) || isFloatingPoint(right)) {
699 double lhs = toDouble(left);
700 double rhs = toDouble(right);
701 if (Double.isNaN(lhs)) {
702 if (Double.isNaN(rhs)) {
703 return 0;
704 } else {
705 return -1;
706 }
707 } else if (Double.isNaN(rhs)) {
708
709 return +1;
710 } else if (lhs < rhs) {
711 return -1;
712 } else if (lhs > rhs) {
713 return +1;
714 } else {
715 return 0;
716 }
717 } else if (isNumberable(left) || isNumberable(right)) {
718 long lhs = toLong(left);
719 long rhs = toLong(right);
720 if (lhs < rhs) {
721 return -1;
722 } else if (lhs > rhs) {
723 return +1;
724 } else {
725 return 0;
726 }
727 } else if (left instanceof String || right instanceof String) {
728 return toString(left).compareTo(toString(right));
729 } else if ("==".equals(operator)) {
730 return left.equals(right) ? 0 : -1;
731 } else if (left instanceof Comparable<?>) {
732 @SuppressWarnings("unchecked")
733 final Comparable<Object> comparable = (Comparable<Object>) left;
734 return comparable.compareTo(right);
735 } else if (right instanceof Comparable<?>) {
736 @SuppressWarnings("unchecked")
737 final Comparable<Object> comparable = (Comparable<Object>) right;
738 return comparable.compareTo(left);
739 }
740 }
741 throw new ArithmeticException("Object comparison:(" + left + " " + operator + " " + right + ")");
742 }
743
744
745
746
747
748
749
750
751 public boolean equals(Object left, Object right) {
752 if (left == right) {
753 return true;
754 } else if (left == null || right == null) {
755 return false;
756 } else if (left instanceof Boolean || right instanceof Boolean) {
757 return toBoolean(left) == toBoolean(right);
758 } else {
759 return compare(left, right, "==") == 0;
760 }
761 }
762
763
764
765
766
767
768
769
770 public boolean lessThan(Object left, Object right) {
771 if ((left == right) || (left == null) || (right == null)) {
772 return false;
773 } else {
774 return compare(left, right, "<") < 0;
775 }
776
777 }
778
779
780
781
782
783
784
785
786 public boolean greaterThan(Object left, Object right) {
787 if ((left == right) || left == null || right == null) {
788 return false;
789 } else {
790 return compare(left, right, ">") > 0;
791 }
792 }
793
794
795
796
797
798
799
800
801 public boolean lessThanOrEqual(Object left, Object right) {
802 if (left == right) {
803 return true;
804 } else if (left == null || right == null) {
805 return false;
806 } else {
807 return compare(left, right, "<=") <= 0;
808 }
809 }
810
811
812
813
814
815
816
817
818 public boolean greaterThanOrEqual(Object left, Object right) {
819 if (left == right) {
820 return true;
821 } else if (left == null || right == null) {
822 return false;
823 } else {
824 return compare(left, right, ">=") >= 0;
825 }
826 }
827
828
829
830
831
832
833
834 public boolean toBoolean(Object val) {
835 if (val == null) {
836 controlNullOperand();
837 return false;
838 } else if (val instanceof Boolean) {
839 return ((Boolean) val).booleanValue();
840 } else if (val instanceof Number) {
841 double number = toDouble(val);
842 return !Double.isNaN(number) && number != 0.d;
843 } else if (val instanceof String) {
844 String strval = val.toString();
845 return strval.length() > 0 && !"false".equals(strval);
846 }
847
848 return false;
849 }
850
851
852
853
854
855
856
857 public int toInteger(Object val) {
858 if (val == null) {
859 controlNullOperand();
860 return 0;
861 } else if (val instanceof Double) {
862 if (!Double.isNaN(((Double) val).doubleValue())) {
863 return 0;
864 } else {
865 return ((Double) val).intValue();
866 }
867 } else if (val instanceof Number) {
868 return ((Number) val).intValue();
869 } else if (val instanceof String) {
870 if ("".equals(val)) {
871 return 0;
872 }
873 return Integer.parseInt((String) val);
874 } else if (val instanceof Boolean) {
875 return ((Boolean) val).booleanValue() ? 1 : 0;
876 } else if (val instanceof Character) {
877 return ((Character) val).charValue();
878 }
879
880 throw new ArithmeticException("Integer coercion: "
881 + val.getClass().getName() + ":(" + val + ")");
882 }
883
884
885
886
887
888
889
890 public long toLong(Object val) {
891 if (val == null) {
892 controlNullOperand();
893 return 0L;
894 } else if (val instanceof Double) {
895 if (!Double.isNaN(((Double) val).doubleValue())) {
896 return 0;
897 } else {
898 return ((Double) val).longValue();
899 }
900 } else if (val instanceof Number) {
901 return ((Number) val).longValue();
902 } else if (val instanceof String) {
903 if ("".equals(val)) {
904 return 0;
905 } else {
906 return Long.parseLong((String) val);
907 }
908 } else if (val instanceof Boolean) {
909 return ((Boolean) val).booleanValue() ? 1L : 0L;
910 } else if (val instanceof Character) {
911 return ((Character) val).charValue();
912 }
913
914 throw new ArithmeticException("Long coercion: "
915 + val.getClass().getName() + ":(" + val + ")");
916 }
917
918
919
920
921
922
923
924
925 public BigInteger toBigInteger(Object val) {
926 if (val == null) {
927 controlNullOperand();
928 return BigInteger.ZERO;
929 } else if (val instanceof BigInteger) {
930 return (BigInteger) val;
931 } else if (val instanceof Double) {
932 if (!Double.isNaN(((Double) val).doubleValue())) {
933 return new BigInteger(val.toString());
934 } else {
935 return BigInteger.ZERO;
936 }
937 } else if (val instanceof Number) {
938 return new BigInteger(val.toString());
939 } else if (val instanceof String) {
940 String string = (String) val;
941 if ("".equals(string.trim())) {
942 return BigInteger.ZERO;
943 } else {
944 return new BigInteger(string);
945 }
946 } else if (val instanceof Character) {
947 int i = ((Character) val).charValue();
948 return BigInteger.valueOf(i);
949 }
950
951 throw new ArithmeticException("BigInteger coercion: "
952 + val.getClass().getName() + ":(" + val + ")");
953 }
954
955
956
957
958
959
960
961
962 public BigDecimal toBigDecimal(Object val) {
963 if (val instanceof BigDecimal) {
964 return roundBigDecimal((BigDecimal) val);
965 } else if (val == null) {
966 controlNullOperand();
967 return BigDecimal.ZERO;
968 } else if (val instanceof String) {
969 String string = ((String) val).trim();
970 if ("".equals(string)) {
971 return BigDecimal.ZERO;
972 }
973 return roundBigDecimal(new BigDecimal(string, getMathContext()));
974 } else if (val instanceof Double) {
975 if (!Double.isNaN(((Double) val).doubleValue())) {
976 return roundBigDecimal(new BigDecimal(val.toString(), getMathContext()));
977 } else {
978 return BigDecimal.ZERO;
979 }
980 } else if (val instanceof Number) {
981 return roundBigDecimal(new BigDecimal(val.toString(), getMathContext()));
982 } else if (val instanceof Character) {
983 int i = ((Character) val).charValue();
984 return new BigDecimal(i);
985 }
986
987 throw new ArithmeticException("BigDecimal coercion: "
988 + val.getClass().getName() + ":(" + val + ")");
989 }
990
991
992
993
994
995
996
997
998 public double toDouble(Object val) {
999 if (val == null) {
1000 controlNullOperand();
1001 return 0;
1002 } else if (val instanceof Double) {
1003 return ((Double) val).doubleValue();
1004 } else if (val instanceof Number) {
1005
1006
1007 return Double.parseDouble(String.valueOf(val));
1008 } else if (val instanceof Boolean) {
1009 return ((Boolean) val).booleanValue() ? 1. : 0.;
1010 } else if (val instanceof String) {
1011 String string = ((String) val).trim();
1012 if ("".equals(string)) {
1013 return Double.NaN;
1014 } else {
1015
1016 return Double.parseDouble(string);
1017 }
1018 } else if (val instanceof Character) {
1019 int i = ((Character) val).charValue();
1020 return i;
1021 }
1022
1023 throw new ArithmeticException("Double coercion: "
1024 + val.getClass().getName() + ":(" + val + ")");
1025 }
1026
1027
1028
1029
1030
1031
1032
1033
1034 public String toString(Object val) {
1035 if (val == null) {
1036 controlNullOperand();
1037 return "";
1038 } else if (val instanceof Double) {
1039 Double dval = (Double) val;
1040 if (Double.isNaN(dval.doubleValue())) {
1041 return "";
1042 } else {
1043 return dval.toString();
1044 }
1045 } else {
1046 return val.toString();
1047 }
1048 }
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060 public Number narrow(Number original) {
1061 return narrowNumber(original, null);
1062 }
1063
1064
1065
1066
1067
1068
1069
1070
1071 protected boolean narrowAccept(Class<?> narrow, Class<?> source) {
1072 return narrow == null || narrow.equals(source);
1073 }
1074
1075
1076
1077
1078
1079
1080
1081
1082 protected Number narrowNumber(Number original, Class<?> narrow) {
1083 if (original == null) {
1084 return original;
1085 }
1086 Number result = original;
1087 if (original instanceof BigDecimal) {
1088 BigDecimal bigd = (BigDecimal) original;
1089
1090 if (bigd.compareTo(BIGD_DOUBLE_MAX_VALUE) > 0) {
1091 return original;
1092 } else {
1093 try {
1094 long l = bigd.longValueExact();
1095
1096 if (narrowAccept(narrow, Integer.class)
1097 && l <= Integer.MAX_VALUE
1098 && l >= Integer.MIN_VALUE) {
1099 return Integer.valueOf((int) l);
1100 } else if (narrowAccept(narrow, Long.class)) {
1101 return Long.valueOf(l);
1102 }
1103 } catch (ArithmeticException xa) {
1104
1105 }
1106 }
1107 }
1108 if (original instanceof Double || original instanceof Float || original instanceof BigDecimal) {
1109 double value = original.doubleValue();
1110 if (narrowAccept(narrow, Float.class)
1111 && value <= Float.MAX_VALUE
1112 && value >= Float.MIN_VALUE) {
1113 result = Float.valueOf(result.floatValue());
1114 }
1115
1116 } else {
1117 if (original instanceof BigInteger) {
1118 BigInteger bigi = (BigInteger) original;
1119
1120 if (bigi.compareTo(BIGI_LONG_MAX_VALUE) > 0
1121 || bigi.compareTo(BIGI_LONG_MIN_VALUE) < 0) {
1122 return original;
1123 }
1124 }
1125 long value = original.longValue();
1126 if (narrowAccept(narrow, Byte.class)
1127 && value <= Byte.MAX_VALUE
1128 && value >= Byte.MIN_VALUE) {
1129
1130 result = Byte.valueOf((byte) value);
1131 } else if (narrowAccept(narrow, Short.class)
1132 && value <= Short.MAX_VALUE
1133 && value >= Short.MIN_VALUE) {
1134 result = Short.valueOf((short) value);
1135 } else if (narrowAccept(narrow, Integer.class)
1136 && value <= Integer.MAX_VALUE
1137 && value >= Integer.MIN_VALUE) {
1138 result = Integer.valueOf((int) value);
1139 }
1140
1141 }
1142 return result;
1143 }
1144 }