Skip to content

Commit

Permalink
Adding Temporal
Browse files Browse the repository at this point in the history
Conversion Tests
LocalDate conversions
  • Loading branch information
kpartlow committed Jan 23, 2024
1 parent 80dc959 commit f3c18b9
Show file tree
Hide file tree
Showing 17 changed files with 1,804 additions and 788 deletions.
10 changes: 7 additions & 3 deletions src/main/java/com/cedarsoftware/util/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -457,29 +457,33 @@ public static AtomicBoolean convertToAtomicBoolean(Object fromInstance)
* @param localDate A Java LocalDate
* @return a long representing the localDate as the number of milliseconds since the
* number of milliseconds since Jan 1, 1970
* @deprecated use convert(localDate, long.class);
*/

public static long localDateToMillis(LocalDate localDate)
{
return com.cedarsoftware.util.convert.Converter.localDateToMillis(localDate, instance.getOptions().getSourceZoneId());
return instance.convert(localDate, long.class);
}

/**
* @param localDateTime A Java LocalDateTime
* @return a long representing the localDateTime as the number of milliseconds since the
* number of milliseconds since Jan 1, 1970
* @deprecated use convert(localDateTime, long.class);
*/
public static long localDateTimeToMillis(LocalDateTime localDateTime)
{
return com.cedarsoftware.util.convert.Converter.localDateTimeToMillis(localDateTime, instance.getOptions().getSourceZoneId());
return instance.convert(localDateTime, long.class);
}

/**
* @param zonedDateTime A Java ZonedDateTime
* @return a long representing the zonedDateTime as the number of milliseconds since the
* number of milliseconds since Jan 1, 1970
* @deprecated use convert(zonedDateTime, long.class);
*/
public static long zonedDateTimeToMillis(ZonedDateTime zonedDateTime)
{
return com.cedarsoftware.util.convert.Converter.zonedDateTimeToMillis(zonedDateTime);
return instance.convert(zonedDateTime, long.class);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.cedarsoftware.util.convert;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
Expand Down Expand Up @@ -61,4 +62,9 @@ static BigDecimal toBigDecimal(Object from, Converter converter, ConverterOption
AtomicBoolean b = (AtomicBoolean) from;
return b.get() ? BigDecimal.ONE : BigDecimal.ZERO;
}

public static BigInteger toBigInteger(Object from, Converter converter, ConverterOptions options) {
AtomicBoolean b = (AtomicBoolean) from;
return b.get() ? BigInteger.ONE : BigInteger.ZERO;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.cedarsoftware.util.convert;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

/**
Expand Down Expand Up @@ -38,6 +40,11 @@ static Integer toInteger(Object from, Converter converter, ConverterOptions opti
return b ? CommonValues.INTEGER_ONE : CommonValues.INTEGER_ZERO;
}

static AtomicInteger toAtomicInteger(Object from, Converter converter, ConverterOptions options) {
Boolean b = (Boolean) from;
return new AtomicInteger(b ? 1 : 0);
}

static AtomicLong toAtomicLong(Object from, Converter converter, ConverterOptions options) {
Boolean b = (Boolean) from;
return new AtomicLong(b ? 1 : 0);
Expand All @@ -58,6 +65,10 @@ static BigDecimal toBigDecimal(Object from, Converter converter, ConverterOption
return b ? BigDecimal.ONE : BigDecimal.ZERO;
}

static BigInteger toBigInteger(Object from, Converter converter, ConverterOptions options) {
return ((Boolean)from) ? BigInteger.ONE : BigInteger.ZERO;
}

static Float toFloat(Object from, Converter converter, ConverterOptions options) {
Boolean b = (Boolean) from;
return b ? CommonValues.FLOAT_ONE : CommonValues.FLOAT_ZERO;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,93 @@

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.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.concurrent.atomic.AtomicLong;

public class CalendarConversion {

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

static Calendar create(long epochMilli, ConverterOptions options) {
Calendar cal = Calendar.getInstance(options.getTimeZone());
cal.clear();
cal.setTimeInMillis(epochMilli);
return cal;
}

static Calendar create(ZonedDateTime time, ConverterOptions options) {
return GregorianCalendar.from(time);
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,73 @@
package com.cedarsoftware.util.convert;

import com.cedarsoftware.util.CaseInsensitiveMap;
import com.cedarsoftware.util.CollectionUtilities;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

public class CharacterConversion {

private CharacterConversion() {
}

static boolean toBoolean(Object from) {
char c = (char) from;
return (c == 1) || (c == 't') || (c == 'T') || (c == '1');
}


static boolean toBoolean(Object from, Converter converter, ConverterOptions options) {
Character c = (Character) from;
return c != CommonValues.CHARACTER_ZERO;
return toBoolean(from);
}

static double toDouble(Object from, Converter converter, ConverterOptions options) {
// downcasting -- not always a safe conversino
static byte toByte(Object from, Converter converter, ConverterOptions options) {
return (byte) (char) from;
}

static short toShort(Object from, Converter converter, ConverterOptions options) {
return (short) (char) from;
}

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

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

static float toFloat(Object from, Converter converter, ConverterOptions options) {
return (char) from;
}

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

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

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

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

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

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

0 comments on commit f3c18b9

Please sign in to comment.