Skip to content

Commit

Permalink
100% Code Coverage for all 687 conversion tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jdereg committed Mar 25, 2024
1 parent d5ba29e commit 73c181e
Show file tree
Hide file tree
Showing 11 changed files with 500 additions and 427 deletions.
12 changes: 12 additions & 0 deletions src/main/java/com/cedarsoftware/util/MapUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,16 @@ public static <K, V> Map<K, V> mapOf(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V
map.put(k5, v5);
return Collections.unmodifiableMap(map);
}

public static <K, V> Map<K, V> mapOf(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6)
{
Map<K, V> map = new LinkedHashMap<>();
map.put(k1, v1);
map.put(k2, v2);
map.put(k3, v3);
map.put(k4, v4);
map.put(k5, v5);
map.put(k6, v6);
return Collections.unmodifiableMap(map);
}
}
6 changes: 5 additions & 1 deletion src/main/java/com/cedarsoftware/util/convert/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,7 @@ private static void buildFactoryConversions() {
CONVERSION_DB.put(pair(String.class, TimeZone.class), StringConversions::toTimeZone);
CONVERSION_DB.put(pair(Map.class, TimeZone.class), MapConversions::toTimeZone);
CONVERSION_DB.put(pair(ZoneId.class, TimeZone.class), ZoneIdConversions::toTimeZone);
CONVERSION_DB.put(pair(ZoneOffset.class, TimeZone.class), UNSUPPORTED);

// Duration conversions supported
CONVERSION_DB.put(pair(Void.class, Duration.class), VoidConversions::toNull);
Expand Down Expand Up @@ -719,13 +720,16 @@ private static void buildFactoryConversions() {
CONVERSION_DB.put(pair(String.class, ZoneId.class), StringConversions::toZoneId);
CONVERSION_DB.put(pair(Map.class, ZoneId.class), MapConversions::toZoneId);
CONVERSION_DB.put(pair(TimeZone.class, ZoneId.class), TimeZoneConversions::toZoneId);
CONVERSION_DB.put(pair(ZoneOffset.class, ZoneId.class), ZoneOffsetConversions::toZoneId);

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

CONVERSION_DB.put(pair(ZoneId.class, ZoneOffset.class), UNSUPPORTED);
CONVERSION_DB.put(pair(TimeZone.class, ZoneOffset.class), UNSUPPORTED);

// MonthDay conversions supported
CONVERSION_DB.put(pair(Void.class, MonthDay.class), VoidConversions::toNull);
CONVERSION_DB.put(pair(MonthDay.class, MonthDay.class), Converter::identity);
Expand Down
41 changes: 0 additions & 41 deletions src/main/java/com/cedarsoftware/util/convert/EnumConversions.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
package com.cedarsoftware.util.convert;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.time.Duration;
import java.time.Instant;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

import com.cedarsoftware.util.CompactLinkedMap;

Expand Down Expand Up @@ -37,39 +31,4 @@ static Map toMap(Object from, Converter converter) {
target.put("name", enumInstance.name());
return target;
}

static long toLong(Object from, Converter converter) {
return ((Duration) from).toMillis();
}

static AtomicLong toAtomicLong(Object from, Converter converter) {
Duration duration = (Duration) from;
return new AtomicLong(duration.toMillis());
}

static BigInteger toBigInteger(Object from, Converter converter) {
Duration duration = (Duration) from;
BigInteger epochSeconds = BigInteger.valueOf(duration.getSeconds());
BigInteger nanos = BigInteger.valueOf(duration.getNano());

// Convert seconds to nanoseconds and add the nanosecond part
return epochSeconds.multiply(BigIntegerConversions.BILLION).add(nanos);
}

static double toDouble(Object from, Converter converter) {
Duration duration = (Duration) from;
return BigDecimalConversions.secondsAndNanosToDouble(duration.getSeconds(), duration.getNano()).doubleValue();
}

static BigDecimal toBigDecimal(Object from, Converter converter) {
Duration duration = (Duration) from;
return BigDecimalConversions.secondsAndNanosToDouble(duration.getSeconds(), duration.getNano());
}

static Timestamp toTimestamp(Object from, Converter converter) {
Duration duration = (Duration) from;
Instant epoch = Instant.EPOCH;
Instant timeAfterDuration = epoch.plus(duration);
return Timestamp.from(timeAfterDuration);
}
}
Loading

0 comments on commit 73c181e

Please sign in to comment.