Make yang.common base types non-final 75/70875/1
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 12 Apr 2018 19:34:42 +0000 (21:34 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 12 Apr 2018 19:39:55 +0000 (21:39 +0200)
While final classes are useful, we will require the ability to mark
validation level of a particular piece of data in a vein similar to
DerivedString -- which requires subclassing to work memory-efficiently.

This patch prepares for that work by making the types non-final,
but does not allow their particulars to be overridden, so they still
form an effectively-final contract just like DerivedString
representations do.

Change-Id: Id7bde4b49d8b8aeb87c9b6e7317e5aa8f55e98eb
JIRA: YANGTOOLS-418
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/Decimal64.java
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/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 aa7829d1ddf8ea46dc97aa9963e00cad89c04f78..dc30514253f4a5a0060b17474cb6addf8162e6f9 100644 (file)
@@ -23,7 +23,7 @@ import org.opendaylight.yangtools.concepts.Immutable;
  * @author Robert Varga
  */
 @Beta
-public final class Decimal64 extends Number implements Comparable<Decimal64>, Immutable {
+public class Decimal64 extends Number implements Comparable<Decimal64>, Immutable {
     private static final long serialVersionUID = 1L;
 
     private static final int MAX_FRACTION_DIGITS = 18;
@@ -65,6 +65,11 @@ public final class Decimal64 extends Number implements Comparable<Decimal64>, Im
         this.value = negative ? -bits : bits;
     }
 
+    protected Decimal64(final Decimal64 other) {
+        this.scaleOffset = other.scaleOffset;
+        this.value = other.value;
+    }
+
     public static Decimal64 valueOf(final byte byteVal) {
         return byteVal < 0 ? new Decimal64(1, -byteVal, 0, true) : new Decimal64(1, byteVal, 0, false);
     }
@@ -193,27 +198,27 @@ public final class Decimal64 extends Number implements Comparable<Decimal64>, Im
         return new Decimal64(fracLen, intPart, fracPart, negative);
     }
 
-    public BigDecimal decimalValue() {
+    public final BigDecimal decimalValue() {
         return BigDecimal.valueOf(value, scaleOffset + 1);
     }
 
     @Override
-    public int intValue() {
+    public final int intValue() {
         return (int) intPart();
     }
 
     @Override
-    public long longValue() {
+    public final long longValue() {
         return intPart();
     }
 
     @Override
-    public float floatValue() {
+    public final float floatValue() {
         return (float) doubleValue();
     }
 
     @Override
-    public double doubleValue() {
+    public final double doubleValue() {
         return 1.0 * value / SCALE[scaleOffset];
     }
 
@@ -225,7 +230,7 @@ public final class Decimal64 extends Number implements Comparable<Decimal64>, Im
      * @return this {@code Decimal64} converted to a {@code byte}.
      * @throws ArithmeticException if {@code this} has a nonzero fractional part, or will not fit in a {@code byte}.
      */
-    public byte byteValueExact() {
+    public final byte byteValueExact() {
         final long val = longValueExact();
         final byte ret = (byte) val;
         if (val != ret) {
@@ -242,7 +247,7 @@ public final class Decimal64 extends Number implements Comparable<Decimal64>, Im
      * @return this {@code Decimal64} converted to a {@code short}.
      * @throws ArithmeticException if {@code this} has a nonzero fractional part, or will not fit in a {@code short}.
      */
-    public short shortValueExact() {
+    public final short shortValueExact() {
         final long val = longValueExact();
         final short ret = (short) val;
         if (val != ret) {
@@ -259,7 +264,7 @@ public final class Decimal64 extends Number implements Comparable<Decimal64>, Im
      * @return this {@code Decimal64} converted to an {@code int}.
      * @throws ArithmeticException if {@code this} has a nonzero fractional part, or will not fit in an {@code int}.
      */
-    public int intValueExact() {
+    public final int intValueExact() {
         final long val = longValueExact();
         final int ret = (int) val;
         if (val != ret) {
@@ -275,7 +280,7 @@ public final class Decimal64 extends Number implements Comparable<Decimal64>, Im
      * @return this {@code Decimal64} converted to a {@code long}.
      * @throws ArithmeticException if {@code this} has a nonzero fractional part.
      */
-    public long longValueExact() {
+    public final long longValueExact() {
         if (fracPart() != 0) {
             throw new ArithmeticException("Conversion of " + this + " would lose fraction");
         }
@@ -284,7 +289,7 @@ public final class Decimal64 extends Number implements Comparable<Decimal64>, Im
 
     @Override
     @SuppressWarnings("checkstyle:parameterName")
-    public int compareTo(final Decimal64 o) {
+    public final int compareTo(final Decimal64 o) {
         if (this == o) {
             return 0;
         }
@@ -297,13 +302,13 @@ public final class Decimal64 extends Number implements Comparable<Decimal64>, Im
     }
 
     @Override
-    public int hashCode() {
+    public final int hashCode() {
         // We need to normalize the results in order to be consistent with equals()
         return Long.hashCode(intPart()) * 31 + Long.hashCode(fracPart());
     }
 
     @Override
-    public boolean equals(final Object obj) {
+    public final boolean equals(final Object obj) {
         if (this == obj) {
             return true;
         }
@@ -320,7 +325,7 @@ public final class Decimal64 extends Number implements Comparable<Decimal64>, Im
     }
 
     @Override
-    public String toString() {
+    public final String toString() {
         // https://tools.ietf.org/html/rfc6020#section-9.3.2
         //
         // The canonical form of a positive decimal64 does not include the sign
index 0bf19dbef339527e22b3f09a47ae2d3ccf9efc63..7b9e50f0cb6aa97ebce356ba7f0eaaa18db1cdd3 100644 (file)
@@ -10,7 +10,6 @@ package org.opendaylight.yangtools.yang.common;
 import static com.google.common.base.Preconditions.checkArgument;
 
 import com.google.common.annotations.Beta;
-import com.google.common.annotations.VisibleForTesting;
 import com.google.common.cache.CacheBuilder;
 import com.google.common.cache.CacheLoader;
 import com.google.common.cache.LoadingCache;
@@ -22,7 +21,7 @@ import org.opendaylight.yangtools.concepts.Immutable;
  * @author Robert Varga
  */
 @Beta
-public final class Uint16 extends Number implements Comparable<Uint16>, Immutable {
+public class Uint16 extends Number implements Comparable<Uint16>, Immutable {
     private static final long serialVersionUID = 1L;
     private static final int MIN_VALUE = 0;
     private static final int MAX_VALUE = 65535;
@@ -65,11 +64,14 @@ public final class Uint16 extends Number implements Comparable<Uint16>, Immutabl
 
     private final short value;
 
-    @VisibleForTesting
     Uint16(final short value) {
         this.value = value;
     }
 
+    protected Uint16(final Uint16 other) {
+        this.value = other.value;
+    }
+
     private static Uint16 instanceFor(final short value) {
         final int slot = Short.toUnsignedInt(value);
         if (slot >= CACHE.length) {
@@ -141,52 +143,48 @@ public final class Uint16 extends Number implements Comparable<Uint16>, Immutabl
     }
 
     @Override
-    public short shortValue() {
+    public final short shortValue() {
         return value;
     }
 
     @Override
-    public int intValue() {
+    public final int intValue() {
         return Short.toUnsignedInt(value);
     }
 
     @Override
-    public long longValue() {
+    public final long longValue() {
         return Short.toUnsignedLong(value);
     }
 
     @Override
-    public float floatValue() {
+    public final float floatValue() {
         return intValue();
     }
 
     @Override
-    public double doubleValue() {
+    public final double doubleValue() {
         return intValue();
     }
 
     @Override
     @SuppressWarnings("checkstyle:parameterName")
-    public int compareTo(final Uint16 o) {
+    public final int compareTo(final Uint16 o) {
         return Integer.compare(intValue(), o.intValue());
     }
 
     @Override
-    public int hashCode() {
+    public final int hashCode() {
         return Short.hashCode(value);
     }
 
     @Override
-    public boolean equals(final Object obj) {
-        if (this == obj) {
-            return true;
-        }
-
-        return obj instanceof Uint16 && value == ((Uint16)obj).value;
+    public final boolean equals(final Object obj) {
+        return this == obj || obj instanceof Uint16 && value == ((Uint16)obj).value;
     }
 
     @Override
-    public String toString() {
+    public final String toString() {
         return String.valueOf(intValue());
     }
 
index b9a03a5ce96f192fdc96cec9cbd72f5b9c7ee482..5dfe09921204c81227709cdd5dd5d439aee40578 100644 (file)
@@ -10,7 +10,6 @@ package org.opendaylight.yangtools.yang.common;
 import static com.google.common.base.Preconditions.checkArgument;
 
 import com.google.common.annotations.Beta;
-import com.google.common.annotations.VisibleForTesting;
 import com.google.common.cache.CacheBuilder;
 import com.google.common.cache.CacheLoader;
 import com.google.common.cache.LoadingCache;
@@ -23,7 +22,7 @@ import org.opendaylight.yangtools.concepts.Immutable;
  * @author Robert Varga
  */
 @Beta
-public final class Uint32 extends Number implements Comparable<Uint32>, Immutable {
+public class Uint32 extends Number implements Comparable<Uint32>, Immutable {
     private static final long serialVersionUID = 1L;
     private static final long MIN_VALUE = 0;
     private static final long MAX_VALUE = 0xffffffffL;
@@ -67,11 +66,14 @@ public final class Uint32 extends Number implements Comparable<Uint32>, Immutabl
 
     private final int value;
 
-    @VisibleForTesting
     Uint32(final int value) {
         this.value = value;
     }
 
+    protected Uint32(final Uint32 other) {
+        this.value = other.value;
+    }
+
     private static Uint32 instanceFor(final int value) {
         final long longSlot = Integer.toUnsignedLong(value);
         if (longSlot >= CACHE.length) {
@@ -148,51 +150,47 @@ public final class Uint32 extends Number implements Comparable<Uint32>, Immutabl
     }
 
     @Override
-    public int intValue() {
+    public final int intValue() {
         return value;
     }
 
     @Override
-    public long longValue() {
+    public final long longValue() {
         return Integer.toUnsignedLong(value);
     }
 
     @Override
-    public float floatValue() {
+    public final float floatValue() {
         return longValue();
     }
 
     @Override
-    public double doubleValue() {
+    public final double doubleValue() {
         return longValue();
     }
 
-    public UnsignedInteger toUnsignedInteger() {
+    public final UnsignedInteger toUnsignedInteger() {
         return UnsignedInteger.fromIntBits(value);
     }
 
     @Override
     @SuppressWarnings("checkstyle:parameterName")
-    public int compareTo(final Uint32 o) {
+    public final int compareTo(final Uint32 o) {
         return Integer.compareUnsigned(value, o.value);
     }
 
     @Override
-    public int hashCode() {
+    public final int hashCode() {
         return Integer.hashCode(value);
     }
 
     @Override
-    public boolean equals(final Object obj) {
-        if (this == obj) {
-            return true;
-        }
-
-        return obj instanceof Uint32 && value == ((Uint32)obj).value;
+    public final boolean equals(final Object obj) {
+        return this == obj || obj instanceof Uint32 && value == ((Uint32)obj).value;
     }
 
     @Override
-    public String toString() {
+    public final String toString() {
         return Integer.toUnsignedString(value);
     }
 
index 86d439328ebee2d846bff6b3d2b05aad628a53e6..3480b938bc295186ee1f2235c363e962b68cf314 100644 (file)
@@ -10,7 +10,6 @@ package org.opendaylight.yangtools.yang.common;
 import static com.google.common.base.Preconditions.checkArgument;
 
 import com.google.common.annotations.Beta;
-import com.google.common.annotations.VisibleForTesting;
 import com.google.common.cache.CacheBuilder;
 import com.google.common.cache.CacheLoader;
 import com.google.common.cache.LoadingCache;
@@ -24,7 +23,7 @@ import org.opendaylight.yangtools.concepts.Immutable;
  * @author Robert Varga
  */
 @Beta
-public final class Uint64 extends Number implements Comparable<Uint64>, Immutable {
+public class Uint64 extends Number implements Comparable<Uint64>, Immutable {
     private static final long serialVersionUID = 1L;
     private static final long MIN_VALUE = 0;
 
@@ -69,11 +68,14 @@ public final class Uint64 extends Number implements Comparable<Uint64>, Immutabl
 
     private final long value;
 
-    @VisibleForTesting
     Uint64(final long value) {
         this.value = value;
     }
 
+    protected Uint64(final Uint64 other) {
+        this.value = other.value;
+    }
+
     private static Uint64 instanceFor(final long value) {
         final int slot = (int)value;
         if (slot < 0 || slot >= CACHE.length) {
@@ -156,53 +158,49 @@ public final class Uint64 extends Number implements Comparable<Uint64>, Immutabl
     }
 
     @Override
-    public int intValue() {
+    public final int intValue() {
         return (int)value;
     }
 
     @Override
-    public long longValue() {
+    public final long longValue() {
         return value;
     }
 
     @Override
-    public float floatValue() {
+    public final float floatValue() {
         // TODO: ditch Guava
         return UnsignedLong.fromLongBits(value).floatValue();
     }
 
     @Override
-    public double doubleValue() {
+    public final double doubleValue() {
         // TODO: ditch Guava
         return UnsignedLong.fromLongBits(value).doubleValue();
     }
 
-    public UnsignedLong toUnsignedLong() {
+    public final UnsignedLong toUnsignedLong() {
         return UnsignedLong.fromLongBits(value);
     }
 
     @Override
     @SuppressWarnings("checkstyle:parameterName")
-    public int compareTo(final Uint64 o) {
+    public final int compareTo(final Uint64 o) {
         return Long.compareUnsigned(value, o.value);
     }
 
     @Override
-    public int hashCode() {
+    public final int hashCode() {
         return Long.hashCode(value);
     }
 
     @Override
-    public boolean equals(final Object obj) {
-        if (this == obj) {
-            return true;
-        }
-
-        return obj instanceof Uint64 && value == ((Uint64)obj).value;
+    public final boolean equals(final Object obj) {
+        return this == obj || obj instanceof Uint64 && value == ((Uint64)obj).value;
     }
 
     @Override
-    public String toString() {
+    public final String toString() {
         return Long.toUnsignedString(value);
     }
 
index 90ed5fac9006c1d1abf72ac8582b765e1e926c7f..1def9c4759606965228a99d33758d2e2afb3f9ab 100644 (file)
@@ -10,7 +10,6 @@ package org.opendaylight.yangtools.yang.common;
 import static com.google.common.base.Preconditions.checkArgument;
 
 import com.google.common.annotations.Beta;
-import com.google.common.annotations.VisibleForTesting;
 import org.opendaylight.yangtools.concepts.Immutable;
 
 /**
@@ -19,7 +18,7 @@ import org.opendaylight.yangtools.concepts.Immutable;
  * @author Robert Varga
  */
 @Beta
-public final class Uint8 extends Number implements Comparable<Uint8>, Immutable {
+public class Uint8 extends Number implements Comparable<Uint8>, Immutable {
     static final short MIN_VALUE = 0;
     static final short MAX_VALUE = 255;
 
@@ -28,11 +27,14 @@ public final class Uint8 extends Number implements Comparable<Uint8>, Immutable
 
     private final byte value;
 
-    @VisibleForTesting
-    Uint8(final byte value) {
+    private Uint8(final byte value) {
         this.value = value;
     }
 
+    protected Uint8(final Uint8 other) {
+        this.value = other.value;
+    }
+
     private static Uint8 instanceFor(final byte value) {
         final int slot = Byte.toUnsignedInt(value);
 
@@ -95,52 +97,48 @@ public final class Uint8 extends Number implements Comparable<Uint8>, Immutable
     }
 
     @Override
-    public byte byteValue() {
+    public final byte byteValue() {
         return value;
     }
 
     @Override
-    public int intValue() {
+    public final int intValue() {
         return Byte.toUnsignedInt(value);
     }
 
     @Override
-    public long longValue() {
+    public final long longValue() {
         return Byte.toUnsignedLong(value);
     }
 
     @Override
-    public float floatValue() {
+    public final float floatValue() {
         return intValue();
     }
 
     @Override
-    public double doubleValue() {
+    public final double doubleValue() {
         return intValue();
     }
 
     @Override
     @SuppressWarnings("checkstyle:parameterName")
-    public int compareTo(final Uint8 o) {
+    public final int compareTo(final Uint8 o) {
         return intValue() - o.intValue();
     }
 
     @Override
-    public int hashCode() {
+    public final int hashCode() {
         return Byte.hashCode(value);
     }
 
     @Override
-    public boolean equals(final Object obj) {
-        if (this == obj) {
-            return true;
-        }
-
-        return obj instanceof Uint8 && value == ((Uint8)obj).value;
+    public final boolean equals(final Object obj) {
+        return this == obj || obj instanceof Uint8 && value == ((Uint8)obj).value;
     }
 
     @Override
-    public String toString() {
+    public final String toString() {
         return String.valueOf(intValue());
     }
 
index 2cbfc86db2b8b5f8949e2e49e2521cee85cd3d16..5311162d941c7d0637859c486502643ca157199d 100644 (file)
@@ -54,7 +54,7 @@ public class Uint16Test {
         final Uint16 zero = Uint16.valueOf(0);
         final Uint16 max = Uint16.valueOf(65535);
 
-        final Uint16 test = new Uint16((short) 5);
+        final Uint16 test = new Uint16(five);
         assertFalse(test.equals(zero));
         assertFalse(test.equals(new Object()));
         assertFalse(test.equals(max));
index 72a4c232519b6cf36ddfe0bce997519db70d1fa5..0a301c9e20e17aa49b95cdfd4a7976f49338d03f 100644 (file)
@@ -55,7 +55,7 @@ public class Uint32Test {
         final Uint32 zero = Uint32.valueOf(0);
         final Uint32 max = Uint32.valueOf(4294967295L);
 
-        final Uint32 test = new Uint32(5);
+        final Uint32 test = new Uint32(five);
         assertFalse(test.equals(zero));
         assertFalse(test.equals(new Object()));
         assertFalse(test.equals(max));
index 33025d9d5c425162422db80439204dd12611bb06..39e2014ddd651a8de88f819d89ea13ff1d3e6131 100644 (file)
@@ -56,7 +56,7 @@ public class Uint64Test {
         final Uint64 zero = Uint64.valueOf(0);
         final Uint64 max = Uint64.valueOf(4294967295L);
 
-        final Uint64 test = new Uint64(5);
+        final Uint64 test = new Uint64(five);
         assertFalse(test.equals(zero));
         assertFalse(test.equals(new Object()));
         assertFalse(test.equals(max));
index 137af53c70e748a2fc03ba31a5f0b98054f4c5cd..d9a18276fdf073f7db1623614e913668a8d4118b 100644 (file)
@@ -53,7 +53,7 @@ public class Uint8Test {
         final Uint8 zero = Uint8.valueOf(0);
         final Uint8 max = Uint8.valueOf(255);
 
-        final Uint8 test = new Uint8((byte) 5);
+        final Uint8 test = new Uint8(five);
         assertFalse(test.equals(zero));
         assertFalse(test.equals(new Object()));
         assertFalse(test.equals(max));