Skip to content

Commit

Permalink
Added Boolean and Character tests to everything test. Added missing M…
Browse files Browse the repository at this point in the history
…ap tests. Added new stats/info to Everything test output.
  • Loading branch information
jdereg committed Feb 15, 2024
1 parent c267b1a commit 2c01b70
Show file tree
Hide file tree
Showing 10 changed files with 305 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static Short toShort(Object from, Converter converter) {
return b.get() ? CommonValues.SHORT_ONE : CommonValues.SHORT_ZERO;
}

static Integer toInteger(Object from, Converter converter) {
static Integer toInt(Object from, Converter converter) {
AtomicBoolean b = (AtomicBoolean) from;
return b.get() ? CommonValues.INTEGER_ONE : CommonValues.INTEGER_ZERO;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static Short toShort(Object from, Converter converter) {
return b ? CommonValues.SHORT_ONE : CommonValues.SHORT_ZERO;
}

static Integer toInteger(Object from, Converter converter) {
static Integer toInt(Object from, Converter converter) {
Boolean b = (Boolean) from;
return b ? CommonValues.INTEGER_ONE : CommonValues.INTEGER_ZERO;
}
Expand Down
21 changes: 10 additions & 11 deletions src/main/java/com/cedarsoftware/util/convert/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
*/

public final class Converter {
public static final Convert<?> UNSUPPORTED = Converter::unsupported;
private static final Convert<?> UNSUPPORTED = Converter::unsupported;
static final String VALUE = "_v";

private final Map<Map.Entry<Class<?>, Class<?>>, Convert<?>> factory;
Expand Down Expand Up @@ -150,9 +150,9 @@ private static void buildFactoryConversions() {
CONVERSION_DB.put(pair(Long.class, Integer.class), NumberConversions::toInt);
CONVERSION_DB.put(pair(Float.class, Integer.class), NumberConversions::toInt);
CONVERSION_DB.put(pair(Double.class, Integer.class), NumberConversions::toInt);
CONVERSION_DB.put(pair(Boolean.class, Integer.class), BooleanConversions::toInteger);
CONVERSION_DB.put(pair(Boolean.class, Integer.class), BooleanConversions::toInt);
CONVERSION_DB.put(pair(Character.class, Integer.class), CharacterConversions::toInt);
CONVERSION_DB.put(pair(AtomicBoolean.class, Integer.class), AtomicBooleanConversions::toInteger);
CONVERSION_DB.put(pair(AtomicBoolean.class, Integer.class), AtomicBooleanConversions::toInt);
CONVERSION_DB.put(pair(AtomicInteger.class, Integer.class), NumberConversions::toInt);
CONVERSION_DB.put(pair(AtomicLong.class, Integer.class), NumberConversions::toInt);
CONVERSION_DB.put(pair(BigInteger.class, Integer.class), NumberConversions::toInt);
Expand Down Expand Up @@ -261,10 +261,9 @@ private static void buildFactoryConversions() {
CONVERSION_DB.put(pair(Number.class, Boolean.class), NumberConversions::isIntTypeNotZero);
CONVERSION_DB.put(pair(Map.class, Boolean.class), MapConversions::toBoolean);
CONVERSION_DB.put(pair(String.class, Boolean.class), StringConversions::toBoolean);
CONVERSION_DB.put(pair(Year.class, Boolean.class), YearConversions::toBoolean);

// Character/char conversions supported
CONVERSION_DB.put(pair(Void.class, char.class), VoidConversions::toChar);
CONVERSION_DB.put(pair(Void.class, char.class), VoidConversions::toCharacter);
CONVERSION_DB.put(pair(Void.class, Character.class), VoidConversions::toNull);
CONVERSION_DB.put(pair(Byte.class, Character.class), NumberConversions::toCharacter);
CONVERSION_DB.put(pair(Short.class, Character.class), NumberConversions::toCharacter);
Expand Down Expand Up @@ -690,30 +689,29 @@ private static void buildFactoryConversions() {
CONVERSION_DB.put(pair(TimeZone.class, String.class), TimeZoneConversions::toString);

try {
Class zoneInfoClass = Class.forName("sun.util.calendar.ZoneInfo");
Class<?> zoneInfoClass = Class.forName("sun.util.calendar.ZoneInfo");
CONVERSION_DB.put(pair(zoneInfoClass, String.class), TimeZoneConversions::toString);
CONVERSION_DB.put(pair(Void.class, zoneInfoClass), VoidConversions::toNull);
CONVERSION_DB.put(pair(String.class, zoneInfoClass), StringConversions::toTimeZone);
CONVERSION_DB.put(pair(Map.class, zoneInfoClass), MapConversions::toTimeZone);



} catch (Exception e) {
// ignore
} catch (Exception ignore) {
}

// URL conversions
CONVERSION_DB.put(pair(Void.class, URL.class), VoidConversions::toNull);
CONVERSION_DB.put(pair(URL.class, URL.class), Converter::identity);
CONVERSION_DB.put(pair(String.class, URL.class), StringConversions::toURL);
CONVERSION_DB.put(pair(Map.class, URL.class), MapConversions::toURL);

// URI Conversions
CONVERSION_DB.put(pair(Void.class, URI.class), VoidConversions::toNull);
CONVERSION_DB.put(pair(URI.class, URI.class), Converter::identity);
CONVERSION_DB.put(pair(String.class, URI.class), StringConversions::toURI);
CONVERSION_DB.put(pair(Map.class, URI.class), MapConversions::toURI);

// TimeZone Conversions
CONVERSION_DB.put(pair(Void.class, TimeZone.class), VoidConversions::toNull);
CONVERSION_DB.put(pair(TimeZone.class, TimeZone.class), Converter::identity);
CONVERSION_DB.put(pair(String.class, TimeZone.class), StringConversions::toTimeZone);
CONVERSION_DB.put(pair(Map.class, TimeZone.class), MapConversions::toTimeZone);

Expand Down Expand Up @@ -884,6 +882,7 @@ private static void buildFactoryConversions() {
CONVERSION_DB.put(pair(Map.class, Map.class), MapConversions::toMap);
CONVERSION_DB.put(pair(Enum.class, Map.class), MapConversions::initMap);
CONVERSION_DB.put(pair(OffsetDateTime.class, Map.class), OffsetDateTimeConversions::toMap);
CONVERSION_DB.put(pair(Year.class, Map.class), YearConversions::toMap);
}

public Converter(ConverterOptions options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,6 @@ static ZonedDateTime toZonedDateTime(Object from, Converter converter) {
return ((LocalDate) from).atStartOfDay(zoneId);
}

/**
* Warning: Can lose precision going from a full long down to a floating point number
* @param from instance to convert
* @param converter converter instance
* @return the floating point number cast from a long.
*/
static float toFloat(Object from, Converter converter) {
return toLong(from, converter);
}

static double toDouble(Object from, Converter converter) {
return toLong(from, converter);
}
Expand Down
46 changes: 23 additions & 23 deletions src/main/java/com/cedarsoftware/util/convert/MapConversions.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,29 +54,29 @@
* limitations under the License.
*/
final class MapConversions {
private static final String V = "_v";
private static final String VALUE = "value";
private static final String DATE = "date";
private static final String TIME = "time";
private static final String ZONE = "zone";
private static final String YEAR = "year";
private static final String YEARS = "years";
private static final String MONTH = "month";
private static final String MONTHS = "months";
private static final String DAY = "day";
private static final String DAYS = "days";
private static final String HOUR = "hour";
private static final String HOURS = "hours";
private static final String MINUTE = "minute";
private static final String MINUTES = "minutes";
private static final String SECOND = "second";
private static final String SECONDS = "seconds";
private static final String NANO = "nano";
private static final String NANOS = "nanos";
private static final String OFFSET_HOUR = "offsetHour";
private static final String OFFSET_MINUTE = "offsetMinute";
private static final String MOST_SIG_BITS = "mostSigBits";
private static final String LEAST_SIG_BITS = "leastSigBits";
static final String V = "_v";
static final String VALUE = "value";
static final String DATE = "date";
static final String TIME = "time";
static final String ZONE = "zone";
static final String YEAR = "year";
static final String YEARS = "years";
static final String MONTH = "month";
static final String MONTHS = "months";
static final String DAY = "day";
static final String DAYS = "days";
static final String HOUR = "hour";
static final String HOURS = "hours";
static final String MINUTE = "minute";
static final String MINUTES = "minutes";
static final String SECOND = "second";
static final String SECONDS = "seconds";
static final String NANO = "nano";
static final String NANOS = "nanos";
static final String OFFSET_HOUR = "offsetHour";
static final String OFFSET_MINUTE = "offsetMinute";
static final String MOST_SIG_BITS = "mostSigBits";
static final String LEAST_SIG_BITS = "leastSigBits";

static final String OFFSET = "offset";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static Boolean toBoolean(Object from, Converter converter) {
return Boolean.FALSE;
}

static Character toChar(Object from, Converter converter) {
static Character toCharacter(Object from, Converter converter) {
return Character.MIN_VALUE;
}
}
18 changes: 13 additions & 5 deletions src/main/java/com/cedarsoftware/util/convert/YearConversions.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.Year;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

import com.cedarsoftware.util.CompactLinkedMap;

import static com.cedarsoftware.util.convert.MapConversions.YEAR;

/**
* @author Kenny Partlow ([email protected])
* <br>
Expand Down Expand Up @@ -54,11 +59,7 @@ static double toDouble(Object from, Converter converter) {
static float toFloat(Object from, Converter converter) {
return toInt(from, converter);
}

static boolean toBoolean(Object from, Converter converter) {
return toInt(from, converter) == 0;
}


static AtomicBoolean toAtomicBoolean(Object from, Converter converter) {
return new AtomicBoolean(toInt(from, converter) == 0);
}
Expand All @@ -74,4 +75,11 @@ static BigDecimal toBigDecimal(Object from, Converter converter) {
static String toString(Object from, Converter converter) {
return ((Year)from).toString();
}

static Map<?, ?> toMap(Object from, Converter converter) {
Year year = (Year) from;
Map<String, Integer> map = new CompactLinkedMap<>();
map.put(YEAR, year.getValue());
return map;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private static Stream<Arguments> toIntegerParams() {
@ParameterizedTest
@MethodSource("toIntegerParams")
void testToInteger(boolean value, Integer expected) {
Integer actual = AtomicBooleanConversions.toInteger(new AtomicBoolean(value), null);
Integer actual = AtomicBooleanConversions.toInt(new AtomicBoolean(value), null);
assertThat(actual).isSameAs(expected);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private static Stream<Arguments> toIntegerParams() {
@ParameterizedTest
@MethodSource("toIntegerParams")
void testToInteger(boolean value, Integer expected) {
Integer actual = BooleanConversions.toInteger(value, null);
Integer actual = BooleanConversions.toInt(value, null);
assertThat(actual).isSameAs(expected);
}

Expand Down
Loading

0 comments on commit 2c01b70

Please sign in to comment.