Skip to content

Commit

Permalink
Normalized some string conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
kpartlow committed Jan 28, 2024
1 parent 3efe2b2 commit ed91024
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 72 deletions.
113 changes: 44 additions & 69 deletions src/main/java/com/cedarsoftware/util/convert/StringConversions.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,82 +41,74 @@ public class StringConversions {
private static final BigDecimal bigDecimalMinLong = BigDecimal.valueOf(Long.MIN_VALUE);

static Byte toByte(Object from, Converter converter, ConverterOptions options) {
String str = ((String) from).trim();
if (str.isEmpty()) {
return toByte((String)from);
}

private static Byte toByte(String s) {
if (s.isEmpty()) {
return CommonValues.BYTE_ZERO;
}
try {
return Byte.valueOf(str);
return Byte.valueOf(s);
} catch (NumberFormatException e) {
Byte value = toByte(str);
Long value = toLong(s, bigDecimalMinByte, bigDecimalMaxByte);
if (value == null) {
throw new IllegalArgumentException("Value: " + from + " not parseable as a byte value or outside " + Byte.MIN_VALUE + " to " + Byte.MAX_VALUE);
throw new IllegalArgumentException("Value: " + s + " not parseable as a byte value or outside " + Byte.MIN_VALUE + " to " + Byte.MAX_VALUE);
}
return value;
return value.byteValue();
}
}

private static Byte toByte(String s) {
Long value = toLong(s, bigDecimalMinByte, bigDecimalMaxByte);
if (value == null) {
return null;
}
return value.byteValue();
static Short toShort(Object from, Converter converter, ConverterOptions options) {
return toShort(from);
}

static Short toShort(Object from, Converter converter, ConverterOptions options) {
String str = ((String) from).trim();
private static Short toShort(Object o) {
String str = StringUtilities.trimToEmpty((String)o);
if (str.isEmpty()) {
return CommonValues.SHORT_ZERO;
}
try {
return Short.valueOf(str);
} catch (NumberFormatException e) {
Short value = toShort(str);
Long value = toLong(str, bigDecimalMinShort, bigDecimalMaxShort);
if (value == null) {
throw new IllegalArgumentException("Value: " + from + " not parseable as a short value or outside " + Short.MIN_VALUE + " to " + Short.MAX_VALUE);
throw new IllegalArgumentException("Value: " + o + " not parseable as a short value or outside " + Short.MIN_VALUE + " to " + Short.MAX_VALUE);
}
return value;
return value.shortValue();
}
}

private static Short toShort(String s) {
Long value = toLong(s, bigDecimalMinShort, bigDecimalMaxShort);
if (value == null) {
return null;
}
return value.shortValue();
static Integer toInt(Object from, Converter converter, ConverterOptions options) {
return toInt(from);
}

static Integer toInt(Object from, Converter converter, ConverterOptions options) {
String str = ((String) from).trim();
private static Integer toInt(Object from) {
String str = StringUtilities.trimToEmpty((String)from);
if (str.isEmpty()) {
return CommonValues.INTEGER_ZERO;
}
try {
return Integer.valueOf(str);
} catch (NumberFormatException e) {
Integer value = toInt(str);
Long value = toLong(str, bigDecimalMinInteger, bigDecimalMaxInteger);
if (value == null) {
throw new IllegalArgumentException("Value: " + from + " not parseable as an int value or outside " + Integer.MIN_VALUE + " to " + Integer.MAX_VALUE);
}
return value;
return value.intValue();
}
}

private static Integer toInt(String s) {
Long value = toLong(s, bigDecimalMinInteger, bigDecimalMaxInteger);
if (value == null) {
return null;
}
return value.intValue();
static Long toLong(Object from, Converter converter, ConverterOptions options) {
return toLong(from);
}

static Long toLong(Object from, Converter converter, ConverterOptions options) {
String str = ((String) from).trim();
private static Long toLong(Object from) {
String str = StringUtilities.trimToEmpty((String)from);
if (str.isEmpty()) {
return CommonValues.LONG_ZERO;
}

try {
return Long.valueOf(str);
} catch (NumberFormatException e) {
Expand All @@ -142,7 +134,7 @@ private static Long toLong(String s, BigDecimal low, BigDecimal high) {
}

static Float toFloat(Object from, Converter converter, ConverterOptions options) {
String str = ((String) from).trim();
String str = StringUtilities.trimToEmpty((String)from);
if (str.isEmpty()) {
return CommonValues.FLOAT_ZERO;
}
Expand All @@ -154,7 +146,7 @@ static Float toFloat(Object from, Converter converter, ConverterOptions options)
}

static Double toDouble(Object from, Converter converter, ConverterOptions options) {
String str = ((String) from).trim();
String str = StringUtilities.trimToEmpty((String)from);
if (str.isEmpty()) {
return CommonValues.DOUBLE_ZERO;
}
Expand All @@ -166,40 +158,19 @@ static Double toDouble(Object from, Converter converter, ConverterOptions option
}

static AtomicBoolean toAtomicBoolean(Object from, Converter converter, ConverterOptions options) {
String str = ((String) from).trim();
if (str.isEmpty()) {
return new AtomicBoolean(false);
}
return new AtomicBoolean("true".equalsIgnoreCase(str) || "t".equalsIgnoreCase(str));
return new AtomicBoolean(toBoolean((String)from));
}

static AtomicInteger toAtomicInteger(Object from, Converter converter, ConverterOptions options) {
String str = ((String) from).trim();
if (str.isEmpty()) {
return new AtomicInteger(0);
}

Integer integer = toInt(str);
if (integer == null) {
throw new IllegalArgumentException("Value: " + from + " not parseable as an AtomicInteger value or outside " + Integer.MIN_VALUE + " to " + Integer.MAX_VALUE);
}
return new AtomicInteger(integer);
return new AtomicInteger(toInt(from));
}

static AtomicLong toAtomicLong(Object from, Converter converter, ConverterOptions options) {
String str = StringUtilities.trimToEmpty((String)from);
if (str.isEmpty()) {
return new AtomicLong(0L);
}
Long value = toLong(str, bigDecimalMinLong, bigDecimalMaxLong);
if (value == null) {
throw new IllegalArgumentException("Value: " + from + " not parseable as an AtomicLong value or outside " + Long.MIN_VALUE + " to " + Long.MAX_VALUE);
}
return new AtomicLong(value);
return new AtomicLong(toLong(from));
}

static Boolean toBoolean(Object from, Converter converter, ConverterOptions options) {
String str = StringUtilities.trimToEmpty((String)from);
private static Boolean toBoolean(String from) {
String str = StringUtilities.trimToEmpty(from);
if (str.isEmpty()) {
return false;
}
Expand All @@ -212,9 +183,13 @@ static Boolean toBoolean(Object from, Converter converter, ConverterOptions opti
return "true".equalsIgnoreCase(str) || "t".equalsIgnoreCase(str) || "1".equalsIgnoreCase(str) || "y".equalsIgnoreCase(str);
}

static Boolean toBoolean(Object from, Converter converter, ConverterOptions options) {
return toBoolean((String)from);
}

static char toCharacter(Object from, Converter converter, ConverterOptions options) {
String str = StringUtilities.trimToEmpty((String)from);
if (str.isEmpty()) {
String str = StringUtilities.trimToNull((String)from);
if (str == null) {
return CommonValues.CHARACTER_ZERO;
}
if (str.length() == 1) {
Expand All @@ -225,8 +200,8 @@ static char toCharacter(Object from, Converter converter, ConverterOptions optio
}

static BigInteger toBigInteger(Object from, Converter converter, ConverterOptions options) {
String str = StringUtilities.trimToEmpty((String)from);
if (str.isEmpty()) {
String str = StringUtilities.trimToNull((String)from);
if (str == null) {
return BigInteger.ZERO;
}
try {
Expand Down Expand Up @@ -268,8 +243,8 @@ static Date toDate(Object from, Converter converter, ConverterOptions options) {
}

static Instant toInstant(Object from, Converter converter, ConverterOptions options) {
String s = StringUtilities.trimToEmpty((String)from);
if (s.isEmpty()) {
String s = StringUtilities.trimToNull((String)from);
if (s == null) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1609,9 +1609,9 @@ void testLocalDateTimeToBigDecimal(long epochMilli, ZoneId sourceZoneId, LocalDa

private static Stream<Arguments> testAtomicLongParams_withIllegalArguments() {
return Stream.of(
Arguments.of("45badNumber", "not parseable as an AtomicLong"),
Arguments.of( "-9223372036854775809", "not parseable as an AtomicLong"),
Arguments.of("9223372036854775808", "not parseable as an AtomicLong"),
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"),
Arguments.of( TimeZone.getDefault(), "Unsupported conversion"));
}

Expand Down

0 comments on commit ed91024

Please sign in to comment.