Include Uint.ZERO in lower saturation bounds
[yangtools.git] / yang / yang-common / src / main / java / org / opendaylight / yangtools / yang / common / Uint8.java
index ecd1b11b08e4bcdb5cf7d67ef0187da68d131c80..6664927357869de0b92880d056b85e33edcb3da5 100644 (file)
@@ -7,14 +7,12 @@
  */
 package org.opendaylight.yangtools.yang.common;
 
-import static com.google.common.base.Preconditions.checkArgument;
 import static java.util.Objects.requireNonNull;
 
 import com.google.common.annotations.Beta;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
-import org.kohsuke.MetaInfServices;
 import org.opendaylight.yangtools.concepts.Variant;
 
 /**
@@ -25,7 +23,6 @@ import org.opendaylight.yangtools.concepts.Variant;
 @Beta
 @NonNullByDefault
 public class Uint8 extends Number implements CanonicalValue<Uint8> {
-    @MetaInfServices(value = CanonicalValueSupport.class)
     public static final class Support extends AbstractCanonicalValueSupport<Uint8> {
         public Support() {
             super(Uint8.class);
@@ -43,8 +40,8 @@ public class Uint8 extends Number implements CanonicalValue<Uint8> {
 
     private static final CanonicalValueSupport<Uint8> SUPPORT = new Support();
 
-    private static final short MIN_VALUE_SHORT = 0;
     private static final short MAX_VALUE_SHORT = 255;
+    private static final String MAX_VALUE_STR = "255";
 
     private static final long serialVersionUID = 1L;
 
@@ -52,14 +49,31 @@ public class Uint8 extends Number implements CanonicalValue<Uint8> {
 
     static {
         final Uint8[] c = new Uint8[MAX_VALUE_SHORT + 1];
-        for (int i = MIN_VALUE_SHORT; i <= MAX_VALUE_SHORT; ++i) {
+        for (int i = 0; i <= MAX_VALUE_SHORT; ++i) {
             c[i] = new Uint8((byte)i);
         }
         CACHE = c;
     }
 
-    public static final Uint8 ZERO = valueOf(MIN_VALUE_SHORT);
+    /**
+     * Value of {@code 0}.
+     */
+    public static final Uint8 ZERO = valueOf(0);
+    /**
+     * Value of {@code 1}.
+     */
     public static final Uint8 ONE = valueOf(1);
+    /**
+     * Value of {@code 2}.
+     */
+    public static final Uint8 TWO = valueOf(2);
+    /**
+     * Value of {@code 10}.
+     */
+    public static final Uint8 TEN = valueOf(10);
+    /**
+     * Value of {@code 255}.
+     */
     public static final Uint8 MAX_VALUE = valueOf(MAX_VALUE_SHORT);
 
     private final byte value;
