Skip to content

Commit

Permalink
LocalTime to numeric type conversions completed, all tests added.
Browse files Browse the repository at this point in the history
Everything test now permits check for identity and equivalence but not identical.
  • Loading branch information
jdereg committed Mar 2, 2024
1 parent 7ccfe74 commit c43b015
Show file tree
Hide file tree
Showing 13 changed files with 608 additions and 164 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ static AtomicInteger toAtomicInteger(Object from, Converter converter) {
return b.get() ? new AtomicInteger(1) : new AtomicInteger (0);
}


static AtomicLong toAtomicLong(Object from, Converter converter) {
AtomicBoolean b = (AtomicBoolean) from;
return b.get() ? new AtomicLong(1) : new AtomicLong(0);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.cedarsoftware.util.convert;

import java.time.LocalTime;
import java.util.concurrent.atomic.AtomicInteger;

/**
* @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.
*/
final class AtomicIntegerConversions {

private AtomicIntegerConversions() {}

static AtomicInteger toAtomicInteger(Object from, Converter converter) {
AtomicInteger atomicInt = (AtomicInteger) from;
return new AtomicInteger(atomicInt.intValue());
}

static LocalTime toLocalTime(Object from, Converter converter) {
AtomicInteger atomicInteger= (AtomicInteger) from;
return LongConversions.toLocalTime((long)atomicInteger.get(), converter);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.cedarsoftware.util.convert;

import java.time.LocalTime;
import java.util.concurrent.atomic.AtomicLong;

/**
* @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.
*/
final class AtomicLongConversions {

private AtomicLongConversions() {}

static AtomicLong toAtomicLong(Object from, Converter converter) {
AtomicLong atomicLong = (AtomicLong) from;
return new AtomicLong(atomicLong.get());
}

static LocalTime toLocalTime(Object from, Converter converter) {
AtomicLong atomicLong = (AtomicLong) from;
return LongConversions.toLocalTime(atomicLong.get(), converter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
import java.util.Date;
Expand Down Expand Up @@ -46,6 +47,17 @@ static Duration toDuration(Object from, Converter converter) {
return Duration.ofSeconds(seconds.longValue(), nanos.movePointRight(9).longValue());
}

static LocalTime toLocalTime(Object from, Converter converter) {
BigDecimal seconds = (BigDecimal) from;
BigDecimal nanos = seconds.multiply(BigDecimal.valueOf(1_000_000_000));
try {
return LocalTime.ofNanoOfDay(nanos.longValue());
}
catch (Exception e) {
throw new IllegalArgumentException("Input value [" + seconds.toPlainString() + "] for conversion to LocalTime must be >= 0 && <= 86399.999999999", e);
}
}

static LocalDate toLocalDate(Object from, Converter converter) {
return toZonedDateTime(from, converter).toLocalDate();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
import java.util.Date;
Expand Down Expand Up @@ -81,6 +82,15 @@ static ZonedDateTime toZonedDateTime(Object from, Converter converter) {
return toInstant(from, converter).atZone(converter.getOptions().getZoneId());
}

static LocalTime toLocalTime(Object from, Converter converter) {
BigInteger bigI = (BigInteger) from;
try {
return LocalTime.ofNanoOfDay(bigI.longValue());
} catch (Exception e) {
throw new IllegalArgumentException("Input value [" + bigI + "] for conversion to LocalTime must be >= 0 && <= 86399999999999", e);
}
}

static LocalDate toLocalDate(Object from, Converter converter) {
return toZonedDateTime(from, converter).toLocalDate();
}
Expand Down
26 changes: 16 additions & 10 deletions src/main/java/com/cedarsoftware/util/convert/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ private static void buildFactoryConversions() {
CONVERSION_DB.put(pair(Number.class, Integer.class), NumberConversions::toInt);
CONVERSION_DB.put(pair(Map.class, Integer.class), MapConversions::toInt);
CONVERSION_DB.put(pair(String.class, Integer.class), StringConversions::toInt);
CONVERSION_DB.put(pair(LocalTime.class, Integer.class), LocalTimeConversions::toInteger);
CONVERSION_DB.put(pair(Year.class, Integer.class), YearConversions::toInt);

// toLong
Expand All @@ -184,6 +185,7 @@ private static void buildFactoryConversions() {
CONVERSION_DB.put(pair(Instant.class, Long.class), InstantConversions::toLong);
CONVERSION_DB.put(pair(Duration.class, Long.class), DurationConversions::toLong);
CONVERSION_DB.put(pair(LocalDate.class, Long.class), LocalDateConversions::toLong);
CONVERSION_DB.put(pair(LocalTime.class, Long.class), LocalTimeConversions::toLong);
CONVERSION_DB.put(pair(LocalDateTime.class, Long.class), LocalDateTimeConversions::toLong);
CONVERSION_DB.put(pair(OffsetDateTime.class, Long.class), OffsetDateTimeConversions::toLong);
CONVERSION_DB.put(pair(ZonedDateTime.class, Long.class), ZonedDateTimeConversions::toLong);
Expand Down Expand Up @@ -227,6 +229,7 @@ private static void buildFactoryConversions() {
CONVERSION_DB.put(pair(Character.class, Double.class), CharacterConversions::toDouble);
CONVERSION_DB.put(pair(Duration.class, Double.class), DurationConversions::toDouble);
CONVERSION_DB.put(pair(Instant.class, Double.class), InstantConversions::toDouble);
CONVERSION_DB.put(pair(LocalTime.class, Double.class), LocalTimeConversions::toDouble);
CONVERSION_DB.put(pair(LocalDate.class, Double.class), LocalDateConversions::toDouble);
CONVERSION_DB.put(pair(LocalDateTime.class, Double.class), LocalDateTimeConversions::toDouble);
CONVERSION_DB.put(pair(ZonedDateTime.class, Double.class), ZonedDateTimeConversions::toDouble);
Expand Down Expand Up @@ -305,6 +308,7 @@ private static void buildFactoryConversions() {
CONVERSION_DB.put(pair(Timestamp.class, BigInteger.class), TimestampConversions::toBigInteger);
CONVERSION_DB.put(pair(Duration.class, BigInteger.class), DurationConversions::toBigInteger);
CONVERSION_DB.put(pair(Instant.class, BigInteger.class), InstantConversions::toBigInteger);
CONVERSION_DB.put(pair(LocalTime.class, BigInteger.class), LocalTimeConversions::toBigInteger);
CONVERSION_DB.put(pair(LocalDate.class, BigInteger.class), LocalDateConversions::toBigInteger);
CONVERSION_DB.put(pair(LocalDateTime.class, BigInteger.class), LocalDateTimeConversions::toBigInteger);
CONVERSION_DB.put(pair(ZonedDateTime.class, BigInteger.class), ZonedDateTimeConversions::toBigInteger);
Expand Down Expand Up @@ -336,6 +340,7 @@ private static void buildFactoryConversions() {
CONVERSION_DB.put(pair(Timestamp.class, BigDecimal.class), TimestampConversions::toBigDecimal);
CONVERSION_DB.put(pair(Instant.class, BigDecimal.class), InstantConversions::toBigDecimal);
CONVERSION_DB.put(pair(Duration.class, BigDecimal.class), DurationConversions::toBigDecimal);
CONVERSION_DB.put(pair(LocalTime.class, BigDecimal.class), LocalTimeConversions::toBigDecimal);
CONVERSION_DB.put(pair(LocalDate.class, BigDecimal.class), LocalDateConversions::toBigDecimal);
CONVERSION_DB.put(pair(LocalDateTime.class, BigDecimal.class), LocalDateTimeConversions::toBigDecimal);
CONVERSION_DB.put(pair(ZonedDateTime.class, BigDecimal.class), ZonedDateTimeConversions::toBigDecimal);
Expand Down Expand Up @@ -379,9 +384,10 @@ private static void buildFactoryConversions() {
CONVERSION_DB.put(pair(Character.class, AtomicInteger.class), CharacterConversions::toAtomicInteger);
CONVERSION_DB.put(pair(BigInteger.class, AtomicInteger.class), NumberConversions::toAtomicInteger);
CONVERSION_DB.put(pair(BigDecimal.class, AtomicInteger.class), NumberConversions::toAtomicInteger);
CONVERSION_DB.put(pair(AtomicInteger.class, AtomicInteger.class), NumberConversions::toAtomicInteger);
CONVERSION_DB.put(pair(AtomicInteger.class, AtomicInteger.class), AtomicIntegerConversions::toAtomicInteger);
CONVERSION_DB.put(pair(AtomicBoolean.class, AtomicInteger.class), AtomicBooleanConversions::toAtomicInteger);
CONVERSION_DB.put(pair(AtomicLong.class, AtomicInteger.class), NumberConversions::toAtomicInteger);
CONVERSION_DB.put(pair(LocalTime.class, AtomicInteger.class), LocalTimeConversions::toAtomicInteger);
CONVERSION_DB.put(pair(LocalDate.class, AtomicInteger.class), LocalDateConversions::toAtomicLong);
CONVERSION_DB.put(pair(Number.class, AtomicBoolean.class), NumberConversions::toAtomicInteger);
CONVERSION_DB.put(pair(Map.class, AtomicInteger.class), MapConversions::toAtomicInteger);
Expand All @@ -401,14 +407,15 @@ private static void buildFactoryConversions() {
CONVERSION_DB.put(pair(BigInteger.class, AtomicLong.class), NumberConversions::toAtomicLong);
CONVERSION_DB.put(pair(BigDecimal.class, AtomicLong.class), NumberConversions::toAtomicLong);
CONVERSION_DB.put(pair(AtomicBoolean.class, AtomicLong.class), AtomicBooleanConversions::toAtomicLong);
CONVERSION_DB.put(pair(AtomicLong.class, AtomicLong.class), Converter::identity);
CONVERSION_DB.put(pair(AtomicInteger.class, AtomicLong.class), NumberConversions::toAtomicLong);
CONVERSION_DB.put(pair(AtomicLong.class, AtomicLong.class), AtomicLongConversions::toAtomicLong);
CONVERSION_DB.put(pair(Date.class, AtomicLong.class), DateConversions::toAtomicLong);
CONVERSION_DB.put(pair(java.sql.Date.class, AtomicLong.class), DateConversions::toAtomicLong);
CONVERSION_DB.put(pair(Timestamp.class, AtomicLong.class), DateConversions::toAtomicLong);
CONVERSION_DB.put(pair(Instant.class, AtomicLong.class), InstantConversions::toAtomicLong);
CONVERSION_DB.put(pair(Duration.class, AtomicLong.class), DurationConversions::toAtomicLong);
CONVERSION_DB.put(pair(LocalDate.class, AtomicLong.class), LocalDateConversions::toAtomicLong);
CONVERSION_DB.put(pair(LocalTime.class, AtomicLong.class), LocalTimeConversions::toAtomicLong);
CONVERSION_DB.put(pair(LocalDateTime.class, AtomicLong.class), LocalDateTimeConversions::toAtomicLong);
CONVERSION_DB.put(pair(ZonedDateTime.class, AtomicLong.class), ZonedDateTimeConversions::toAtomicLong);
CONVERSION_DB.put(pair(OffsetDateTime.class, AtomicLong.class), OffsetDateTimeConversions::toAtomicLong);
Expand Down Expand Up @@ -567,13 +574,13 @@ private static void buildFactoryConversions() {
CONVERSION_DB.put(pair(Void.class, LocalTime.class), VoidConversions::toNull);
CONVERSION_DB.put(pair(Byte.class, LocalTime.class), UNSUPPORTED);
CONVERSION_DB.put(pair(Short.class, LocalTime.class), UNSUPPORTED);
CONVERSION_DB.put(pair(Integer.class, LocalTime.class), UNSUPPORTED);
CONVERSION_DB.put(pair(Long.class, LocalTime.class), NumberConversions::toLocalTime);
CONVERSION_DB.put(pair(Double.class, LocalTime.class), NumberConversions::toLocalTime);
CONVERSION_DB.put(pair(BigInteger.class, LocalTime.class), NumberConversions::toLocalTime);
CONVERSION_DB.put(pair(BigDecimal.class, LocalTime.class), NumberConversions::toLocalDateTime);
CONVERSION_DB.put(pair(AtomicInteger.class, LocalTime.class), UNSUPPORTED);
CONVERSION_DB.put(pair(AtomicLong.class, LocalTime.class), NumberConversions::toLocalTime);
CONVERSION_DB.put(pair(Integer.class, LocalTime.class), IntegerConversions::toLocalTime);
CONVERSION_DB.put(pair(Long.class, LocalTime.class), LongConversions::toLocalTime);
CONVERSION_DB.put(pair(Double.class, LocalTime.class), DoubleConversions::toLocalTime);
CONVERSION_DB.put(pair(BigInteger.class, LocalTime.class), BigIntegerConversions::toLocalTime);
CONVERSION_DB.put(pair(BigDecimal.class, LocalTime.class), BigDecimalConversions::toLocalTime);
CONVERSION_DB.put(pair(AtomicInteger.class, LocalTime.class), AtomicIntegerConversions::toLocalTime);
CONVERSION_DB.put(pair(AtomicLong.class, LocalTime.class), AtomicLongConversions::toLocalTime);
CONVERSION_DB.put(pair(java.sql.Date.class, LocalTime.class), DateConversions::toLocalTime);
CONVERSION_DB.put(pair(Timestamp.class, LocalTime.class), DateConversions::toLocalTime);
CONVERSION_DB.put(pair(Date.class, LocalTime.class), DateConversions::toLocalTime);
Expand All @@ -584,7 +591,6 @@ private static void buildFactoryConversions() {
CONVERSION_DB.put(pair(ZonedDateTime.class, LocalTime.class), ZonedDateTimeConversions::toLocalTime);
CONVERSION_DB.put(pair(OffsetDateTime.class, LocalTime.class), OffsetDateTimeConversions::toLocalTime);
CONVERSION_DB.put(pair(Calendar.class, LocalTime.class), CalendarConversions::toLocalTime);
CONVERSION_DB.put(pair(Number.class, LocalTime.class), NumberConversions::toLocalTime);
CONVERSION_DB.put(pair(Map.class, LocalTime.class), MapConversions::toLocalTime);
CONVERSION_DB.put(pair(String.class, LocalTime.class), StringConversions::toLocalTime);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
import java.util.Date;
Expand Down Expand Up @@ -46,6 +47,17 @@ static Date toSqlDate(Object from, Converter converter) {
return new java.sql.Date((long)(d * 1000));
}

static LocalTime toLocalTime(Object from, Converter converter) {
double seconds = (double) from;
double nanos = seconds * 1_000_000_000.0;
try {
return LocalTime.ofNanoOfDay((long)nanos);
}
catch (Exception e) {
throw new IllegalArgumentException("Input value [" + seconds + "] for conversion to LocalTime must be >= 0 && <= 86399.999999999", e);
}
}

static LocalDate toLocalDate(Object from, Converter converter) {
return toZonedDateTime(from, converter).toLocalDate();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.cedarsoftware.util.convert;

import java.time.LocalTime;

/**
* @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.
*/
final class IntegerConversions {

private IntegerConversions() {}

static LocalTime toLocalTime(Object from, Converter converter) {
int ms = (Integer) from;
return LongConversions.toLocalTime((long)ms, converter);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package com.cedarsoftware.util.convert;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

import com.cedarsoftware.util.CompactLinkedMap;

Expand All @@ -24,6 +29,7 @@
* limitations under the License.
*/
final class LocalTimeConversions {
static final BigDecimal BILLION = BigDecimal.valueOf(1_000_000_000);

private LocalTimeConversions() {}

Expand All @@ -43,6 +49,39 @@ static Map<String, Object> toMap(Object from, Converter converter) {
return target;
}

static int toInteger(Object from, Converter converter) {
LocalTime lt = (LocalTime) from;
return (int) (lt.toNanoOfDay() / 1_000_000); // Convert nanoseconds to milliseconds.
}

static long toLong(Object from, Converter converter) {
LocalTime lt = (LocalTime) from;
return lt.toNanoOfDay() / 1_000_000; // Convert nanoseconds to milliseconds.
}

static double toDouble(Object from, Converter converter) {
LocalTime lt = (LocalTime) from;
return lt.toNanoOfDay() / 1_000_000_000.0;
}

static BigInteger toBigInteger(Object from, Converter converter) {
LocalTime lt = (LocalTime) from;
return BigInteger.valueOf(lt.toNanoOfDay());
}

static BigDecimal toBigDecimal(Object from, Converter converter) {
LocalTime lt = (LocalTime) from;
return new BigDecimal(lt.toNanoOfDay()).divide(BILLION, 9, RoundingMode.HALF_UP);
}

static AtomicInteger toAtomicInteger(Object from, Converter converter) {
return new AtomicInteger((int)toLong(from, converter));
}

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

static String toString(Object from, Converter converter) {
LocalTime localTime = (LocalTime) from;
return localTime.format(DateTimeFormatter.ISO_LOCAL_TIME);
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/com/cedarsoftware/util/convert/LongConversions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.cedarsoftware.util.convert;

import java.time.LocalTime;

/**
* @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.
*/
final class LongConversions {

private LongConversions() {}

static LocalTime toLocalTime(Object from, Converter converter) {
long millis = (Long) from;
try {
return LocalTime.ofNanoOfDay(millis * 1_000_000);
} catch (Exception e) {
throw new IllegalArgumentException("Input value [" + millis + "] for conversion to LocalTime must be >= 0 && <= 86399999", e);
}
}
}
Loading

0 comments on commit c43b015

Please sign in to comment.