Skip to content

Commit

Permalink
ZoneId supported added to Converter
Browse files Browse the repository at this point in the history
  • Loading branch information
jdereg committed Feb 5, 2024
1 parent 6040524 commit 5b6a7d1
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 5 deletions.
10 changes: 9 additions & 1 deletion src/main/java/com/cedarsoftware/util/convert/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.time.Period;
import java.time.Year;
import java.time.YearMonth;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.AbstractMap;
import java.util.Calendar;
Expand Down Expand Up @@ -676,6 +677,7 @@ private static void buildFactoryConversions() {
DEFAULT_FACTORY.put(pair(MonthDay.class, String.class), StringConversions::toString);
DEFAULT_FACTORY.put(pair(YearMonth.class, String.class), StringConversions::toString);
DEFAULT_FACTORY.put(pair(Period.class, String.class), StringConversions::toString);
DEFAULT_FACTORY.put(pair(ZoneId.class, String.class), StringConversions::toString);
DEFAULT_FACTORY.put(pair(OffsetTime.class, String.class), OffsetTimeConversions::toString);
DEFAULT_FACTORY.put(pair(OffsetDateTime.class, String.class), OffsetDateTimeConversions::toString);
DEFAULT_FACTORY.put(pair(Year.class, String.class), YearConversions::toString);
Expand Down Expand Up @@ -710,7 +712,12 @@ private static void buildFactoryConversions() {
DEFAULT_FACTORY.put(pair(Map.class, Instant.class), MapConversions::toInstant);
DEFAULT_FACTORY.put(pair(OffsetDateTime.class, Instant.class), OffsetDateTimeConversions::toInstant);

// java.time.ZoneId = com.cedarsoftware.util.io.DEFAULT_FACTORY.ZoneIdFactory
// ZoneId conversions supported
DEFAULT_FACTORY.put(pair(Void.class, ZoneId.class), VoidConversions::toNull);
DEFAULT_FACTORY.put(pair(ZoneId.class, ZoneId.class), Converter::identity);
DEFAULT_FACTORY.put(pair(String.class, ZoneId.class), StringConversions::toZoneId);
DEFAULT_FACTORY.put(pair(Map.class, ZoneId.class), MapConversions::toZoneId);

// java.time.ZoneOffset = com.cedarsoftware.util.io.DEFAULT_FACTORY.ZoneOffsetFactory
// java.time.ZoneRegion = com.cedarsoftware.util.io.DEFAULT_FACTORY.ZoneIdFactory

Expand Down Expand Up @@ -830,6 +837,7 @@ private static void buildFactoryConversions() {
DEFAULT_FACTORY.put(pair(MonthDay.class, Map.class), MonthDayConversions::toMap);
DEFAULT_FACTORY.put(pair(YearMonth.class, Map.class), YearMonthConversions::toMap);
DEFAULT_FACTORY.put(pair(Period.class, Map.class), PeriodConversions::toMap);
DEFAULT_FACTORY.put(pair(ZoneId.class, Map.class), ZoneIdConversions::toMap);
DEFAULT_FACTORY.put(pair(Class.class, Map.class), MapConversions::initMap);
DEFAULT_FACTORY.put(pair(UUID.class, Map.class), MapConversions::initMap);
DEFAULT_FACTORY.put(pair(Calendar.class, Map.class), MapConversions::initMap);
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/cedarsoftware/util/convert/MapConversions.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.time.Period;
import java.time.Year;
import java.time.YearMonth;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Calendar;
Expand Down Expand Up @@ -325,6 +326,16 @@ static Period toPeriod(Object from, Converter converter, ConverterOptions option
}
}

static ZoneId toZoneId(Object from, Converter converter, ConverterOptions options) {
Map<String, Object> map = (Map<String, Object>) from;
if (map.containsKey(ZONE)) {
ZoneId zoneId = converter.convert(map.get(ZONE), ZoneId.class, options);
return zoneId;
} else {
return fromSingleKey(from, converter, options, ZONE, ZoneId.class);
}
}

static Year toYear(Object from, Converter converter, ConverterOptions options) {
return fromSingleKey(from, converter, options, YEAR, Year.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.time.Period;
import java.time.Year;
import java.time.YearMonth;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
Expand Down Expand Up @@ -393,6 +394,19 @@ static ZonedDateTime toZonedDateTime(Object from, Converter converter, Converter
return toZonedDateTime(from, options);
}

static ZoneId toZoneId(Object from, Converter converter, ConverterOptions options) {
String s = StringUtilities.trimToNull(asString(from));
if (s == null) {
return null;
}
try {
return ZoneId.of(s);
}
catch (Exception e) {
throw new IllegalArgumentException("Unknown time-zone ID: '" + s + "'");
}
}

static OffsetDateTime toOffsetDateTime(Object from, Converter converter, ConverterOptions options) {
String s = StringUtilities.trimToNull(asString(from));
if (s == null) {
Expand Down Expand Up @@ -465,7 +479,6 @@ static byte[] toByteArray(Object from, ConverterOptions options) {
return s.getBytes(options.getCharset());
}


static byte[] toByteArray(Object from, Converter converter, ConverterOptions options) {
return toByteArray(from, options);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.cedarsoftware.util.convert;

import java.time.ZoneId;
import java.util.Map;

import com.cedarsoftware.util.CompactLinkedMap;

/**
* @author John DeRegnaucourt ([email protected])
* <br>
* Copyright (c) Cedar Software LLC
* <br><br>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <br><br>
* <a href="http://www.apache.org/licenses/LICENSE-2.0">License</a>
* <br><br>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public final class ZoneIdConversions {

private ZoneIdConversions() {}

static Map toMap(Object from, Converter converter, ConverterOptions options) {
ZoneId zoneID = (ZoneId) from;
Map<String, Object> target = new CompactLinkedMap<>();
target.put("zone", zoneID.toString());
return target;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.time.Period;
import java.time.Year;
import java.time.YearMonth;
import java.time.ZoneId;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -173,7 +174,7 @@ class ConverterEverythingTest
{ new BigDecimal("128"), (byte)-128 },
});
TEST_FACTORY.put(pair(Number.class, Byte.class), new Object[][] {

{ -2L, (byte) -2 },
});
TEST_FACTORY.put(pair(Map.class, Byte.class), new Object[][] {
{ mapOf("_v", "-1"), (byte) -1 },
Expand Down Expand Up @@ -337,6 +338,31 @@ class ConverterEverythingTest
});
TEST_FACTORY.put(pair(Number.class, Year.class), new Object[][] {
{ (byte)101, new IllegalArgumentException("Unsupported conversion, source type [Byte (101)] target type 'Year'") },
{ (short)2024, Year.of(2024) },
});

// ZoneId
ZoneId NY_Z = ZoneId.of("America/New_York");
ZoneId TOKYO_Z = ZoneId.of("Asia/Tokyo");
TEST_FACTORY.put(pair(Void.class, ZoneId.class), new Object[][] {
{ null, null },
});
TEST_FACTORY.put(pair(ZoneId.class, ZoneId.class), new Object[][] {
{ NY_Z, NY_Z },
{ TOKYO_Z, TOKYO_Z },
});
TEST_FACTORY.put(pair(String.class, ZoneId.class), new Object[][] {
{ "America/New_York", NY_Z },
{ "Asia/Tokyo", TOKYO_Z },
{ "America/Cincinnati", new IllegalArgumentException("Unknown time-zone ID: 'America/Cincinnati'") },
});
TEST_FACTORY.put(pair(Map.class, ZoneId.class), new Object[][] {
{ mapOf("_v", "America/New_York"), NY_Z },
{ mapOf("_v", NY_Z), NY_Z },
{ mapOf("zone", NY_Z), NY_Z },
{ mapOf("_v", "Asia/Tokyo"), TOKYO_Z },
{ mapOf("_v", TOKYO_Z), TOKYO_Z },
{ mapOf("zone", mapOf("_v", TOKYO_Z)), TOKYO_Z },
});
}

Expand Down Expand Up @@ -388,7 +414,9 @@ void testEverything() {
System.err.println("{ " + getShortName(sourceClass) + ".class ==> " + getShortName(targetClass) + ".class }");
System.err.print("testPair[" + i + "] = ");
if (testPair.length == 2) {
System.err.println("{ " + testPair[0].toString() + ", " + testPair[1].toString() + " }");
String pair0 = testPair[0] == null ? "null" : testPair[0].toString();
String pair1 = testPair[1] == null ? "null" : testPair[1].toString();
System.err.println("{ " + pair0 + ", " + pair1 + " }");
}
System.err.println();
e.printStackTrace();
Expand All @@ -401,7 +429,7 @@ void testEverything() {
}

if (neededTests > 0) {
System.err.println("Conversions needing tests: " + neededTests);
System.err.println(neededTests + " tests need to be added.");
System.err.flush();
}
if (failed) {
Expand Down

0 comments on commit 5b6a7d1

Please sign in to comment.