Skip to content

Commit

Permalink
Added more converter tests.
Browse files Browse the repository at this point in the history
For all Date & Time related classes, their conversion to Map will consistently have "date" and "time" keys, a "zone" or "offset" key, and "epochMillis" (and "epochNanos" when appropriate)
  • Loading branch information
jdereg committed Mar 17, 2024
1 parent 80c78a4 commit 3a7e5b0
Show file tree
Hide file tree
Showing 14 changed files with 304 additions and 356 deletions.
24 changes: 24 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
</developers>

<properties>
<maven.build.timestamp.format>yyyy-MM-dd'T'HH:mm:ss.SSSZ</maven.build.timestamp.format>
<!-- Java source, target, and release version -->
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
Expand All @@ -40,6 +41,7 @@
<version.plugin.nexus>1.6.13</version.plugin.nexus>

<!-- Build maven-***-plugins -->
<version.plugin.jar>3.3.0</version.plugin.jar>
<version.plugin.gpg>3.1.0</version.plugin.gpg>
<version.plugin.compiler>3.12.1</version.plugin.compiler>
<version.plugin.javadoc>3.6.3</version.plugin.javadoc>
Expand Down Expand Up @@ -112,6 +114,28 @@

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>${version.plugin.jar}</version> <!-- Use the latest version available -->
<configuration>
<archive>
<manifestEntries>
<Implementation-Title>java-util</Implementation-Title>
<Implementation-Version>${project.version}</Implementation-Version>
<Implementation-Vendor-Id>com.cedarsoftware</Implementation-Vendor-Id>
<Implementation-URL>https://github.com/jdereg/java-util</Implementation-URL>
<Built-By>${user.name}</Built-By>
<Build-Timestamp>${maven.build.timestamp}</Build-Timestamp>
<Build-Jdk>${java.version} (${java.vendor} ${java.vm.version})</Build-Jdk>
<Build-OS>${os.name} ${os.arch} ${os.version}</Build-OS>
</manifestEntries>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-scr-plugin</artifactId>
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/cedarsoftware/util/MapUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,14 @@ public static <K, V> Map<K, V> mapOf(K k1, V v1, K k2, V v2, K k3, V v3)
map.put(k3, v3);
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)
{
Map<K, V> map = new LinkedHashMap<>();
map.put(k1, v1);
map.put(k2, v2);
map.put(k3, v3);
map.put(k4, v4);
return Collections.unmodifiableMap(map);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,9 @@ static String toString(Object from, Converter converter) {
static Map<String, Object> toMap(Object from, Converter converter) {
Calendar cal = (Calendar) from;
Map<String, Object> target = new CompactLinkedMap<>();
target.put(MapConversions.YEAR, cal.get(Calendar.YEAR));
target.put(MapConversions.MONTH, cal.get(Calendar.MONTH) + 1);
target.put(MapConversions.DAY, cal.get(Calendar.DAY_OF_MONTH));
target.put(MapConversions.HOUR, cal.get(Calendar.HOUR_OF_DAY));
target.put(MapConversions.MINUTE, cal.get(Calendar.MINUTE));
target.put(MapConversions.SECOND, cal.get(Calendar.SECOND));
target.put(MapConversions.MILLI_SECONDS, cal.get(Calendar.MILLISECOND));
target.put(MapConversions.ZONE, cal.getTimeZone().getID());
target.put(MapConversions.DATE, LocalDate.of(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.DAY_OF_MONTH)).toString());
target.put(MapConversions.TIME, LocalTime.of(cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND), cal.get(Calendar.MILLISECOND) * 1_000_000).toString());
target.put(MapConversions.ZONE, cal.getTimeZone().toZoneId().toString());
return target;
}
}
6 changes: 4 additions & 2 deletions src/main/java/com/cedarsoftware/util/convert/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,7 @@ private static void buildFactoryConversions() {
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);
CONVERSION_DB.put(pair(ZoneId.class, TimeZone.class), ZoneIdConversions::toTimeZone);

// Duration conversions supported
CONVERSION_DB.put(pair(Void.class, Duration.class), VoidConversions::toNull);
Expand Down Expand Up @@ -774,7 +775,8 @@ private static void buildFactoryConversions() {
CONVERSION_DB.put(pair(ZoneId.class, ZoneId.class), Converter::identity);
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);

