Add utility converters 97/85097/5
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 13 Oct 2019 08:40:36 +0000 (10:40 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 13 Oct 2019 11:16:26 +0000 (13:16 +0200)
This adds toUint{8,16,32,64} methods to ease conversion between
Uint types.

JIRA: YANGTOOLS-1033
Change-Id: I010a9103e2115e7808aa7d04ea9a5b41ab12ddd6
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
yang/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/Uint16Test.java
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
yang/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/Uint8Test.java

index 91ae038dae4bbda18dd96ff90716d7dcfd48b28e..491559859ded67b3e474b3d455eac3bbaf6a6c7c 100644 (file)
@@ -300,6 +300,34 @@ public class Uint16 extends Number implements CanonicalValue<Uint16> {
         return intValue();
     }
 
+    /**
+     * Convert this value to a {@code Uint8}.
+     *
+     * @return A Uint8
+     * @throws IllegalArgumentException if this value is greater than 255.
+     */
+    public final Uint8 toUint8() {
+        return Uint8.valueOf(toJava());
+    }
+
+    /**
+     * Convert this value to a {@code Uint32}.
+     *
+     * @return A Uint32
+     */
+    public final Uint32 toUint32() {
+        return Uint32.fromIntBits(intValue());
+    }
+
+    /**
+     * Convert this value to a {@code Uint64}.
+     *
+     * @return A Uint64
+     */
+    public final Uint64 toUint64() {
+        return Uint64.fromLongBits(longValue());
+    }
+
     @Override
     public final int hashCode() {
         return Short.hashCode(value);
index 0fe1a913f48bd85608f00489fe2006f642b5ccc1..6b3b39291483b9ce191b953a49d74709480a3bd4 100644 (file)
@@ -315,6 +315,35 @@ public class Uint32 extends Number implements CanonicalValue<Uint32> {
         return UnsignedInteger.fromIntBits(value);
     }
 
+    /**
+     * Convert this value to a {@code Uint8}.
+     *
+     * @return A Uint8
+     * @throws IllegalArgumentException if this value is greater than 255.
+     */
+    public final Uint8 toUint8() {
+        return Uint8.valueOf(toJava());
+    }
+
+    /**
+     * Convert this value to a {@code Uint16}.
+     *
+     * @return A Uint16
+     * @throws IllegalArgumentException if this value is greater than 65535.
+     */
+    public final Uint16 toUint16() {
+        return Uint16.valueOf(toJava());
+    }
+
+    /**
+     * Convert this value to a {@code Uint64}.
+     *
+     * @return A Uint64
+     */
+    public final Uint64 toUint64() {
+        return Uint64.fromLongBits(longValue());
+    }
+
     @Override
     public final int hashCode() {
         return Integer.hashCode(value);
index 2be5479397820627d8cf089a09860ab9c6bb0fbf..6d537fd009ac49f27c1ae05d35b4a7c4afd29ea0 100644 (file)
@@ -333,6 +333,45 @@ public class Uint64 extends Number implements CanonicalValue<Uint64> {
         return UnsignedLong.fromLongBits(value);
     }
 
+    /**
+     * Convert this value to a {@code Uint8}.
+     *
+     * @return A Uint8
+     * @throws IllegalArgumentException if this value is greater than 255.
+     */
+    public final Uint8 toUint8() {
+        if ((value & 0xFFFFFFFFFFFFFF00L) != 0) {
+            UintConversions.throwIAE(toString(), 255);
+        }
+        return Uint8.fromByteBits((byte) value);
+    }
+
+    /**
+     * Convert this value to a {@code Uint16}.
+     *
+     * @return A Uint16
+     * @throws IllegalArgumentException if this value is greater than 65535.
+     */
+    public final Uint16 toUint16() {
+        if ((value & 0xFFFFFFFFFFFF0000L) != 0) {
+            UintConversions.throwIAE(toString(), 65535);
+        }
+        return Uint16.fromShortBits((short) value);
+    }
+
+    /**
+     * Convert this value to a {@code Uint64}.
+     *
+     * @return A Uint32
+     * @throws IllegalArgumentException if this value is greater than 4294967295.
+     */
+    public final Uint32 toUint32() {
+        if ((value & 0xFFFFFFFF00000000L) != 0) {
+            UintConversions.throwIAE(toString(), 4294967295L);
+        }
+        return Uint32.fromIntBits((int) value);
+    }
+
     @Override
     public final int hashCode() {
         return Long.hashCode(value);
index 27dc70c058e84d1c6ddef7ff90fd1a3ca75b814c..99548c3327b6c7ee01b2893eb2731ac28446cf69 100644 (file)
@@ -274,6 +274,33 @@ public class Uint8 extends Number implements CanonicalValue<Uint8> {
         return shortValue();
     }
 
+    /**
+     * Convert this value to a {@code Uint16}.
+     *
+     * @return A Uint16
+     */
+    public final Uint16 toUint16() {
+        return Uint16.fromShortBits(shortValue());
+    }
+
+    /**
+     * Convert this value to a {@code Uint32}.
+     *
+     * @return A Uint32
+     */
+    public final Uint32 toUint32() {
+        return Uint32.fromIntBits(intValue());
+    }
+
+    /**
+     * Convert this value to a {@code Uint64}.
+     *
+     * @return A Uint64
+     */
+    public final Uint64 toUint64() {
+        return Uint64.fromLongBits(longValue());
+    }
+
     @Override
     public final int hashCode() {
         return Byte.hashCode(value);
index 34f1ce32008f3573658dee3a768485c30e464ab9..7b8c22303077091ac2287205314257e7c313f8e3 100644 (file)
@@ -139,6 +139,11 @@ public final class UintConversions {
         }
     }
 
+    static void throwIAE(final String value, final long max) {
+        // "Invalid range: 65536, expected: [[0..65535]]."
+        throw new IllegalArgumentException("Invalid range: " + value + ", expected: [[0.." + max + "]].");
+    }
+
     private static void throwIAE(final int value, final int max) {
         // "Invalid range: 65536, expected: [[0..65535]]."
         throw new IllegalArgumentException("Invalid range: " + value + ", expected: [[0.." + max + "]].");
index 57625ecc50793a2d6461aa14c7bbb64d77ee8be4..767780bc932d8f84b1ba240d16d1b6e1153dbb4e 100644 (file)
@@ -94,6 +94,15 @@ public class Uint16Test {
         assertSame(Uint16.valueOf(5), Uint16.valueOf(Uint8.valueOf(5)));
         assertSame(Uint16.valueOf(10), Uint16.valueOf(Uint32.valueOf(10)));
         assertSame(Uint16.valueOf(20), Uint16.valueOf(Uint64.valueOf(20)));
+
+        assertEquals(Uint8.TEN, Uint16.TEN.toUint8());
+        assertEquals(Uint32.valueOf(65535), Uint16.MAX_VALUE.toUint32());
+        assertEquals(Uint64.valueOf(65535), Uint16.MAX_VALUE.toUint64());
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testToUint8() {
+        Uint16.MAX_VALUE.toUint8();
     }
 
     @Test
index 81716a14e28f5608923cd4c5f2808cf0369b2736..2706dc0e842c09d09e017b8e1c7ec2fd03a32688 100644 (file)
@@ -95,6 +95,20 @@ public class Uint32Test {
 
         assertSame(Uint32.valueOf(5), Uint32.valueOf(UnsignedInteger.fromIntBits(5)));
         assertEquals(UnsignedInteger.fromIntBits(5), Uint32.valueOf(5).toGuava());
+
+        assertEquals(Uint8.TEN, Uint32.TEN.toUint8());
+        assertEquals(Uint16.TEN, Uint32.TEN.toUint16());
+        assertEquals(Uint64.valueOf(4294967295L), Uint32.MAX_VALUE.toUint64());
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testToUint8() {
+        Uint32.MAX_VALUE.toUint8();
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testToUint16() {
+        Uint32.MAX_VALUE.toUint16();
     }
 
     @Test
index c4df482be1352b0bd4a3215a6955ca9f269363cc..dfc5e84f33bd167c029dc62f5a807fc3a7f50617 100644 (file)
@@ -99,6 +99,25 @@ public class Uint64Test {
 
         assertSame(Uint64.valueOf(5), Uint64.valueOf(UnsignedLong.fromLongBits(5)));
         assertEquals(UnsignedLong.fromLongBits(5), Uint64.valueOf(5).toGuava());
+
+        assertEquals(Uint8.TEN, Uint64.TEN.toUint8());
+        assertEquals(Uint16.TEN, Uint64.TEN.toUint16());
+        assertEquals(Uint32.TEN, Uint64.TEN.toUint32());
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testToUint8() {
+        Uint64.MAX_VALUE.toUint8();
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testToUint16() {
+        Uint64.MAX_VALUE.toUint16();
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testToUint32() {
+        Uint64.MAX_VALUE.toUint32();
     }
 
     @Test
index 93a31556340294804ccb27c4f9c2335025c80553..a231e96a219e45046a803774bfb00badfffc4447 100644 (file)
@@ -90,6 +90,10 @@ public class Uint8Test {
         assertSame(Uint8.valueOf(5), Uint8.valueOf(Uint16.valueOf(5)));
         assertSame(Uint8.valueOf(10), Uint8.valueOf(Uint32.valueOf(10)));
         assertSame(Uint8.valueOf(20), Uint8.valueOf(Uint64.valueOf(20)));
+
+        assertEquals(Uint16.valueOf(255), Uint8.MAX_VALUE.toUint16());
+        assertEquals(Uint32.valueOf(255), Uint8.MAX_VALUE.toUint32());
+        assertEquals(Uint64.valueOf(255), Uint8.MAX_VALUE.toUint64());
     }
 
     @Test