Skip to content

Commit

Permalink
Updated String to integer types to support Strings that have floating…
Browse files Browse the repository at this point in the history
… point values only, as integers with the fractional part truncated.
  • Loading branch information
jdereg committed Jan 13, 2024
1 parent 14bbdfe commit 5356beb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
28 changes: 19 additions & 9 deletions src/main/java/com/cedarsoftware/util/convert/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -246,7 +247,7 @@ private static void buildFactoryConversions() {
try {
return Long.valueOf(str);
} catch (NumberFormatException e) {
Long value = strToLong(str, Long.MIN_VALUE, Long.MAX_VALUE);
Long value = strToLong(str, bigDecimalMinLong, bigDecimalMaxLong);
if (value == null) {
throw new IllegalArgumentException("Value: " + fromInstance + " not parseable as a long value or outside " + Long.MIN_VALUE + " to " + Long.MAX_VALUE);
}
Expand Down Expand Up @@ -556,7 +557,7 @@ private static void buildFactoryConversions() {
if (str.isEmpty()) {
return new AtomicLong(0L);
}
Long value = strToLong(str, Long.MIN_VALUE, Long.MAX_VALUE);
Long value = strToLong(str, bigDecimalMinLong, bigDecimalMaxLong);
if (value == null) {
throw new IllegalArgumentException("Value: " + fromInstance + " not parseable as an AtomicLong value or outside " + Long.MIN_VALUE + " to " + Long.MAX_VALUE);
}
Expand Down Expand Up @@ -1348,9 +1349,18 @@ public static long zonedDateTimeToMillis(ZonedDateTime zonedDateTime) {
return zonedDateTime.toInstant().toEpochMilli();
}

private static final BigDecimal bigDecimalMinByte = BigDecimal.valueOf(Byte.MIN_VALUE);
private static final BigDecimal bigDecimalMaxByte = BigDecimal.valueOf(Byte.MAX_VALUE);
private static final BigDecimal bigDecimalMinShort = BigDecimal.valueOf(Short.MIN_VALUE);
private static final BigDecimal bigDecimalMaxShort = BigDecimal.valueOf(Short.MAX_VALUE);
private static final BigDecimal bigDecimalMinInteger = BigDecimal.valueOf(Integer.MIN_VALUE);
private static final BigDecimal bigDecimalMaxInteger = BigDecimal.valueOf(Integer.MAX_VALUE);
private static final BigDecimal bigDecimalMaxLong = BigDecimal.valueOf(Long.MAX_VALUE);
private static final BigDecimal bigDecimalMinLong = BigDecimal.valueOf(Long.MIN_VALUE);

private static Byte strToByte(String s)
{
Long value = strToLong(s, Byte.MIN_VALUE, Byte.MAX_VALUE);
Long value = strToLong(s, bigDecimalMinByte, bigDecimalMaxByte);
if (value == null) {
return null;
}
Expand All @@ -1359,7 +1369,7 @@ private static Byte strToByte(String s)

private static Short strToShort(String s)
{
Long value = strToLong(s, Short.MIN_VALUE, Short.MAX_VALUE);
Long value = strToLong(s, bigDecimalMinShort, bigDecimalMaxShort);
if (value == null) {
return null;
}
Expand All @@ -1368,19 +1378,19 @@ private static Short strToShort(String s)

private static Integer strToInteger(String s)
{
Long value = strToLong(s, Integer.MIN_VALUE, Integer.MAX_VALUE);
Long value = strToLong(s, bigDecimalMinInteger, bigDecimalMaxInteger);
if (value == null) {
return null;
}
return value.intValue();
}
private static Long strToLong(String s, long low, long high)

private static Long strToLong(String s, BigDecimal low, BigDecimal high)
{
try {
BigDecimal big = new BigDecimal(s);
long value = big.longValue();
if (value < low || value > high) {
big = big.setScale(0, RoundingMode.DOWN);
if (big.compareTo(low) == -1 || big.compareTo(high) == 1) {
return null;
}
return big.longValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,6 @@ void testEpochDays()
// 6-digits - max case - all 9's
date = DateUtilities.parseDate("999999");
gmtDateString = sdf.format(date);
System.out.println("gmtDateString = " + gmtDateString);
assertEquals("4707-11-28 00:00:00", gmtDateString);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,6 @@ void testInt_fromBoolean(Object value, Integer expectedResult)

private static Stream<Arguments> testIntegerParams_withIllegalArguments() {
return Stream.of(
Arguments.of("11.5", "not parseable as an integer"),
Arguments.of("45badNumber", "not parseable as an integer"),
Arguments.of( "12147483648", "not parseable as an integer"),
Arguments.of("2147483649", "not parseable as an integer"),
Expand Down Expand Up @@ -528,7 +527,6 @@ void testLong_fromLocalDate()

private static Stream<Arguments> testLongParams_withIllegalArguments() {
return Stream.of(
Arguments.of("11.5", "not parseable as a long value"),
Arguments.of("45badNumber", "not parseable as a long value"),
Arguments.of( "-9223372036854775809", "not parseable as a long value"),
Arguments.of("9223372036854775808", "not parseable as a long value"),
Expand Down Expand Up @@ -635,7 +633,6 @@ void testAtomicLong_fromLocalDate()

private static Stream<Arguments> testAtomicLongParams_withIllegalArguments() {
return Stream.of(
Arguments.of("11.5", "not parseable as an AtomicLong"),
Arguments.of("45badNumber", "not parseable as an AtomicLong"),
Arguments.of( "-9223372036854775809", "not parseable as an AtomicLong"),
Arguments.of("9223372036854775808", "not parseable as an AtomicLong"),
Expand Down

0 comments on commit 5356beb

Please sign in to comment.