-
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.
Making sure that times that handle nanos, support the nanos on double…
… and BigDecimal
- Loading branch information
Showing
9 changed files
with
282 additions
and
110 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
src/main/java/com/cedarsoftware/util/convert/BigDecimalConversions.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,60 @@ | ||
package com.cedarsoftware.util.convert; | ||
|
||
import java.math.BigDecimal; | ||
import java.math.BigInteger; | ||
import java.sql.Timestamp; | ||
import java.time.Instant; | ||
import java.time.LocalDateTime; | ||
import java.time.ZonedDateTime; | ||
import java.util.UUID; | ||
|
||
/** | ||
* @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 BigDecimalConversions { | ||
static Instant toInstant(Object from, Converter converter) { | ||
BigDecimal time = (BigDecimal) from; | ||
long seconds = time.longValue() / 1000; | ||
int nanos = time.remainder(BigDecimal.valueOf(1000)).multiply(BigDecimal.valueOf(1_000_000)).intValue(); | ||
return Instant.ofEpochSecond(seconds, nanos); | ||
} | ||
|
||
static LocalDateTime toLocalDateTime(Object from, Converter converter) { | ||
return toZonedDateTime(from, converter).toLocalDateTime(); | ||
} | ||
|
||
static ZonedDateTime toZonedDateTime(Object from, Converter converter) { | ||
return toInstant(from, converter).atZone(converter.getOptions().getZoneId()); | ||
} | ||
|
||
static Timestamp toTimestamp(Object from, Converter converter) { | ||
return Timestamp.from(toInstant(from, converter)); | ||
} | ||
|
||
static BigInteger toBigInteger(Object from, Converter converter) { | ||
return ((BigDecimal)from).toBigInteger(); | ||
} | ||
|
||
static String toString(Object from, Converter converter) { | ||
return ((BigDecimal) from).stripTrailingZeros().toPlainString(); | ||
} | ||
|
||
static UUID toUUID(Object from, Converter converter) { | ||
BigInteger bigInt = ((BigDecimal) from).toBigInteger(); | ||
return BigIntegerConversions.toUUID(bigInt, converter); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/com/cedarsoftware/util/convert/BigIntegerConversions.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,55 @@ | ||
package com.cedarsoftware.util.convert; | ||
|
||
import java.math.BigDecimal; | ||
import java.math.BigInteger; | ||
import java.util.UUID; | ||
|
||
/** | ||
* @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 BigIntegerConversions { | ||
static BigDecimal toBigDecimal(Object from, Converter converter) { | ||
return new BigDecimal((BigInteger)from); | ||
} | ||
|
||
static UUID toUUID(Object from, Converter converter) { | ||
BigInteger bigInteger = (BigInteger) from; | ||
if (bigInteger.signum() < 0) { | ||
throw new IllegalArgumentException("Cannot convert a negative number [" + bigInteger + "] to a UUID"); | ||
} | ||
StringBuilder hex = new StringBuilder(bigInteger.toString(16)); | ||
|
||
// Pad the string to 32 characters with leading zeros (if necessary) | ||
while (hex.length() < 32) { | ||
hex.insert(0, "0"); | ||
} | ||
|
||
// Split into two 64-bit parts | ||
String highBitsHex = hex.substring(0, 16); | ||
String lowBitsHex = hex.substring(16, 32); | ||
|
||
// Combine and format into standard UUID format | ||
String uuidString = highBitsHex.substring(0, 8) + "-" + | ||
highBitsHex.substring(8, 12) + "-" + | ||
highBitsHex.substring(12, 16) + "-" + | ||
lowBitsHex.substring(0, 4) + "-" + | ||
lowBitsHex.substring(4, 16); | ||
|
||
// Create UUID from string | ||
return UUID.fromString(uuidString); | ||
} | ||
} |
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
51 changes: 51 additions & 0 deletions
51
src/main/java/com/cedarsoftware/util/convert/DoubleConversions.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,51 @@ | ||
package com.cedarsoftware.util.convert; | ||
|
||
import java.sql.Timestamp; | ||
import java.time.Instant; | ||
import java.time.LocalDateTime; | ||
import java.time.ZonedDateTime; | ||
|
||
/** | ||
* @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 DoubleConversions { | ||
private DoubleConversions() { } | ||
|
||
static Instant toInstant(Object from, Converter converter) { | ||
double d = (Double) from; | ||
long seconds = (long) d / 1000; | ||
int nanoAdjustment = (int) ((d - seconds * 1000) * 1_000_000); | ||
return Instant.ofEpochSecond(seconds, nanoAdjustment); | ||
} | ||
|
||
static LocalDateTime toLocalDateTime(Object from, Converter converter) { | ||
return toZonedDateTime(from, converter).toLocalDateTime(); | ||
} | ||
|
||
static ZonedDateTime toZonedDateTime(Object from, Converter converter) { | ||
return toInstant(from, converter).atZone(converter.getOptions().getZoneId()); | ||
} | ||
|
||
static Timestamp toTimestamp(Object from, Converter converter) { | ||
double milliseconds = (Double) from; | ||
long millisPart = (long) milliseconds; | ||
int nanosPart = (int) ((milliseconds - millisPart) * 1_000_000); | ||
Timestamp timestamp = new Timestamp(millisPart); | ||
timestamp.setNanos(timestamp.getNanos() + nanosPart); | ||
return timestamp; | ||
} | ||
} |
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.