Add UintConversions 13/84213/2
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 5 Sep 2019 07:56:46 +0000 (09:56 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 5 Sep 2019 09:02:12 +0000 (11:02 +0200)
This adds utility conversion methods for Java/Guava equivalents,
so that transitioning to/from these is easier on the users.

JIRA: YANGTOOLS-1018
Change-Id: Ibcd0cd908cd85c0f038206f96e667a144569582b
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/Uint16.java
yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/Uint32.java
yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/Uint64.java
yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/Uint8.java
yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/UintConversions.java [new file with mode: 0644]
yang/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/Uint32Test.java
yang/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/Uint64Test.java

index 8a2812f2fe55441a2024a8cff5d03cfe4e6de3aa..a3ee5ff4e9c8525d39ba6b3484e08747faff95d5 100644 (file)
@@ -279,6 +279,15 @@ public class Uint16 extends Number implements CanonicalValue<Uint16> {
         return intValue() < CACHE_SIZE ? this : INTERNER.intern(this);
     }
 
+    /**
+     * Convert this value to an {@code int}.
+     *
+     * @return An int
+     */
+    public final int toJava() {
+        return intValue();
+    }
+
     @Override
     public final int hashCode() {
         return Short.hashCode(value);
index fc776edd55ef72ce5595ba830a5218afb65313b2..1fd3e3f10909657a0a72678e192ecdb67b5e67d4 100644 (file)
@@ -148,7 +148,7 @@ public class Uint32 extends Number implements CanonicalValue<Uint32> {
      *
      * @param longVal long value
      * @return A Uint8 instance
-     * @throws IllegalArgumentException if intVal is less than zero or greater than 4294967295
+     * @throws IllegalArgumentException if longVal is less than zero or greater than 4294967295
      */
     public static Uint32 valueOf(final long longVal) {
         checkArgument(longVal >= MIN_VALUE_LONG && longVal <= MAX_VALUE_LONG, "Value %s is outside of allowed range",
@@ -258,15 +258,6 @@ public class Uint32 extends Number implements CanonicalValue<Uint32> {
         return longValue();
     }
 
-    /**
-     * Convert this value to an {@link UnsignedInteger}.
-     *
-     * @return An UnsignedInteger instance
-     */
-    public final UnsignedInteger toUnsignedInteger() {
-        return UnsignedInteger.fromIntBits(value);
-    }
-
     @Override
     @SuppressWarnings("checkstyle:parameterName")
     public final int compareTo(final Uint32 o) {
@@ -292,6 +283,24 @@ public class Uint32 extends Number implements CanonicalValue<Uint32> {
         return value >= 0 && value < CACHE_SIZE ? this : INTERNER.intern(this);
     }
 
+    /**
+     * Convert this value to a {@code long}.
+     *
+     * @return A long
+     */
+    public final long toJava() {
+        return longValue();
+    }
+
+    /**
+     * Convert this value to an {@link UnsignedInteger}.
+     *
+     * @return An UnsignedInteger instance
+     */
+    public final UnsignedInteger toGuava() {
+        return UnsignedInteger.fromIntBits(value);
+    }
+
     @Override
     public final int hashCode() {
         return Integer.hashCode(value);
index 8896e2cf08da3c1661041b92b563203425ba3852..c7793cf12f8def4ae980b832e68880abb4594fe3 100644 (file)
@@ -148,7 +148,7 @@ public class Uint64 extends Number implements CanonicalValue<Uint64> {
      *
      * @param longVal long value
      * @return A Uint8 instance
-     * @throws IllegalArgumentException if intVal is less than zero
+     * @throws IllegalArgumentException if longVal is less than zero
      */
     public static Uint64 valueOf(final long longVal) {
         checkArgument(longVal >= MIN_VALUE_LONG, "Negative values are not allowed");
@@ -193,7 +193,7 @@ public class Uint64 extends Number implements CanonicalValue<Uint64> {
      *
      * @param ulong UnsignedLong value
      * @return A Uint64 instance
-     * @throws NullPointerException if uint is null
+     * @throws NullPointerException if ulong is null
      */
     public static Uint64 valueOf(final UnsignedLong ulong) {
         return instanceFor(ulong.longValue());
@@ -204,7 +204,7 @@ public class Uint64 extends Number implements CanonicalValue<Uint64> {
      *
      * @param bigInt BigInteger value
      * @return A Uint64 instance
-     * @throws NullPointerException if uint is null
+     * @throws NullPointerException if bigInt is null
      * @throws IllegalArgumentException if bigInt is less than zero or greater than 18446744073709551615
      */
     public static Uint64 valueOf(final BigInteger bigInt) {
@@ -272,15 +272,6 @@ public class Uint64 extends Number implements CanonicalValue<Uint64> {
         return UnsignedLong.fromLongBits(value).doubleValue();
     }
 
-    /**
-     * Convert this value to an {@link UnsignedLong}.
-     *
-     * @return An UnsignedLong instance
-     */
-    public final UnsignedLong toUnsignedLong() {
-        return UnsignedLong.fromLongBits(value);
-    }
-
     @Override
     @SuppressWarnings("checkstyle:parameterName")
     public final int compareTo(final Uint64 o) {
@@ -306,6 +297,24 @@ public class Uint64 extends Number implements CanonicalValue<Uint64> {
         return value >= 0 && value < CACHE_SIZE ? this : INTERNER.intern(this);
     }
 
+    /**
+     * Convert this value to a {@link BigInteger}.
+     *
+     * @return A BigInteger instance
+     */
+    public final BigInteger toJava() {
+        // FIXME: ditch the Guava transition
+        return toGuava().bigIntegerValue();
+    }
+
+    /**
+     * Convert this value to an {@link UnsignedLong}.
+     *
+     * @return An UnsignedLong instance
+     */
+    public final UnsignedLong toGuava() {
+        return UnsignedLong.fromLongBits(value);
+    }
 
     @Override
     public final int hashCode() {
index f3aaa4f480e54d91a459fe35ff63f8528164f360..24582406d73834b27c2015e0d94ed75d208678e0 100644 (file)
@@ -254,6 +254,15 @@ public class Uint8 extends Number implements CanonicalValue<Uint8> {
         return SUPPORT;
     }
 
+    /**
+     * Convert this value to a {@code short}.
+     *
+     * @return A short
+     */
+    public final short toJava() {
+        return shortValue();
+    }
+
     @Override
     public final int hashCode() {
         return Byte.hashCode(value);
diff --git a/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/UintConversions.java b/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/UintConversions.java
new file mode 100644 (file)
index 0000000..67e8f9f
--- /dev/null
@@ -0,0 +1,105 @@
+/*
+ * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.common;
+
+import com.google.common.annotations.Beta;
+import com.google.common.primitives.UnsignedInteger;
+import com.google.common.primitives.UnsignedLong;
+import java.math.BigInteger;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+
+/**
+ * Utility methods for converting Java and Guava integer types to their {@link Uint8}, {@link Uint16}, {@link Uint32}
+ * and {@link Uint64} equivalents. While individual types provide these through their {@code valueOf()} methods, this
+ * class allows dealing with multiple types through a static import:
+ *
+ * <pre>
+ *   <code>
+ *     import static org.opendaylight.yangtools.yang.common.UintConversions.fromJava;
+ *
+ *     Uint16 two = fromJava(32);
+ *     Uint32 one = fromJava(32L);
+ *   </code>
+ * </pre>
+ *
+ * @author Robert Varga
+ */
+@Beta
+@NonNullByDefault
+public final class UintConversions {
+    private UintConversions() {
+        // Hidden on purpose
+    }
+
+    /**
+     * Convert a {@code short} in range 0-255 to an Uint8.
+     *
+     * @param value value
+     * @return Uint8 object
+     * @throws IllegalArgumentException if value is less than zero or greater than 255
+     */
+    public static Uint8 fromJava(final short value) {
+        return Uint8.valueOf(value);
+    }
+
+    /**
+     * Convert an {@code int} in range 0-65535 to a Uint16.
+     *
+     * @param value value
+     * @return Uint16 object
+     * @throws IllegalArgumentException if value is less than zero or greater than 65535.
+     */
+    public static Uint16 fromJava(final int value) {
+        return Uint16.valueOf(value);
+    }
+
+    /**
+     * Convert a {@code long} in range 0-4294967295 to a Uint32.
+     *
+     * @param value value
+     * @return Uint32 object
+     * @throws IllegalArgumentException if value is less than zero or greater than 4294967295
+     */
+    public static Uint32 fromJava(final long value) {
+        return Uint32.valueOf(value);
+    }
+
+    /**
+     * Convert a {@link BigInteger} in range 0-18446744073709551615 to an Uint64.
+     *
+     * @param value value
+     * @return Uint64 object
+     * @throws NullPointerException if value is null
+     * @throws IllegalArgumentException if value is less than zero or greater than 18446744073709551615
+     */
+    public static Uint64 fromJava(final BigInteger value) {
+        return Uint64.valueOf(value);
+    }
+
+    /**
+     * Convert an {@link UnsignedInteger} to a Uint32.
+     *
+     * @param value value
+     * @return Uint32 object
+     * @throws NullPointerException if value is null
+     */
+    public static Uint32 fromGuava(final UnsignedInteger value) {
+        return Uint32.valueOf(value);
+    }
+
+    /**
+     * Convert an {@link UnsignedLong} to a Uint64.
+     *
+     * @param value value
+     * @return Uint64 object
+     * @throws NullPointerException if value is null
+     */
+    public static Uint64 fromGuava(final UnsignedLong value) {
+        return Uint64.valueOf(value);
+    }
+}
index eafa32f2f771acf4fdca3f4854ada60ae6475b95..11d77463af9aae3d0ae7cf8346b4b5ccd3469427 100644 (file)
@@ -94,7 +94,7 @@ public class Uint32Test {
         assertSame(Uint32.valueOf(20), Uint32.valueOf(Uint64.valueOf(20)));
 
         assertSame(Uint32.valueOf(5), Uint32.valueOf(UnsignedInteger.fromIntBits(5)));
-        assertEquals(UnsignedInteger.fromIntBits(5), Uint32.valueOf(5).toUnsignedInteger());
+        assertEquals(UnsignedInteger.fromIntBits(5), Uint32.valueOf(5).toGuava());
     }
 
     @Test
index 130c8b1751134e15b9db9029e5adb7c8cb06c5d6..64bdf9fcf7fc792866ce9403e12ee3e0e11f4fde 100644 (file)
@@ -96,7 +96,7 @@ public class Uint64Test {
         assertEquals(Uint64.valueOf(30), Uint64.valueOf(new BigInteger("30")));
 
         assertSame(Uint64.valueOf(5), Uint64.valueOf(UnsignedLong.fromLongBits(5)));
-        assertEquals(UnsignedLong.fromLongBits(5), Uint64.valueOf(5).toUnsignedLong());
+        assertEquals(UnsignedLong.fromLongBits(5), Uint64.valueOf(5).toGuava());
     }
 
     @Test