Skip to content

Commit

Permalink
Merge pull request #91 from kpartlow/master
Browse files Browse the repository at this point in the history
Moved over more types OffsetDateTime, OffsetTime, Year, added tests.
  • Loading branch information
jdereg authored Jan 31, 2024
2 parents 46b62a7 + 88ceecb commit f35b995
Show file tree
Hide file tree
Showing 17 changed files with 644 additions and 73 deletions.
19 changes: 19 additions & 0 deletions src/main/java/com/cedarsoftware/util/ClassUtilities.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.cedarsoftware.util;

import com.cedarsoftware.util.convert.StringConversions;

import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -290,4 +293,20 @@ else if (className.equals("[C"))
}
return currentClass;
}

public static boolean isClassFinal(Class<?> c) {
return (c.getModifiers() & Modifier.FINAL) != 0;
}

public static boolean areAllConstructorsPrivate(Class<?> c) {
Constructor<?>[] constructors = c.getDeclaredConstructors();

for (Constructor<?> constructor : constructors) {
if ((constructor.getModifiers() & Modifier.PRIVATE) == 0) {
return false;
}
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -115,4 +116,9 @@ static Calendar create(long epochMilli, ConverterOptions options) {
cal.setTimeInMillis(epochMilli);
return cal;
}

static String toString(Object from, Converter converter, ConverterOptions options) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
return simpleDateFormat.format(((Calendar) from).getTime());
}
}
70 changes: 64 additions & 6 deletions src/main/java/com/cedarsoftware/util/convert/Converter.java

Large diffs are not rendered by default.

24 changes: 0 additions & 24 deletions src/main/java/com/cedarsoftware/util/convert/DateConversions.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,28 +114,4 @@ static String timestampToString(Object from, Converter converter, ConverterOptio
return simpleDateFormat.format(((Date) from));
}

static String calendarToString(Object from, Converter converter, ConverterOptions options) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
return simpleDateFormat.format(((Calendar) from).getTime());
}

static String localDateToString(Object from, Converter converter, ConverterOptions options) {
LocalDate localDate = (LocalDate) from;
return localDate.format(DateTimeFormatter.ISO_LOCAL_DATE);
}

static String localTimeToString(Object from, Converter converter, ConverterOptions options) {
LocalTime localTime = (LocalTime) from;
return localTime.format(DateTimeFormatter.ISO_LOCAL_TIME);
}

static String localDateTimeToString(Object from, Converter converter, ConverterOptions options) {
LocalDateTime localDateTime = (LocalDateTime) from;
return localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
}