// ZoneOffset conversions supported
CONVERSION_DB.put(pair(Void.class, ZoneOffset.class), VoidConversions::toNull);
CONVERSION_DB.put(pair(ZoneOffset.class, ZoneOffset.class), Converter::identity);
Expand Down Expand Up @@ -894,7 +896,7 @@ private static void buildFactoryConversions() {
CONVERSION_DB.put(pair(java.sql.Date.class, Map.class), DateConversions::toMap);
CONVERSION_DB.put(pair(Timestamp.class, Map.class), MapConversions::initMap);
CONVERSION_DB.put(pair(LocalDate.class, Map.class), LocalDateConversions::toMap);
CONVERSION_DB.put(pair(LocalDateTime.class, Map.class), MapConversions::initMap);
CONVERSION_DB.put(pair(LocalDateTime.class, Map.class), LocalDateTimeConversions::toMap);
CONVERSION_DB.put(pair(ZonedDateTime.class, Map.class), MapConversions::initMap);
CONVERSION_DB.put(pair(Duration.class, Map.class), DurationConversions::toMap);
CONVERSION_DB.put(pair(Instant.class, Map.class), InstantConversions::toMap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

import com.cedarsoftware.util.CompactLinkedMap;

import static com.cedarsoftware.util.convert.Converter.VALUE;

/**
* @author Kenny Partlow ([email protected])
* <br>
Expand Down Expand Up @@ -147,7 +145,11 @@ static String toString(Object from, Converter converter) {
static Map<String, Object> toMap(Object from, Converter converter) {
Date date = (Date) from;
Map<String, Object> map = new CompactLinkedMap<>();
map.put(VALUE, date.getTime());
ZonedDateTime zdt = toZonedDateTime(date, converter);
map.put(MapConversions.DATE, zdt.toLocalDate().toString());
map.put(MapConversions.TIME, zdt.toLocalTime().toString());
map.put(MapConversions.ZONE, converter.getOptions().getZoneId().toString());
map.put(MapConversions.EPOCH_MILLIS, date.getTime());
return map;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
Expand All @@ -16,8 +16,6 @@

import com.cedarsoftware.util.CompactLinkedMap;

import static com.cedarsoftware.util.convert.Converter.VALUE;

/**
* @author Kenny Partlow ([email protected])
* <br>
Expand Down Expand Up @@ -52,8 +50,8 @@ static LocalDateTime toLocalDateTime(Object from, Converter converter) {
}

static ZonedDateTime toZonedDateTime(Object from, Converter converter) {
ZoneId zoneId = converter.getOptions().getZoneId();
return ((LocalDate) from).atStartOfDay(zoneId);
LocalDate localDate = (LocalDate) from;
return ZonedDateTime.of(localDate, LocalTime.parse("00:00:00"), converter.getOptions().getZoneId());
}

static double toDouble(Object from, Converter converter) {
Expand Down Expand Up @@ -102,7 +100,7 @@ static String toString(Object from, Converter converter) {
static Map<String, Object> toMap(Object from, Converter converter) {
LocalDate localDate = (LocalDate) from;
Map<String, Object> target = new CompactLinkedMap<>();
target.put(VALUE, localDate.toString());
target.put(MapConversions.DATE, localDate.toString());
return target;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

import com.cedarsoftware.util.CompactLinkedMap;

/**
* @author Kenny Partlow ([email protected])
* <br>
Expand Down Expand Up @@ -100,4 +103,12 @@ static String toString(Object from, Converter converter) {
LocalDateTime localDateTime = (LocalDateTime) from;
return localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
}

static Map toMap(Object from, Converter converter) {
LocalDateTime localDateTime = (LocalDateTime) from;
Map<String, Object> target = new CompactLinkedMap<>();
target.put(MapConversions.DATE, localDateTime.toLocalDate().toString());
target.put(MapConversions.TIME, localDateTime.toLocalTime().toString());
return target;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,7 @@ private LocalTimeConversions() {}
static Map<String, Object> toMap(Object from, Converter converter) {
LocalTime localTime = (LocalTime) from;
Map<String, Object> target = new CompactLinkedMap<>();
target.put("hour", localTime.getHour());
target.put("minute", localTime.getMinute());
if (localTime.getNano() != 0) { // Only output 'nano' when not 0 (and then 'second' is required).
target.put("nano", localTime.getNano());
target.put("second", localTime.getSecond());
} else { // 0 nano, 'second' is optional if 0
if (localTime.getSecond() != 0) {
target.put("second", localTime.getSecond());
}
}
target.put(MapConversions.TIME, localTime.toString());
return target;
}

Expand Down
Loading

0 comments on commit 3a7e5b0

Please sign in to comment.