Skip to content

Commit

Permalink
More unit tests added for conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
jdereg committed Mar 9, 2024
1 parent 3852e71 commit 9be3dbb
Show file tree
Hide file tree
Showing 6 changed files with 351 additions and 193 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,7 @@ private static void buildFactoryConversions() {
CONVERSION_DB.put(pair(Calendar.class, Map.class), CalendarConversions::toMap);
CONVERSION_DB.put(pair(Number.class, Map.class), MapConversions::initMap);
CONVERSION_DB.put(pair(Map.class, Map.class), MapConversions::toMap);
CONVERSION_DB.put(pair(Enum.class, Map.class), MapConversions::initMap);
CONVERSION_DB.put(pair(Enum.class, Map.class), EnumConversions::toMap);
CONVERSION_DB.put(pair(OffsetDateTime.class, Map.class), OffsetDateTimeConversions::toMap);
CONVERSION_DB.put(pair(Year.class, Map.class), YearConversions::toMap);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ final class DateConversions {
private DateConversions() {}

static ZonedDateTime toZonedDateTime(Object from, Converter converter) {
return Instant.ofEpochMilli(toLong(from, converter)).atZone(converter.getOptions().getZoneId());
Date date = (Date) from;
return Instant.ofEpochMilli(date.getTime()).atZone(converter.getOptions().getZoneId());
}

static long toLong(Object from, Converter converter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private DurationConversions() {}

static Map toMap(Object from, Converter converter) {
long sec = ((Duration) from).getSeconds();
long nanos = ((Duration) from).getNano();
int nanos = ((Duration) from).getNano();
Map<String, Object> target = new CompactLinkedMap<>();
target.put("seconds", sec);
target.put("nanos", nanos);
Expand Down
75 changes: 75 additions & 0 deletions src/main/java/com/cedarsoftware/util/convert/EnumConversions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.cedarsoftware.util.convert;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.time.Duration;
import java.time.Instant;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

import com.cedarsoftware.util.CompactLinkedMap;

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

private EnumConversions() {}

static Map toMap(Object from, Converter converter) {
Enum enumInstance = (Enum) from;
Map<String, Object> target = new CompactLinkedMap<>();
target.put("name", enumInstance.name());
return target;
}

static long toLong(Object from, Converter converter) {
return ((Duration) from).toMillis();
}

static AtomicLong toAtomicLong(Object from, Converter converter) {
Duration duration = (Duration) from;
return new AtomicLong(duration.toMillis());
}

static BigInteger toBigInteger(Object from, Converter converter) {
Duration duration = (Duration) from;
BigInteger epochSeconds = BigInteger.valueOf(duration.getSeconds());
BigInteger nanos = BigInteger.valueOf(duration.getNano());

// Convert seconds to nanoseconds and add the nanosecond part
return epochSeconds.multiply(BigIntegerConversions.BILLION).add(nanos);
}

static double toDouble(Object from, Converter converter) {
Duration duration = (Duration) from;
return BigDecimalConversions.secondsAndNanosToDouble(duration.getSeconds(), duration.getNano()).doubleValue();
}

static BigDecimal toBigDecimal(Object from, Converter converter) {
Duration duration = (Duration) from;
return BigDecimalConversions.secondsAndNanosToDouble(duration.getSeconds(), duration.getNano());
}

static Timestamp toTimestamp(Object from, Converter converter) {
Duration duration = (Duration) from;
Instant epoch = Instant.EPOCH;
Instant timeAfterDuration = epoch.plus(duration);
return Timestamp.from(timeAfterDuration);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ static Duration toDuration(Object from, Converter converter) {
if (map.containsKey(SECONDS)) {
ConverterOptions options = converter.getOptions();
long sec = converter.convert(map.get(SECONDS), long.class);
long nanos = converter.convert(map.get(NANOS), long.class);
int nanos = converter.convert(map.get(NANOS), int.class);
return Duration.ofSeconds(sec, nanos);
} else {
return fromValueForMultiKey(from, converter, Duration.class, DURATION_PARAMS);
Expand Down
Loading

0 comments on commit 9be3dbb

Please sign in to comment.