Skip to content

Commit

Permalink
Moved more String conversions to StringConversion.java, following est…
Browse files Browse the repository at this point in the history
…ablished naming patterns.
  • Loading branch information
jdereg committed Jan 14, 2024
1 parent cc96f3d commit 6c21323
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 28 deletions.
30 changes: 3 additions & 27 deletions src/main/java/com/cedarsoftware/util/convert/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -316,18 +316,7 @@ private static void buildFactoryConversions() {
DEFAULT_FACTORY.put(pair(Calendar.class, BigInteger.class), CalendarConversion::toBigInteger);
DEFAULT_FACTORY.put(pair(Number.class, BigInteger.class), (fromInstance, converter, options) -> new BigInteger(fromInstance.toString()));
DEFAULT_FACTORY.put(pair(Map.class, BigInteger.class), MapConversion::toBigInteger);
DEFAULT_FACTORY.put(pair(String.class, BigInteger.class), (fromInstance, converter, options) -> {
String str = ((String) fromInstance).trim();
if (str.isEmpty()) {
return BigInteger.ZERO;
}
try {
BigDecimal bigDec = new BigDecimal(str);
return bigDec.toBigInteger();
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Value: " + fromInstance + " not parseable as a BigInteger value.");
}
});
DEFAULT_FACTORY.put(pair(String.class, BigInteger.class), StringConversion::toBigInteger);



Expand Down Expand Up @@ -381,13 +370,7 @@ private static void buildFactoryConversions() {
DEFAULT_FACTORY.put(pair(AtomicLong.class, AtomicBoolean.class), NumberConversion::toAtomicBoolean);
DEFAULT_FACTORY.put(pair(Number.class, AtomicBoolean.class), NumberConversion::toAtomicBoolean);
DEFAULT_FACTORY.put(pair(Map.class, AtomicBoolean.class), MapConversion::toAtomicBoolean);
DEFAULT_FACTORY.put(pair(String.class, AtomicBoolean.class), (fromInstance, converter, options) -> {
String str = ((String) fromInstance).trim();
if (str.isEmpty()) {
return new AtomicBoolean(false);
}
return new AtomicBoolean("true".equalsIgnoreCase(str));
});
DEFAULT_FACTORY.put(pair(String.class, AtomicBoolean.class), StringConversion::toAtomicBoolean);

// AtomicInteger conversions supported
DEFAULT_FACTORY.put(pair(Void.class, AtomicInteger.class), VoidConversion::toNull);
Expand Down Expand Up @@ -484,14 +467,7 @@ private static void buildFactoryConversions() {
return converter.fromValueMap((Map<?, ?>) fromInstance, java.sql.Date.class, CollectionUtilities.setOf("time"), options);
}
});
DEFAULT_FACTORY.put(pair(String.class, java.sql.Date.class), (fromInstance, converter, options) -> {
String str = ((String) fromInstance).trim();
Date date = DateUtilities.parseDate(str);
if (date == null) {
return null;
}
return new java.sql.Date(date.getTime());
});
DEFAULT_FACTORY.put(pair(String.class, java.sql.Date.class), StringConversion::toSqlDate);

// Timestamp conversions supported
DEFAULT_FACTORY.put(pair(Void.class, Timestamp.class), VoidConversion::toNull);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.cedarsoftware.util.convert;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.util.Date;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

import com.cedarsoftware.util.DateUtilities;

/**
* @author John DeRegnaucourt ([email protected])
* <br>
Expand Down Expand Up @@ -157,6 +162,14 @@ 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));
}

static AtomicInteger toAtomicInteger(Object from, Converter converter, ConverterOptions options) {
String str = ((String) from).trim();
if (str.isEmpty()) {
Expand Down Expand Up @@ -208,7 +221,20 @@ public static char toCharacter(Object from, Converter converter, ConverterOption
return (char) Integer.parseInt(str.trim());
}

public static BigDecimal toBigDecimal(Object from, Converter converter, ConverterOptions options) {
static BigInteger toBigInteger(Object from, Converter converter, ConverterOptions options) {
String str = ((String) from).trim();
if (str.isEmpty()) {
return BigInteger.ZERO;
}
try {
BigDecimal bigDec = new BigDecimal(str);
return bigDec.toBigInteger();
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Value: " + from + " not parseable as a BigInteger value.");
}
}

static BigDecimal toBigDecimal(Object from, Converter converter, ConverterOptions options) {
String str = ((String) from).trim();
if (str.isEmpty()) {
return BigDecimal.ZERO;
Expand All @@ -219,4 +245,13 @@ public static BigDecimal toBigDecimal(Object from, Converter converter, Converte
throw new IllegalArgumentException("Value: " + from + " not parseable as a BigDecimal value.");
}
}

static java.sql.Date toSqlDate(Object from, Converter converter, ConverterOptions options) {
String str = ((String) from).trim();
Date date = DateUtilities.parseDate(str);
if (date == null) {
return null;
}
return new java.sql.Date(date.getTime());
}
}

0 comments on commit 6c21323

Please sign in to comment.