From: Robert Varga Date: Sun, 13 Mar 2022 12:19:17 +0000 (+0100) Subject: Add Decimal64.{min,max}ValueIn(scale) X-Git-Tag: v8.0.0~4 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=yangtools.git;a=commitdiff_plain;h=26ba18ba986bad723b13b90161daf274d965de3a Add Decimal64.{min,max}ValueIn(scale) There are few places where we are interested in limits supported by Decimal64, make sure to expose them. JIRA: YANGTOOLS-1405 Change-Id: I7629e6996d6743628188141f9587c20e565736ae Signed-off-by: Robert Varga --- diff --git a/common/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/Decimal64.java b/common/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/Decimal64.java index 2f2de8837a..1905d2c145 100644 --- a/common/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/Decimal64.java +++ b/common/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/Decimal64.java @@ -161,8 +161,18 @@ public class Decimal64 extends Number implements CanonicalValue { 1000000000000000000L }; + private static final Decimal64[] MIN_VALUE; + private static final Decimal64[] MAX_VALUE; + static { verify(SCALE.length == MAX_SCALE); + + MIN_VALUE = new Decimal64[MAX_SCALE]; + MAX_VALUE = new Decimal64[MAX_SCALE]; + for (byte i = 0; i < MAX_SCALE; ++i) { + MIN_VALUE[i] = new Decimal64(i, -9223372036854775808L); + MAX_VALUE[i] = new Decimal64(i, 9223372036854775807L); + } } private final byte scaleOffset; @@ -197,6 +207,28 @@ public class Decimal64 extends Number implements CanonicalValue { return new Decimal64(offsetOf(scale), unscaledValue); } + /** + * Return the minimum value supported in specified scale. + * + * @param scale scale to use + * @return Minimum value in that scale + * @throws IllegalArgumentException if {@code scale} is not in range {@code [1..18]} + */ + public static Decimal64 minValueIn(final int scale) { + return MIN_VALUE[offsetOf(scale)]; + } + + /** + * Return the maximum value supported in specified scale. + * + * @param scale scale to use + * @return Maximum value in that scale + * @throws IllegalArgumentException if {@code scale} is not in range {@code [1..18]} + */ + public static Decimal64 maxValueIn(final int scale) { + return MAX_VALUE[offsetOf(scale)]; + } + // >>> FIXME: these need to take a scale value and perform a range check. we also need truncating counterparts public static Decimal64 valueOf(final byte byteVal) { return byteVal < 0 ? new Decimal64(1, -byteVal, 0, true) : new Decimal64(1, byteVal, 0, false);