Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added capability to reverse test, added URI, URL #100

Merged
merged 4 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion src/main/java/com/cedarsoftware/util/convert/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.UUID;
Expand All @@ -39,6 +40,7 @@

import com.cedarsoftware.util.ClassUtilities;


/**
* Instance conversion utility. Convert from primitive to other primitives, plus support for Number, Date,
* TimeStamp, SQL Date, LocalDate, LocalDateTime, ZonedDateTime, Calendar, Big*, Atomic*, Class, UUID,
Expand Down Expand Up @@ -685,7 +687,36 @@ private static void buildFactoryConversions() {
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);

CONVERSION_DB.put(pair(TimeZone.class, String.class), TimeZoneConversions::toString);

try {
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
}

// URL conversions
CONVERSION_DB.put(pair(Void.class, URL.class), VoidConversions::toNull);
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(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(String.class, TimeZone.class), StringConversions::toTimeZone);
CONVERSION_DB.put(pair(Map.class, TimeZone.class), MapConversions::toTimeZone);

// Duration conversions supported
CONVERSION_DB.put(pair(Void.class, Duration.class), VoidConversions::toNull);
CONVERSION_DB.put(pair(Duration.class, Duration.class), Converter::identity);
Expand Down
70 changes: 70 additions & 0 deletions src/main/java/com/cedarsoftware/util/convert/MapConversions.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.sql.Timestamp;
import java.time.Duration;
import java.time.Instant;
Expand Down Expand Up @@ -84,6 +87,13 @@ final class MapConversions {
private static final String ID = "id";
public static final String LANGUAGE = "language";
public static final String VARIANT = "variant";
public static final String JAR = "jar";
public static final String AUTHORITY = "authority";
public static final String REF = "ref";
public static final String PORT = "port";
public static final String FILE = "file";
public static final String HOST = "host";
public static final String PROTOCOL = "protocol";
private static String COUNTRY = "country";

private MapConversions() {}
Expand Down Expand Up @@ -183,6 +193,18 @@ static Timestamp toTimestamp(Object from, Converter converter) {
return fromValueForMultiKey(map, converter, Timestamp.class, TIMESTAMP_PARAMS);
}

private static final String[] TIMEZONE_PARAMS = new String[] { ZONE };
static TimeZone toTimeZone(Object from, Converter converter) {
Map<?, ?> map = (Map<?, ?>) from;
ConverterOptions options = converter.getOptions();

if (map.containsKey(ZONE)) {
return converter.convert(map.get(ZONE), TimeZone.class);
} else {
return fromValueForMultiKey(map, converter, TimeZone.class, TIMEZONE_PARAMS);
}
}

private static final String[] CALENDAR_PARAMS = new String[] { TIME, ZONE };
static Calendar toCalendar(Object from, Converter converter) {
Map<?, ?> map = (Map<?, ?>) from;
Expand Down Expand Up @@ -436,6 +458,54 @@ static Year toYear(Object from, Converter converter) {
return fromSingleKey(from, converter, YEAR, Year.class);
}

static URL toURL(Object from, Converter converter) {
Map<String, Object> map = (Map<String, Object>)from;
StringBuilder builder = new StringBuilder(20);

try {
if (map.containsKey(VALUE) || map.containsKey(V)) {
return fromValue(map, converter, URL.class);
}

String protocol = (String) map.get(PROTOCOL);
String host = (String) map.get(HOST);
String file = (String) map.get(FILE);
String authority = (String) map.get(AUTHORITY);
String ref = (String) map.get(REF);
Long port = (Long) map.get(PORT);

builder.append(protocol);
builder.append(':');
if (!protocol.equalsIgnoreCase(JAR)) {
builder.append("//");
}
if (authority != null && !authority.isEmpty()) {
builder.append(authority);
} else {
if (host != null && !host.isEmpty()) {
builder.append(host);
}
if (!port.equals(-1L)) {
builder.append(":" + port);
}
}
if (file != null && !file.isEmpty()) {
builder.append(file);
}
if (ref != null && !ref.isEmpty()) {
builder.append("#" + ref);
}
return URI.create(builder.toString()).toURL();
} catch (MalformedURLException e) {
throw new IllegalArgumentException("Cannot convert Map to URL. Malformed URL: '" + builder + "'");
}
}

static URI toURI(Object from, Converter converter) {
Map<?, ?> map = asMap(from);
return fromValue(map, converter, URI.class);
}

static Map<String, ?> initMap(Object from, Converter converter) {
Map<String, Object> map = new CompactLinkedMap<>();
map.put(V, from);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.sql.Timestamp;
Expand All @@ -27,6 +30,7 @@
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.Optional;
import java.util.TimeZone;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
Expand Down Expand Up @@ -241,6 +245,27 @@ static BigDecimal toBigDecimal(Object from, Converter converter) {
}
}

static URL toURL(Object from, Converter converter) {
String str = StringUtilities.trimToNull(asString(from));
if (str == null) {
return null;
}
try {
URI uri = URI.create((String) from);
return uri.toURL();
} catch (MalformedURLException mue) {
throw new IllegalArgumentException("Cannot convert String '" + str);
}
}

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

static String enumToString(Object from, Converter converter) {
return ((Enum<?>) from).name();
}
Expand Down Expand Up @@ -327,6 +352,15 @@ static Timestamp toTimestamp(Object from, Converter converter) {
return instant == null ? null : new Timestamp(instant.toEpochMilli());
}

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

return TimeZone.getTimeZone(str);
}

static Calendar toCalendar(Object from, Converter converter) {
return parseDate(from, converter).map(GregorianCalendar::from).orElse(null);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.cedarsoftware.util.convert;

import java.util.TimeZone;

public class TimeZoneConversions {
static String toString(Object from, Converter converter) {
TimeZone timezone = (TimeZone)from;
return timezone.getID();
}

}
Loading
Loading