Skip to content

Commit

Permalink
All conversion code excluding Map conversions, has been moved to stat…
Browse files Browse the repository at this point in the history
…ic Conversions classes.
  • Loading branch information
jdereg committed Jan 28, 2024
1 parent 8e3cc4a commit cb27d25
Show file tree
Hide file tree
Showing 9 changed files with 263 additions and 232 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,84 +10,83 @@
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.concurrent.atomic.AtomicLong;

public class CalendarConversions {

static Date toDate(Object fromInstance) {
return ((Calendar)fromInstance).getTime();
static Date toDate(Object from) {
return ((Calendar)from).getTime();
}

static long toLong(Object fromInstance) {
return toDate(fromInstance).getTime();
static long toLong(Object from) {
return toDate(from).getTime();
}

static Instant toInstant(Object fromInstance) {
return ((Calendar)fromInstance).toInstant();
static Instant toInstant(Object from) {
return ((Calendar)from).toInstant();
}

static ZonedDateTime toZonedDateTime(Object fromInstance, ConverterOptions options) {
return toInstant(fromInstance).atZone(options.getZoneId());
static ZonedDateTime toZonedDateTime(Object from, ConverterOptions options) {
return toInstant(from).atZone(options.getZoneId());
}

static ZonedDateTime toZonedDateTime(Object fromInstance, Converter converter, ConverterOptions options) {
return toZonedDateTime(fromInstance, options);
static ZonedDateTime toZonedDateTime(Object from, Converter converter, ConverterOptions options) {
return toZonedDateTime(from, options);
}

static Long toLong(Object fromInstance, Converter converter, ConverterOptions options) {
return toLong(fromInstance);
static Long toLong(Object from, Converter converter, ConverterOptions options) {
return toLong(from);
}

static double toDouble(Object fromInstance, Converter converter, ConverterOptions options) {
return (double)toLong(fromInstance);
static double toDouble(Object from, Converter converter, ConverterOptions options) {
return (double)toLong(from);
}


static Date toDate(Object fromInstance, Converter converter, ConverterOptions options) {
return toDate(fromInstance);
static Date toDate(Object from, Converter converter, ConverterOptions options) {
return toDate(from);
}

static java.sql.Date toSqlDate(Object fromInstance, Converter converter, ConverterOptions options) {
return new java.sql.Date(toLong(fromInstance));
static java.sql.Date toSqlDate(Object from, Converter converter, ConverterOptions options) {
return new java.sql.Date(toLong(from));
}

static Timestamp toTimestamp(Object fromInstance, Converter converter, ConverterOptions options) {
return new Timestamp(toLong(fromInstance));
static Timestamp toTimestamp(Object from, Converter converter, ConverterOptions options) {
return new Timestamp(toLong(from));
}

static AtomicLong toAtomicLong(Object fromInstance, Converter converter, ConverterOptions options) {
return new AtomicLong(toLong(fromInstance));
static AtomicLong toAtomicLong(Object from, Converter converter, ConverterOptions options) {
return new AtomicLong(toLong(from));
}

static Instant toInstant(Object fromInstance, Converter converter, ConverterOptions options) {
return toInstant(fromInstance);
static Instant toInstant(Object from, Converter converter, ConverterOptions options) {
return toInstant(from);
}

static LocalDateTime toLocalDateTime(Object fromInstance, Converter converter, ConverterOptions options) {
return toZonedDateTime(fromInstance, options).toLocalDateTime();
static LocalDateTime toLocalDateTime(Object from, Converter converter, ConverterOptions options) {
return toZonedDateTime(from, options).toLocalDateTime();
}

static LocalDate toLocalDate(Object fromInstance, Converter converter, ConverterOptions options) {
return toZonedDateTime(fromInstance, options).toLocalDate();
static LocalDate toLocalDate(Object from, Converter converter, ConverterOptions options) {
return toZonedDateTime(from, options).toLocalDate();
}

static LocalTime toLocalTime(Object fromInstance, Converter converter, ConverterOptions options) {
return toZonedDateTime(fromInstance, options).toLocalTime();
static LocalTime toLocalTime(Object from, Converter converter, ConverterOptions options) {
return toZonedDateTime(from, options).toLocalTime();
}

static BigDecimal toBigDecimal(Object fromInstance, Converter converter, ConverterOptions options) {
return BigDecimal.valueOf(toLong(fromInstance));
static BigDecimal toBigDecimal(Object from, Converter converter, ConverterOptions options) {
return BigDecimal.valueOf(toLong(from));
}

static BigInteger toBigInteger(Object fromInstance, Converter converter, ConverterOptions options) {
return BigInteger.valueOf(toLong(fromInstance));
static BigInteger toBigInteger(Object from, Converter converter, ConverterOptions options) {
return BigInteger.valueOf(toLong(from));
}

static Calendar clone(Object fromInstance, Converter converter, ConverterOptions options) {
Calendar from = (Calendar)fromInstance;
static Calendar clone(Object from, Converter converter, ConverterOptions options) {
Calendar calendar = (Calendar)from;
// mutable class, so clone it.
return (Calendar)from.clone();
return (Calendar)calendar.clone();
}

static Calendar create(long epochMilli, ConverterOptions options) {
Expand Down
60 changes: 28 additions & 32 deletions src/main/java/com/cedarsoftware/util/convert/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ private static void buildFactoryConversions() {
DEFAULT_FACTORY.put(pair(Calendar.class, String.class), DateConversions::calendarToString);
DEFAULT_FACTORY.put(pair(Number.class, String.class), StringConversions::toString);
DEFAULT_FACTORY.put(pair(Map.class, String.class), MapConversions::toString);
DEFAULT_FACTORY.put(pair(Enum.class, String.class), (fromInstance, converter, options) -> ((Enum<?>) fromInstance).name());
DEFAULT_FACTORY.put(pair(Enum.class, String.class), StringConversions::enumToString);
DEFAULT_FACTORY.put(pair(String.class, String.class), Converter::identity);
DEFAULT_FACTORY.put(pair(Duration.class, String.class), StringConversions::toString);
DEFAULT_FACTORY.put(pair(Instant.class, String.class), StringConversions::toString);
Expand All @@ -600,7 +600,7 @@ private static void buildFactoryConversions() {
// Duration conversions supported
DEFAULT_FACTORY.put(pair(Void.class, Duration.class), VoidConversions::toNull);
DEFAULT_FACTORY.put(pair(Duration.class, Duration.class), Converter::identity);
DEFAULT_FACTORY.put(pair(String.class, Duration.class), (fromInstance, converter, options) -> Duration.parse((String) fromInstance));
DEFAULT_FACTORY.put(pair(String.class, Duration.class), StringConversions::toString);
DEFAULT_FACTORY.put(pair(Map.class, Duration.class), MapConversions::toDuration);

// Instant conversions supported
Expand All @@ -619,7 +619,6 @@ private static void buildFactoryConversions() {
DEFAULT_FACTORY.put(pair(ZonedDateTime.class, Instant.class), ZonedDateTimeConversions::toInstant);
DEFAULT_FACTORY.put(pair(Calendar.class, Instant.class), CalendarConversions::toInstant);
DEFAULT_FACTORY.put(pair(Number.class, Instant.class), NumberConversions::toInstant);

DEFAULT_FACTORY.put(pair(String.class, Instant.class), StringConversions::toInstant);
DEFAULT_FACTORY.put(pair(Map.class, Instant.class), MapConversions::toInstant);

Expand All @@ -635,10 +634,7 @@ private static void buildFactoryConversions() {
// MonthDay conversions supported
DEFAULT_FACTORY.put(pair(Void.class, MonthDay.class), VoidConversions::toNull);
DEFAULT_FACTORY.put(pair(MonthDay.class, MonthDay.class), Converter::identity);
DEFAULT_FACTORY.put(pair(String.class, MonthDay.class), (fromInstance, converter, options) -> {
String monthDay = (String) fromInstance;
return MonthDay.parse(monthDay);
});
DEFAULT_FACTORY.put(pair(String.class, MonthDay.class), StringConversions::toMonthDay);
DEFAULT_FACTORY.put(pair(Map.class, MonthDay.class), MapConversions::toMonthDay);

// Map conversions supported
Expand All @@ -662,24 +658,24 @@ private static void buildFactoryConversions() {
DEFAULT_FACTORY.put(pair(LocalDate.class, Map.class), MapConversions::initMap);
DEFAULT_FACTORY.put(pair(LocalDateTime.class, Map.class), MapConversions::initMap);
DEFAULT_FACTORY.put(pair(ZonedDateTime.class, Map.class), MapConversions::initMap);
DEFAULT_FACTORY.put(pair(Duration.class, Map.class), (fromInstance, converter, options) -> {
long sec = ((Duration) fromInstance).getSeconds();
long nanos = ((Duration) fromInstance).getNano();
DEFAULT_FACTORY.put(pair(Duration.class, Map.class), (from, converter, options) -> {
long sec = ((Duration) from).getSeconds();
long nanos = ((Duration) from).getNano();
Map<String, Object> target = new LinkedHashMap<>();
target.put("seconds", sec);
target.put("nanos", nanos);
return target;
});
DEFAULT_FACTORY.put(pair(Instant.class, Map.class), (fromInstance, converter, options) -> {
long sec = ((Instant) fromInstance).getEpochSecond();
long nanos = ((Instant) fromInstance).getNano();
DEFAULT_FACTORY.put(pair(Instant.class, Map.class), (from, converter, options) -> {
long sec = ((Instant) from).getEpochSecond();
long nanos = ((Instant) from).getNano();
Map<String, Object> target = new LinkedHashMap<>();
target.put("seconds", sec);
target.put("nanos", nanos);
return target;
});
DEFAULT_FACTORY.put(pair(LocalTime.class, Map.class), (fromInstance, converter, options) -> {
LocalTime localTime = (LocalTime) fromInstance;
DEFAULT_FACTORY.put(pair(LocalTime.class, Map.class), (from, converter, options) -> {
LocalTime localTime = (LocalTime) from;
Map<String, Object> target = new LinkedHashMap<>();
target.put("hour", localTime.getHour());
target.put("minute", localTime.getMinute());
Expand All @@ -693,8 +689,8 @@ private static void buildFactoryConversions() {
}
return target;
});
DEFAULT_FACTORY.put(pair(MonthDay.class, Map.class), (fromInstance, converter, options) -> {
MonthDay monthDay = (MonthDay) fromInstance;
DEFAULT_FACTORY.put(pair(MonthDay.class, Map.class), (from, converter, options) -> {
MonthDay monthDay = (MonthDay) from;
Map<String, Object> target = new LinkedHashMap<>();
target.put("day", monthDay.getDayOfMonth());
target.put("month", monthDay.getMonthValue());
Expand All @@ -704,8 +700,8 @@ private static void buildFactoryConversions() {
DEFAULT_FACTORY.put(pair(UUID.class, Map.class), MapConversions::initMap);
DEFAULT_FACTORY.put(pair(Calendar.class, Map.class), MapConversions::initMap);
DEFAULT_FACTORY.put(pair(Number.class, Map.class), MapConversions::initMap);
DEFAULT_FACTORY.put(pair(Map.class, Map.class), (fromInstance, converter, options) -> {
Map<?, ?> source = (Map<?, ?>) fromInstance;
DEFAULT_FACTORY.put(pair(Map.class, Map.class), (from, converter, options) -> {
Map<?, ?> source = (Map<?, ?>) from;
Map<?, ?> copy = new LinkedHashMap<>(source);
return copy;
});
Expand Down Expand Up @@ -733,7 +729,7 @@ public Converter(ConverterOptions options) {
* convert(map, double.class) // Converter will extract the value associated to the "_v" (or "value") key and convert it.
* </pre>
*
* @param fromInstance A value used to create the targetType, even though it may
* @param from A value used to create the targetType, even though it may
* not (most likely will not) be the same data type as the targetType
* @param toType Class which indicates the targeted (final) data type.
* Please note that in addition to the 8 Java primitives, the targeted class
Expand All @@ -743,8 +739,8 @@ public Converter(ConverterOptions options) {
* fields within the Map to perform the conversion.
* @return An instanceof targetType class, based upon the value passed in.
*/
public <T> T convert(Object fromInstance, Class<T> toType) {
return this.convert(fromInstance, toType, options);
public <T> T convert(Object from, Class<T> toType) {
return this.convert(from, toType, options);
}

/**
Expand All @@ -763,7 +759,7 @@ public <T> T convert(Object fromInstance, Class<T> toType) {
* convert(map, double.class) // Converter will extract the value associated to the "_v" (or "value") key and convert it.
* </pre>
*
* @param fromInstance A value used to create the targetType, even though it may
* @param from A value used to create the targetType, even though it may
* not (most likely will not) be the same data type as the targetType
* @param toType Class which indicates the targeted (final) data type.
* Please note that in addition to the 8 Java primitives, the targeted class
Expand All @@ -776,17 +772,17 @@ public <T> T convert(Object fromInstance, Class<T> toType) {
* @return An instanceof targetType class, based upon the value passed in.
*/
@SuppressWarnings("unchecked")
public <T> T convert(Object fromInstance, Class<T> toType, ConverterOptions options) {
public <T> T convert(Object from, Class<T> toType, ConverterOptions options) {
if (toType == null) {
throw new IllegalArgumentException("toType cannot be null");
}
Class<?> sourceType;
if (fromInstance == null) {
if (from == null) {
// Do not promote primitive to primitive wrapper - allows for different 'from NULL' type for each.
sourceType = Void.class;
} else {
// Promote primitive to primitive wrapper so we don't have to define so many duplicates in the factory map.
sourceType = fromInstance.getClass();
sourceType = from.getClass();
if (toType.isPrimitive()) {
toType = (Class<T>) toPrimitiveWrapperClass(toType);
}
Expand All @@ -795,7 +791,7 @@ public <T> T convert(Object fromInstance, Class<T> toType, ConverterOptions opti
// Direct Mapping
Convert<?> converter = factory.get(pair(sourceType, toType));
if (converter != null) {
return (T) converter.convert(fromInstance, this, options);
return (T) converter.convert(from, this, options);
}

// Try inheritance
Expand All @@ -805,10 +801,10 @@ public <T> T convert(Object fromInstance, Class<T> toType, ConverterOptions opti
if (!isDirectConversionSupportedFor(sourceType, toType)) {
addConversion(sourceType, toType, converter);
}
return (T) converter.convert(fromInstance, this, options);
return (T) converter.convert(from, this, options);
}

throw new IllegalArgumentException("Unsupported conversion, source type [" + name(fromInstance) + "] target type '" + getShortName(toType) + "'");
throw new IllegalArgumentException("Unsupported conversion, source type [" + name(from) + "] target type '" + getShortName(toType) + "'");
}

/**
Expand Down Expand Up @@ -910,11 +906,11 @@ static String getShortName(Class<?> type) {
return java.sql.Date.class.equals(type) ? type.getName() : type.getSimpleName();
}

static private String name(Object fromInstance) {
if (fromInstance == null) {
static private String name(Object from) {
if (from == null) {
return "null";
}
return getShortName(fromInstance.getClass()) + " (" + fromInstance + ")";
return getShortName(from.getClass()) + " (" + from + ")";
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.cedarsoftware.util.convert;

/**
* @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 class DurationConversions {

}
Loading

0 comments on commit cb27d25

Please sign in to comment.