Include Uint.ZERO in lower saturation bounds
[yangtools.git] / yang / yang-common / src / main / java / org / opendaylight / yangtools / yang / common / Uint8.java
index 27dc70c058e84d1c6ddef7ff90fd1a3ca75b814c..6664927357869de0b92880d056b85e33edcb3da5 100644 (file)
@@ -217,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}
      *
@@ -274,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);