static String zonedDateTimeToString(Object from, Converter converter, ConverterOptions options) {
ZonedDateTime zonedDateTime = (ZonedDateTime) from;
return zonedDateTime.format(DateTimeFormatter.ISO_DATE_TIME);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
Expand Down Expand Up @@ -116,4 +117,11 @@ static BigInteger toBigInteger(Object from, Converter converter, ConverterOption
static BigDecimal toBigDecimal(Object from, Converter converter, ConverterOptions options) {
return BigDecimal.valueOf(toLong(from, options));
}

static String toString(Object from, Converter converter, ConverterOptions options) {
LocalDate localDate = (LocalDate) from;
return localDate.format(DateTimeFormatter.ISO_LOCAL_DATE);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
Expand Down Expand Up @@ -100,4 +101,9 @@ static BigInteger toBigInteger(Object from, Converter converter, ConverterOption
static BigDecimal toBigDecimal(Object from, Converter converter, ConverterOptions options) {
return BigDecimal.valueOf(toLong(from, options));
}

static String toString(Object from, Converter converter, ConverterOptions options) {
LocalDateTime localDateTime = (LocalDateTime) from;
return localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.cedarsoftware.util.convert;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;

import com.cedarsoftware.util.CompactLinkedMap;
Expand Down Expand Up @@ -41,4 +42,9 @@ static Map<String, Object> toMap(Object from, Converter converter, ConverterOpti
}
return target;
}

static String toString(Object from, Converter converter, ConverterOptions options) {
LocalTime localTime = (LocalTime) from;
return localTime.format(DateTimeFormatter.ISO_LOCAL_TIME);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.cedarsoftware.util.convert;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Stream;

public class OffsetDateTimeConversions {
private OffsetDateTimeConversions() {}

static OffsetDateTime toDifferentZone(Object from, ConverterOptions options) {
OffsetDateTime offsetDateTime = (OffsetDateTime) from;
return offsetDateTime.toInstant().atZone(options.getZoneId()).toOffsetDateTime();
}

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

static long toLong(Object from) {
return toInstant(from).toEpochMilli();
}

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

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

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

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

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

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

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

static Calendar toCalendar(Object from, Converter converter, ConverterOptions options) {
Calendar calendar = Calendar.getInstance(options.getTimeZone());
calendar.setTimeInMillis(toLong(from));
return calendar;
}

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

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

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

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

static OffsetTime toOffsetTime(Object from, Converter converter, ConverterOptions options) {
OffsetDateTime dateTime = (OffsetDateTime) from;
return dateTime.toOffsetTime();
}

static String toString(Object from, Converter converter, ConverterOptions options) {
OffsetDateTime offsetDateTime = (OffsetDateTime) from;
return offsetDateTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.MonthDay;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.Year;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
Expand Down Expand Up @@ -328,14 +332,14 @@ static LocalDateTime toLocalDateTime(Object from, Converter converter, Converter
}

static LocalTime toLocalTime(Object from, Converter converter, ConverterOptions options) {
String str = (String) from;
if (StringUtilities.isEmpty(str)) {
String str = StringUtilities.trimToNull(asString(from));
if (str == null) {
return null;
}

try {
return LocalTime.parse(str);
}
catch (Exception e) {
} catch (Exception e) {
ZonedDateTime zdt = DateUtilities.parseDate(str, options.getSourceZoneIdForLocalDates(), true);
return zdt.toLocalTime();
}
Expand All @@ -354,6 +358,32 @@ static ZonedDateTime toZonedDateTime(Object from, Converter converter, Converter
return toZonedDateTime(from, options);
}

static OffsetDateTime toOffsetDateTime(Object from, Converter converter, ConverterOptions options) {
String s = StringUtilities.trimToNull(asString(from));
if (s == null) {
return null;
}

try {
return OffsetDateTime.parse(s, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
} catch (Exception e) {
return toZonedDateTime(from, options).toOffsetDateTime();
}
}

static OffsetTime toOffsetTime(Object from, Converter converter, ConverterOptions options) {
String s = StringUtilities.trimToNull(asString(from));
if (s == null) {
return null;
}

try {
return OffsetTime.parse(s, DateTimeFormatter.ISO_OFFSET_TIME);
} catch (Exception e) {
return toZonedDateTime(from, options).toOffsetDateTime().toOffsetTime();
}
}

static Instant toInstant(Object from, Converter converter, ConverterOptions options) {
String s = StringUtilities.trimToNull(asString(from));
if (s == null) {
Expand Down Expand Up @@ -421,4 +451,14 @@ static StringBuilder toStringBuilder(Object from, Converter converter, Converter
return from == null ? null : new StringBuilder(from.toString());
}

static Year toYear(Object from, Converter converter, ConverterOptions options) {
String s = StringUtilities.trimToNull(asString(from));
if (s == null) {
return null;
}

return Year.of(Integer.parseInt(s));
}


}
66 changes: 66 additions & 0 deletions src/main/java/com/cedarsoftware/util/convert/YearConversions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.cedarsoftware.util.convert;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Year;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

public class YearConversions {
private YearConversions() {}

static int toInt(Object from) {
return ((Year)from).getValue();
}

static long toLong(Object from, Converter converter, ConverterOptions options) {
return toInt(from);
}

static int toInt(Object from, Converter converter, ConverterOptions options) {
return toInt(from);
}

static AtomicInteger toAtomicInteger(Object from, Converter converter, ConverterOptions options) {
return new AtomicInteger(toInt(from));
}

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

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

static boolean toBoolean(Object from, Converter converter, ConverterOptions options) {
return toInt(from) == 0;
}

static AtomicBoolean toAtomicBoolean(Object from, Converter converter, ConverterOptions options) {
return new AtomicBoolean(toInt(from) == 0);
}

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

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

static String toString(Object from, Converter converter, ConverterOptions options) {
return ((Year)from).toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
Expand Down Expand Up @@ -94,4 +95,10 @@ static BigInteger toBigInteger(Object from, Converter converter, ConverterOption
static BigDecimal toBigDecimal(Object from, Converter converter, ConverterOptions options) {
return BigDecimal.valueOf(toLong(from));
}

static String toString(Object from, Converter converter, ConverterOptions options) {
ZonedDateTime zonedDateTime = (ZonedDateTime) from;
return zonedDateTime.format(DateTimeFormatter.ISO_DATE_TIME);
}

}
Loading

0 comments on commit f35b995

Please sign in to comment.