-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More unit tests added for conversions
- Loading branch information
Showing
6 changed files
with
351 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/main/java/com/cedarsoftware/util/convert/EnumConversions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.