@@ -95,7 +109,7 @@ public class Uint8 extends Number implements CanonicalValue<Uint8> {
      * @throws IllegalArgumentException if byteVal is less than zero
      */
     public static Uint8 valueOf(final byte byteVal) {
-        checkArgument(byteVal >= MIN_VALUE_SHORT, "Negative values are not allowed");
+        UintConversions.checkNonNegative(byteVal, MAX_VALUE_STR);
         return instanceFor(byteVal);
     }
 
@@ -108,9 +122,8 @@ public class Uint8 extends Number implements CanonicalValue<Uint8> {
      * @throws IllegalArgumentException if shortVal is less than zero or greater than 255.
      */
     public static Uint8 valueOf(final short shortVal) {
-        checkArgument(shortVal >= MIN_VALUE_SHORT && shortVal <= MAX_VALUE_SHORT,
-                "Value %s is outside of allowed range", shortVal);
-        return instanceFor((byte)(shortVal & 0xff));
+        UintConversions.checkRange(shortVal, MAX_VALUE_SHORT);
+        return instanceFor((byte)shortVal);
     }
 
     /**
@@ -121,9 +134,8 @@ public class Uint8 extends Number implements CanonicalValue<Uint8> {
      * @throws IllegalArgumentException if intVal is less than zero or greater than 255.
      */
     public static Uint8 valueOf(final int intVal) {
-        checkArgument(intVal >= MIN_VALUE_SHORT && intVal <= MAX_VALUE_SHORT,
-                "Value %s is outside of allowed range", intVal);
-        return instanceFor((byte)(intVal & 0xff));
+        UintConversions.checkRange(intVal, MAX_VALUE_SHORT);
+        return instanceFor((byte)intVal);
     }
 
     /**
@@ -135,9 +147,8 @@ public class Uint8 extends Number implements CanonicalValue<Uint8> {
      * @throws IllegalArgumentException if intVal is less than zero or greater than 255.
      */
     public static Uint8 valueOf(final long longVal) {
-        checkArgument(longVal >= MIN_VALUE_SHORT && longVal <= MAX_VALUE_SHORT,
-                "Value %s is outside of allowed range", longVal);
-        return instanceFor((byte)(longVal & 0xff));
+        UintConversions.checkRange(longVal, MAX_VALUE_SHORT);
+        return instanceFor((byte)longVal);
     }
 
     /**
@@ -206,6 +217,68 @@ public class Uint8 extends Number implements CanonicalValue<Uint8> {
         return valueOf(Short.parseShort(requireNonNull(string), radix));
     }
 
+    /**
+     * Returns an {@code Uint8} corresponding to a given {@code byteVal} if it is representable. If the value is
+     * negative {@link #ZERO} will be returned.
+     *
+     * @param byteVal byte value
+     * @return A Uint8 instance
+     */
+    public static Uint8 saturatedOf(final byte byteVal) {
+        return byteVal <= 0 ? Uint8.ZERO : instanceFor(byteVal);
+    }
+
+    /**
+     * Returns an {@code Uint8} corresponding to a given {@code shortVal} if it is representable. If the value is
+     * negative {@link #ZERO} will be returned. If the value is greater than 255, {@link #MAX_VALUE} will be returned.
+     *
+     * @param shortVal short value
+     * @return A Uint8 instance
+     */
+    public static Uint8 saturatedOf(final short shortVal) {
+        if (shortVal <= 0) {
+            return Uint8.ZERO;
+        }
+        if (shortVal >= MAX_VALUE_SHORT) {
+            return Uint8.MAX_VALUE;
+        }
+        return instanceFor((byte) shortVal);
+    }
+
+    /**
+     * Returns an {@code Uint8} corresponding to a given {@code intVal} if it is representable. If the value is
+     * negative {@link #ZERO} will be returned. If the value is greater than 255, {@link #MAX_VALUE} will be returned.
+     *
+     * @param intVal int value
+     * @return A Uint8 instance
+     */
+    public static Uint8 saturatedOf(final int intVal) {
+        if (intVal <= 0) {
+            return Uint8.ZERO;
+        }
+        if (intVal >= MAX_VALUE_SHORT) {
+            return Uint8.MAX_VALUE;
+        }
+        return instanceFor((byte) intVal);
+    }
+
+    /**
+     * Returns an {@code Uint8} corresponding to a given {@code longVal} if it is representable. If the value is
+     * negative {@link #ZERO} will be returned. If the value is greater than 255, {@link #MAX_VALUE} will be returned.
+     *
+     * @param longVal long value
+     * @return A Uint8 instance
+     */
+    public static Uint8 saturatedOf(final long longVal) {
+        if (longVal <= 0) {
+            return Uint8.ZERO;
+        }
+        if (longVal >= MAX_VALUE_SHORT) {
+            return Uint8.MAX_VALUE;
+        }
+        return instanceFor((byte) longVal);
+    }
+
     /**
      * {@inheritDoc}
      *
@@ -263,6 +336,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);
@@ -273,6 +373,16 @@ public class Uint8 extends Number implements CanonicalValue<Uint8> {
         return this == obj || obj instanceof Uint8 && value == ((Uint8)obj).value;
     }
 
+    /**
+     * A slightly faster version of {@link #equals(Object)}.
+     *
+     * @param obj Uint8 object
+     * @return {@code true} if this object is the same as the obj argument; {@code false} otherwise.
+     */
+    public final boolean equals(final @Nullable Uint8 obj) {
+        return this == obj || obj != null && value == obj.value;
+    }
+
     @Override
     public final String toString() {
         return toCanonicalString();