Fix CanonicalValueViolation.getMessage()
[yangtools.git] / yang / yang-common / src / main / java / org / opendaylight / yangtools / yang / common / Uint32.java
index 21ddac294a334e8bd866ae17c98765ae0acc824e..5b3e6c2b6a739b6e0193b689848ba0ce87bf0797 100644 (file)
@@ -16,7 +16,6 @@ import com.google.common.primitives.UnsignedInteger;
 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;
 
 /**
@@ -27,7 +26,6 @@ import org.opendaylight.yangtools.concepts.Variant;
 @Beta
 @NonNullByDefault
 public class Uint32 extends Number implements CanonicalValue<Uint32> {
-    @MetaInfServices(value = CanonicalValueSupport.class)
     public static final class Support extends AbstractCanonicalValueSupport<Uint32> {
         public Support() {
             super(Uint32.class);
@@ -74,8 +72,25 @@ public class Uint32 extends Number implements CanonicalValue<Uint32> {
 
     private static final Interner<Uint32> INTERNER = Interners.newWeakInterner();
 
+    /**
+     * Value of {@code 0}.
+     */
     public static final Uint32 ZERO = valueOf(0).intern();
+    /**
+     * Value of {@code 1}.
+     */
     public static final Uint32 ONE = valueOf(1).intern();
+    /**
+     * Value of {@code 2}.
+     */
+    public static final Uint32 TWO = valueOf(2).intern();
+    /**
+     * Value of {@code 10}.
+     */
+    public static final Uint32 TEN = valueOf(10).intern();
+    /**
+     * Value of {@code 4294967295}.
+     */
     public static final Uint32 MAX_VALUE = valueOf(MAX_VALUE_LONG).intern();
 
     private final int value;
@@ -151,7 +166,7 @@ public class Uint32 extends Number implements CanonicalValue<Uint32> {
      * @throws IllegalArgumentException if longVal is less than zero or greater than 4294967295
      */
     public static Uint32 valueOf(final long longVal) {
-        UintConversions.checkRange(longVal, MAX_VALUE_LONG, MAX_VALUE_STR);
+        UintConversions.checkRange(longVal, MAX_VALUE_LONG);
         return instanceFor((int)longVal);
     }
 
@@ -230,6 +245,57 @@ public class Uint32 extends Number implements CanonicalValue<Uint32> {
         return instanceFor(Integer.parseUnsignedInt(requireNonNull(string), radix));
     }
 
+    /**
+     * Returns an {@code Uint32} 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 Uint32 instance
+     */
+    public static Uint32 saturatedOf(final byte byteVal) {
+        return byteVal <= 0 ? Uint32.ZERO : instanceFor(byteVal);
+    }
+
+    /**
+     * Returns an {@code Uint32} corresponding to a given {@code shortVal} if it is representable. If the value is
+     * negative {@link #ZERO} will be returned.
+     *
+     * @param shortVal short value
+     * @return A Uint32 instance
+     */
+    public static Uint32 saturatedOf(final short shortVal) {
+        return shortVal <= 0 ? Uint32.ZERO : instanceFor(shortVal);
+    }
+
+    /**
+     * Returns an {@code Uint32} corresponding to a given {@code intVal} if it is representable. If the value is
+     * negative {@link #ZERO} will be returned.
+     *
+     * @param intVal int value
+     * @return A Uint32 instance
+     */
+    public static Uint32 saturatedOf(final int intVal) {
+        return intVal <= 0 ? Uint32.ZERO : instanceFor(intVal);
+    }
+
+    /**
+     * Returns an {@code Uint32} 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 4294967295, {@link #MAX_VALUE} will be
+     * returned.
+     *
+     * @param longVal long value
+     * @return A Uint32 instance
+     */
+    public static Uint32 saturatedOf(final long longVal) {
+        if (longVal <= 0) {
+            return Uint32.ZERO;
+        }
+        if (longVal >= MAX_VALUE_LONG) {
+            return Uint32.MAX_VALUE;
+        }
+        return instanceFor((int) longVal);
+    }
+
     /**
      * {@inheritDoc}
      *
@@ -300,6 +366,45 @@ 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());
+    }
+
+    public final Uint8 toSaturatedUint8() {
+        return Uint8.saturatedOf(toJava());
+    }
+
+    public final Uint16 toSaturatedUint16() {
+        return Uint16.saturatedOf(toJava());
+    }
+
+    // FIXME: more saturated conversions
+
     @Override
     public final int hashCode() {
         return Integer.hashCode(value);
@@ -310,6 +415,16 @@ public class Uint32 extends Number implements CanonicalValue<Uint32> {
         return this == obj || obj instanceof Uint32 && value == ((Uint32)obj).value;
     }
 
+    /**
+     * A slightly faster version of {@link #equals(Object)}.
+     *
+     * @param obj Uint32 object
+     * @return {@code true} if this object is the same as the obj argument; {@code false} otherwise.
+     */
+    public final boolean equals(final @Nullable Uint32 obj) {
+        return this == obj || obj != null && value == obj.value;
+    }
+
     @Override
     public final String toString() {
         return toCanonicalString();