Add Decimal64.{min,max}ValueIn(scale) 01/100101/1
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 13 Mar 2022 12:19:17 +0000 (13:19 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 13 Mar 2022 12:20:05 +0000 (13:20 +0100)
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 <robert.varga@pantheon.tech>
common/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/Decimal64.java

index 2f2de8837a755bfd7d889de6b05074645da2460e..1905d2c1456ba083e94afc113204989946027d82 100644 (file)
@@ -161,8 +161,18 @@ public class Decimal64 extends Number implements CanonicalValue<Decimal64> {
         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<Decimal64> {
         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);