Move Decimal64.toInt() method
[yangtools.git] / yang / yang-common / src / main / java / org / opendaylight / yangtools / yang / common / Uint32.java
index 5dfe09921204c81227709cdd5dd5d439aee40578..0818206172c6cdac457d19a2883d7666bae3eda5 100644 (file)
@@ -14,7 +14,10 @@ import com.google.common.cache.CacheBuilder;
 import com.google.common.cache.CacheLoader;
 import com.google.common.cache.LoadingCache;
 import com.google.common.primitives.UnsignedInteger;
-import org.opendaylight.yangtools.concepts.Immutable;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
+import org.kohsuke.MetaInfServices;
+import org.opendaylight.yangtools.concepts.Variant;
 
 /**
  * Dedicated type for YANG's 'type uint32' type.
@@ -22,7 +25,25 @@ import org.opendaylight.yangtools.concepts.Immutable;
  * @author Robert Varga
  */
 @Beta
-public class Uint32 extends Number implements Comparable<Uint32>, Immutable {
+@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);
+        }
+
+        @Override
+        public Variant<Uint32, CanonicalValueViolation> fromString(final String str) {
+            try {
+                return Variant.ofFirst(Uint32.valueOf(str));
+            } catch (IllegalArgumentException e) {
+                return CanonicalValueViolation.variantOf(e);
+            }
+        }
+    }
+
+    private static final CanonicalValueSupport<Uint32> SUPPORT = new Support();
     private static final long serialVersionUID = 1L;
     private static final long MIN_VALUE = 0;
     private static final long MAX_VALUE = 0xffffffffL;
@@ -76,17 +97,20 @@ public class Uint32 extends Number implements Comparable<Uint32>, Immutable {
 
     private static Uint32 instanceFor(final int value) {
         final long longSlot = Integer.toUnsignedLong(value);
-        if (longSlot >= CACHE.length) {
-            for (Uint32 c : COMMON) {
-                if (c.value == value) {
-                    return c;
-                }
-            }
+        return longSlot < CACHE.length ? fromCache((int)longSlot, value) : fromCommon(value);
+    }
 
-            return LRU.getUnchecked(value);
+    private static Uint32 fromCommon(final int value) {
+        for (Uint32 c : COMMON) {
+            if (c.value == value) {
+                return c;
+            }
         }
+        return LRU.getUnchecked(value);
+    }
 
-        final int slot = (int)longSlot;
+    private static Uint32 fromCache(final int slot, final int value) {
+        // FIXME: 4.0.0: use VarHandles here
         Uint32 ret = CACHE[slot];
         if (ret == null) {
             synchronized (CACHE) {
@@ -97,7 +121,6 @@ public class Uint32 extends Number implements Comparable<Uint32>, Immutable {
                 }
             }
         }
-
         return ret;
     }
 
@@ -179,19 +202,29 @@ public class Uint32 extends Number implements Comparable<Uint32>, Immutable {
         return Integer.compareUnsigned(value, o.value);
     }
 
+    @Override
+    public final String toCanonicalString() {
+        return Integer.toUnsignedString(value);
+    }
+
+    @Override
+    public final CanonicalValueSupport<Uint32> support() {
+        return SUPPORT;
+    }
+
     @Override
     public final int hashCode() {
         return Integer.hashCode(value);
     }
 
     @Override
-    public final boolean equals(final Object obj) {
+    public final boolean equals(final @Nullable Object obj) {
         return this == obj || obj instanceof Uint32 && value == ((Uint32)obj).value;
     }
 
     @Override
     public final String toString() {
-        return Integer.toUnsignedString(value);
+        return toCanonicalString();
     }
 
     private Object readResolve() {