24
24
import java .util .Optional ;
25
25
26
26
import org .libj .lang .Classes ;
27
+ import org .libj .lang .Numbers ;
27
28
import org .libj .lang .ParseException ;
28
29
import org .libj .math .SafeMath ;
29
30
@@ -74,33 +75,62 @@ static Object createValid(final Class<?> type, final String decode, final String
74
75
}
75
76
76
77
private static Double makeValid (final Range range ) {
78
+ // if (range != null && range.toString().equals("[10000000000,]"))
79
+ // System.err.println();
80
+ // System.err.println("Range: " + range);
77
81
if (range == null )
78
82
return null ;
79
83
80
- if (range .getMax () == null ) {
81
- final double x = range .getMin ().doubleValue () + 1d ;
82
- return x ;
83
- }
84
+ if (range .getMax () == null )
85
+ return ensureHasFraction (ensureNotTooBig (range .getMin ().doubleValue ()) + 1d );
84
86
85
87
if (range .getMin () == null )
86
- return range .getMax ().doubleValue () - 1d ;
88
+ return ensureHasFraction ( ensureNotTooBig ( range .getMax ().doubleValue ()) - 1d ) ;
87
89
88
- return (( range .getMax ().doubleValue () - range .getMin ().doubleValue ()) * random .nextDouble ()) + range .getMin ().doubleValue ();
90
+ return ensureHasFraction ((( range .getMax ().doubleValue () - range .getMin ().doubleValue ()) * random .nextDouble ()) + range .getMin ().doubleValue () );
89
91
}
90
92
91
93
private static Double makeInvalid (final Range range ) {
92
- return range .getMin () != null ? range .getMin ().doubleValue () - 1d : range .getMax ().doubleValue () + 1d ;
94
+ double ensureHasFraction = range .getMin () != null ? range .getMin ().doubleValue () - 1d : ensureNotTooBig (range .getMax ().doubleValue ()) + 1d ;
95
+ // System.err.println(ensureHasFraction);
96
+ return ensureHasFraction ;
93
97
}
94
98
95
99
private static Number setScale (final Double value , final int scale ) {
96
- return scale == Integer .MAX_VALUE ? value : scale == 0 ? value .longValue () : SafeMath .round (value , scale , RoundingMode .FLOOR );
100
+ return scale == Integer .MAX_VALUE || scale == Integer . MIN_VALUE ? value : scale == 0 ? value .longValue () : SafeMath .round (value , scale , RoundingMode .FLOOR );
97
101
}
98
102
99
103
private static Number setScale (final BigDecimal value , final int scale ) {
100
- return scale == Integer .MAX_VALUE ? value : scale == 0 ? value .longValue () : value .setScale (scale , RoundingMode .FLOOR );
104
+ return scale == Integer .MAX_VALUE || scale == Integer .MIN_VALUE ? value : scale == 0 ? value .longValue () : value .setScale (scale , RoundingMode .FLOOR );
105
+ }
106
+
107
+ private static float ensureNotTooBig (final float value ) {
108
+ return value % 1000000000 ;
109
+ }
110
+
111
+ private static double ensureNotTooBig (final double value ) {
112
+ return value % 1000000000 ;
113
+ }
114
+
115
+ private static float ensureHasFraction (final float value ) {
116
+ return ensureHasFraction (Numbers .isWhole (value ) ? value + Math .ulp (value ) : value );
117
+ }
118
+
119
+ private static double ensureHasFraction (final double value ) {
120
+ return Numbers .isWhole (value ) ? value + Math .ulp (value ) : value ;
101
121
}
102
122
103
123
private static Object toProperForm (final Class <?> type , final String decode , final int scale , final Double value ) {
124
+ final Object properForm2 = toProperForm2 (type , decode , scale , value );
125
+ if (properForm2 instanceof Number && ((Number )properForm2 ).doubleValue () > 100000 ) {
126
+ toProperForm2 (type , decode , scale , value );
127
+ // System.err.println("Proper form: " + properForm2);
128
+ }
129
+
130
+ return properForm2 ;
131
+ }
132
+
133
+ private static Object toProperForm2 (final Class <?> type , final String decode , final int scale , final Double value ) {
104
134
final Number result ;
105
135
if (BigInteger .class .isAssignableFrom (type ))
106
136
result = BigInteger .valueOf (value == null ? random .nextLong () : value .longValue ());
@@ -113,11 +143,11 @@ else if (type == Short.class || type == short.class)
113
143
else if (type == Byte .class || type == byte .class )
114
144
result = value == null ? (byte )random .nextInt () : value .byteValue ();
115
145
else if (type == Float .class || type == float .class )
116
- result = value == null ? random .nextFloat () * random .nextInt () : value .floatValue ();
146
+ result = ensureHasFraction ( value == null ? ensureNotTooBig ( random .nextFloat () * random .nextInt ()) : value .floatValue () );
117
147
else if (BigDecimal .class .isAssignableFrom (type ))
118
- result = setScale (value == null ? BigDecimal .valueOf (random .nextDouble () * random .nextLong ()) : BigDecimal .valueOf (value ), scale );
148
+ result = setScale (value == null ? BigDecimal .valueOf (ensureHasFraction ( ensureNotTooBig ( random .nextDouble () * random .nextLong ()))) : BigDecimal .valueOf (ensureHasFraction ( value ) ), scale );
119
149
else
120
- result = setScale (value == null ? random .nextDouble () * random .nextLong () : value , scale );
150
+ result = ensureHasFraction (( double ) setScale (value == null ? ensureNotTooBig ( random .nextDouble () * random .nextLong ()) : value , scale ) );
121
151
122
152
if (decode != null && decode .length () > 0 )
123
153
return JsdUtil .invoke (JsdUtil .parseExecutable (decode , String .class ), String .valueOf (result ));
0 commit comments