Skip to content

Commit

Permalink
LocaleConversions
Browse files Browse the repository at this point in the history
  • Loading branch information
kpartlow committed Feb 13, 2024
1 parent 5078935 commit 03dba88
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 10 deletions.
12 changes: 12 additions & 0 deletions src/main/java/com/cedarsoftware/util/convert/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URI;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.sql.Timestamp;
Expand All @@ -23,6 +25,7 @@
import java.util.AbstractMap;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
Expand Down Expand Up @@ -628,6 +631,12 @@ private static void buildFactoryConversions() {
CONVERSION_DB.put(pair(Map.class, Class.class), MapConversions::toClass);
CONVERSION_DB.put(pair(String.class, Class.class), StringConversions::toClass);

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

// String conversions supported
CONVERSION_DB.put(pair(Void.class, String.class), VoidConversions::toNull);
CONVERSION_DB.put(pair(Byte.class, String.class), StringConversions::toString);
Expand Down Expand Up @@ -673,6 +682,9 @@ private static void buildFactoryConversions() {
CONVERSION_DB.put(pair(OffsetTime.class, String.class), OffsetTimeConversions::toString);
CONVERSION_DB.put(pair(OffsetDateTime.class, String.class), OffsetDateTimeConversions::toString);
CONVERSION_DB.put(pair(Year.class, String.class), YearConversions::toString);
CONVERSION_DB.put(pair(Locale.class, String.class), LocaleConversions::toString);
CONVERSION_DB.put(pair(URL.class, String.class), StringConversions::toString);
CONVERSION_DB.put(pair(URI.class, String.class), StringConversions::toString);

// Duration conversions supported
CONVERSION_DB.put(pair(Void.class, Duration.class), VoidConversions::toNull);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.cedarsoftware.util.convert;

import java.util.Locale;

public final class LocaleConversions {
private LocaleConversions() {}

static String toString(Object from, Converter converter) {
return ((Locale)from).toLanguageTag();
}
}
51 changes: 41 additions & 10 deletions src/main/java/com/cedarsoftware/util/convert/MapConversions.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import java.util.UUID;
Expand Down Expand Up @@ -81,6 +82,9 @@ final class MapConversions {
static final String DATE_TIME = "dateTime";

private static final String ID = "id";
public static final String LANGUAGE = "language";
public static final String VARIANT = "variant";
private static String COUNTRY = "country";

private MapConversions() {}

Expand Down Expand Up @@ -204,6 +208,32 @@ static Calendar toCalendar(Object from, Converter converter) {
}
}

private static final String[] LOCALE_PARAMS = new String[] { LANGUAGE };
static Locale toLocale(Object from, Converter converter) {
Map<?, ?> map = (Map<?, ?>) from;

if (map.containsKey(VALUE) || map.containsKey(V)) {
return fromValueForMultiKey(map, converter, Locale.class, LOCALE_PARAMS);
}

String language = converter.convert(map.get(LANGUAGE), String.class);
if (language == null) {
throw new IllegalArgumentException("java.util.Locale must specify 'language' field");
}
String country = converter.convert(map.get(COUNTRY), String.class);
String variant = converter.convert(map.get(VARIANT), String.class);

if (country == null) {
return new Locale(language);
}
if (variant == null) {
return new Locale(language, country);
}

return new Locale(language, country, variant);
}


private static final String[] LOCAL_DATE_PARAMS = new String[] { YEAR, MONTH, DAY };
static LocalDate toLocalDate(Object from, Converter converter) {
Map<?, ?> map = (Map<?, ?>) from;
Expand Down Expand Up @@ -360,16 +390,18 @@ static YearMonth toYearMonth(Object from, Converter converter) {

private static final String[] PERIOD_PARAMS = new String[] { YEARS, MONTHS, DAYS };
static Period toPeriod(Object from, Converter converter) {

Map<String, Object> map = (Map<String, Object>) from;
if (map.containsKey(YEARS) && map.containsKey(MONTHS) && map.containsKey(DAYS)) {
ConverterOptions options = converter.getOptions();
int years = converter.convert(map.get(YEARS), int.class);
int months = converter.convert(map.get(MONTHS), int.class);
int days = converter.convert(map.get(DAYS), int.class);
return Period.of(years, months, days);
} else {

if (map.containsKey(VALUE) || map.containsKey(V)) {
return fromValueForMultiKey(from, converter, Period.class, PERIOD_PARAMS);
}

Number years = converter.convert(map.getOrDefault(YEARS, 0), int.class);
Number months = converter.convert(map.getOrDefault(MONTHS, 0), int.class);
Number days = converter.convert(map.getOrDefault(DAYS, 0), int.class);

return Period.of(years.intValue(), months.intValue(), days.intValue());
}

static ZoneId toZoneId(Object from, Converter converter) {
Expand All @@ -391,10 +423,9 @@ static ZoneId toZoneId(Object from, Converter converter) {
static ZoneOffset toZoneOffset(Object from, Converter converter) {
Map<String, Object> map = (Map<String, Object>) from;
if (map.containsKey(HOURS)) {
ConverterOptions options = converter.getOptions();
int hours = converter.convert(map.get(HOURS), int.class);
int minutes = converter.convert(map.get(MINUTES), int.class); // optional
int seconds = converter.convert(map.get(SECONDS), int.class); // optional
int minutes = converter.convert(map.getOrDefault(MINUTES, 0), int.class); // optional
int seconds = converter.convert(map.getOrDefault(SECONDS, 0), int.class); // optional
return ZoneOffset.ofHoursMinutesSeconds(hours, minutes, seconds);
} else {
return fromValueForMultiKey(from, converter, ZoneOffset.class, ZONE_OFFSET_PARAMS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
Expand Down Expand Up @@ -351,6 +352,15 @@ static LocalTime toLocalTime(Object from, Converter converter) {
}
}

static Locale toLocale(Object from, Converter converter) {
String str = StringUtilities.trimToNull(asString(from));
if (str == null) {
return null;
}

return Locale.forLanguageTag(str);
}

private static Optional<ZonedDateTime> parseDate(Object from, Converter converter) {
String str = StringUtilities.trimToNull(asString(from));

Expand Down

0 comments on commit 03dba88

Please sign in to comment.