Update and document Uint 74/84174/5
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 3 Sep 2019 11:35:38 +0000 (13:35 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 3 Sep 2019 17:49:11 +0000 (19:49 +0200)
This updates Uint classes to expose ZERO/ONE/MAX_VALUE and documents
their methods.

Caching is reworked to work on tunable statically-populated caches,
where Uint8 is always fully cached. This mirrors what is being done
for boxing purposes on java.lang.Byte, etc.

On top of the statically-populated caches, each type has a dedicated
Interner instance, which can be efficiently consulted using intern().

JIRA: YANGTOOLS-1018
Change-Id: Ic25b5ac685c920f7d82d588309a543f9a8eb43b7
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
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/Uint32Test.java
yang/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/Uint64Test.java

index 1ee9c28f93cd1cf0a4b0949606c9f5453e2c3d0e..8a2812f2fe55441a2024a8cff5d03cfe4e6de3aa 100644 (file)
@@ -8,11 +8,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 com.google.common.cache.CacheBuilder;
-import com.google.common.cache.CacheLoader;
-import com.google.common.cache.LoadingCache;
+import com.google.common.collect.Interner;
+import com.google.common.collect.Interners;
+import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
 import org.kohsuke.MetaInfServices;
@@ -47,44 +48,35 @@ public class Uint16 extends Number implements CanonicalValue<Uint16> {
     private static final int MIN_VALUE_INT = 0;
     private static final int MAX_VALUE_INT = 65535;
 
-    /**
-     * Cache of first 256 values.
-     */
-    private static final Uint16[] CACHE = new Uint16[256];
+    private static final String CACHE_SIZE_PROPERTY = "org.opendaylight.yangtools.yang.common.Uint16.cache.size";
+    private static final int DEFAULT_CACHE_SIZE = 256;
 
     /**
-     * Commonly encountered values.
+     * Tunable cache for values. By default it holds {@value #DEFAULT_CACHE_SIZE} entries. This can be
+     * changed via {@value #CACHE_SIZE_PROPERTY} system property.
      */
-    private static final Uint16[] COMMON = new Uint16[] {
-        new Uint16(Short.MAX_VALUE),
-        new Uint16((short)32768),
-        new Uint16((short)65535),
-    };
+    private static final int CACHE_SIZE;
 
-    public static final Uint16 MIN_VALUE = valueOf(MIN_VALUE_INT);
-    public static final Uint16 MAX_VALUE = valueOf(MAX_VALUE_INT);
+    static {
+        final int p = Integer.getInteger(CACHE_SIZE_PROPERTY, DEFAULT_CACHE_SIZE);
+        CACHE_SIZE = p >= 0 ? Math.min(p, MAX_VALUE_INT + 1) : DEFAULT_CACHE_SIZE;
+    }
 
-    /**
-     * Tunable weak LRU cache for other values. By default it holds {@value #DEFAULT_LRU_SIZE} entries. This can be
-     * changed via {@value #LRU_SIZE_PROPERTY} system property.
-     */
-    private static final int DEFAULT_LRU_SIZE = 1024;
-    private static final String LRU_SIZE_PROPERTY = "org.opendaylight.yangtools.yang.common.Uint16.LRU.size";
-    private static final int MAX_LRU_SIZE = MAX_VALUE_INT + 1;
-    private static final int LRU_SIZE;
+    private static final @NonNull Uint16[] CACHE;
 
     static {
-        final int p = Integer.getInteger(LRU_SIZE_PROPERTY, DEFAULT_LRU_SIZE);
-        LRU_SIZE = p >= 0 ? Math.min(p, MAX_LRU_SIZE) : DEFAULT_LRU_SIZE;
+        final Uint16[] c = new Uint16[CACHE_SIZE];
+        for (int i = MIN_VALUE_INT; i < c.length; ++i) {
+            c[i] = new Uint16((short) i);
+        }
+        CACHE = c;
     }
 
-    private static final LoadingCache<Short, Uint16> LRU = CacheBuilder.newBuilder().weakValues().maximumSize(LRU_SIZE)
-            .build(new CacheLoader<Short, Uint16>() {
-                @Override
-                public Uint16 load(final Short key) {
-                    return new Uint16(key);
-                }
-            });
+    private static final Interner<Uint16> INTERNER = Interners.newWeakInterner();
+
+    public static final Uint16 ZERO = valueOf(MIN_VALUE_INT).intern();
+    public static final Uint16 ONE = valueOf(1).intern();
+    public static final Uint16 MAX_VALUE = valueOf(MAX_VALUE_INT).intern();
 
     private final short value;
 
@@ -98,79 +90,145 @@ public class Uint16 extends Number implements CanonicalValue<Uint16> {
 
     private static Uint16 instanceFor(final short value) {
         final int slot = Short.toUnsignedInt(value);
-        return slot < CACHE.length ? fromCache(slot, value) : fromCommon(value);
-    }
-
-    private static Uint16 fromCommon(final short value) {
-        for (Uint16 c : COMMON) {
-            if (c.value == value) {
-                return c;
-            }
-        }
-        return LRU.getUnchecked(value);
-    }
-
-    private static Uint16 fromCache(final int slot, final short value) {
-        // FIXME: 4.0.0: use VarHandles here
-        Uint16 ret = CACHE[slot];
-        if (ret == null) {
-            synchronized (CACHE) {
-                ret = CACHE[slot];
-                if (ret == null) {
-                    ret = new Uint16(value);
-                    CACHE[slot] = ret;
-                }
-            }
-        }
-        return ret;
+        return slot < CACHE.length ? CACHE[slot] : new Uint16(value);
     }
 
+    /**
+     * Returns an {@code Uint16} corresponding to a given bit representation. The argument is interpreted as an
+     * unsigned 16-bit value.
+     *
+     * @param bits unsigned bit representation
+     * @return A Uint16 instance
+     */
     public static Uint16 fromShortBits(final short bits) {
         return instanceFor(bits);
     }
 
+    /**
+     * Returns an {@code Uint16} corresponding to a given {@code byteVal}. The inverse operation is
+     * {@link #byteValue()}.
+     *
+     * @param byteVal byte value
+     * @return A Uint16 instance
+     * @throws IllegalArgumentException if byteVal is less than zero
+     */
     public static Uint16 valueOf(final byte byteVal) {
         checkArgument(byteVal >= MIN_VALUE_INT, "Negative values are not allowed");
         return instanceFor(byteVal);
     }
 
+    /**
+     * Returns an {@code Uint16} corresponding to a given {@code shortVal}. The inverse operation is
+     * {@link #shortValue()}.
+     *
+     * @param shortVal short value
+     * @return A Uint16 instance
+     * @throws IllegalArgumentException if shortVal is less than zero.
+     */
     public static Uint16 valueOf(final short shortVal) {
         checkArgument(shortVal >= MIN_VALUE_INT, "Negative values are not allowed");
         return instanceFor(shortVal);
     }
 
+    /**
+     * Returns an {@code Uint16} corresponding to a given {@code intVal}. The inverse operation is {@link #intValue()}.
+     *
+     * @param intVal int value
+     * @return A Uint16 instance
+     * @throws IllegalArgumentException if intVal is less than zero or greater than 65535.
+     */
     public static Uint16 valueOf(final int intVal) {
         checkArgument(intVal >= MIN_VALUE_INT && intVal <= MAX_VALUE_INT, "Value %s is outside of allowed range",
                 intVal);
         return instanceFor((short)(intVal & 0xffff));
     }
 
+    /**
+     * Returns an {@code Uint16} corresponding to a given {@code longVal}. The inverse operation is
+     * {@link #longValue()}.
+     *
+     * @param longVal long value
+     * @return A Uint16 instance
+     * @throws IllegalArgumentException if intVal is less than zero or greater than 65535.
+     */
     public static Uint16 valueOf(final long longVal) {
         checkArgument(longVal >= MIN_VALUE_INT && longVal <= MAX_VALUE_INT, "Value %s is outside of allowed range",
                 longVal);
         return instanceFor((short)(longVal & 0xffff));
     }
 
+    /**
+     * Returns an {@code Uint16} corresponding to a given {@code uint}.
+     *
+     * @param uint Uint8 value
+     * @return A Uint16 instance
+     * @throws NullPointerException if uint is null
+     */
     public static Uint16 valueOf(final Uint8 uint) {
         return instanceFor(uint.shortValue());
     }
 
+    /**
+     * Returns an {@code Uint8} corresponding to a given {@code uint}.
+     *
+     * @param uint Uint32 value
+     * @return A Uint16 instance
+     * @throws NullPointerException if uint is null
+     * @throws IllegalArgumentException if uint is greater than 65535.
+     */
     public static Uint16 valueOf(final Uint32 uint) {
         return valueOf(uint.longValue());
     }
 
+    /**
+     * Returns an {@code Uint8} corresponding to a given {@code uint}.
+     *
+     * @param uint Uint64 value
+     * @return A Uint16 instance
+     * @throws NullPointerException if uint is null
+     * @throws IllegalArgumentException if uint is greater than 65535.
+     */
     public static Uint16 valueOf(final Uint64 uint) {
         return valueOf(uint.longValue());
     }
 
+    /**
+     * Returns an {@code Uint16} holding the value of the specified {@code String}, parsed as an unsigned {@code int}
+     * value.
+     *
+     * @param string String to parse
+     * @return A Uint16 instance
+     * @throws NullPointerException if string is null
+     * @throws IllegalArgumentException if the parsed value is less than zero or greater than 65535
+     * @throws NumberFormatException if the string does not contain a parsable unsigned {@code int} value.
+     */
     public static Uint16 valueOf(final String string) {
         return valueOf(string, 10);
     }
 
+    /**
+     * Returns an {@code Uint16} holding the value of the specified {@code String}, parsed as an unsigned {@code int}
+     * value.
+     *
+     * @param string String to parse
+     * @param radix Radix to use
+     * @return A Uint16 instance
+     * @throws NullPointerException if string is null
+     * @throws IllegalArgumentException if the parsed value is less than zero or greater than 65535
+     * @throws NumberFormatException if the string does not contain a parsable unsigned {@code int} value, or if the
+     *                               {@code radix} is outside of allowed range.
+     */
     public static Uint16 valueOf(final String string, final int radix) {
-        return valueOf(Integer.parseInt(string, radix));
+        return valueOf(Integer.parseInt(requireNonNull(string), radix));
     }
 
+    /**
+     * {@inheritDoc}
+     *
+     * <p>
+     * The inverse operation is {@link #fromShortBits(short)}. In case this value is greater than
+     * {@link Short#MAX_VALUE}, the returned value will be equal to {@code this - 2^16}.
+     */
     @Override
     public final short shortValue() {
         return value;
@@ -212,6 +270,15 @@ public class Uint16 extends Number implements CanonicalValue<Uint16> {
         return SUPPORT;
     }
 
+    /**
+     * Return an interned (shared) instance equivalent to this object. This may return the same object.
+     *
+     * @return A shared instance.
+     */
+    public final Uint16 intern() {
+        return intValue() < CACHE_SIZE ? this : INTERNER.intern(this);
+    }
+
     @Override
     public final int hashCode() {
         return Short.hashCode(value);
index a0aae17535634f3f031f0ce63d1b013d3c6d15f1..fc776edd55ef72ce5595ba830a5218afb65313b2 100644 (file)
@@ -10,10 +10,10 @@ package org.opendaylight.yangtools.yang.common;
 import static com.google.common.base.Preconditions.checkArgument;
 
 import com.google.common.annotations.Beta;
-import com.google.common.cache.CacheBuilder;
-import com.google.common.cache.CacheLoader;
-import com.google.common.cache.LoadingCache;
+import com.google.common.collect.Interner;
+import com.google.common.collect.Interners;
 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;
@@ -48,46 +48,35 @@ public class Uint32 extends Number implements CanonicalValue<Uint32> {
     private static final long MIN_VALUE_LONG = 0;
     private static final long MAX_VALUE_LONG = 0xffffffffL;
 
-    /**
-     * Cache of first 256 values.
-     */
-    private static final Uint32[] CACHE = new Uint32[256];
-    /**
-     * Commonly encountered values.
-     */
-    private static final Uint32[] COMMON = {
-        new Uint32(Short.MAX_VALUE),
-        new Uint32(32768),
-        new Uint32(65535),
-        new Uint32(65536),
-        new Uint32(Integer.MAX_VALUE),
-        new Uint32(-1)
-    };
-
-    public static final Uint32 MIN_VALUE = valueOf(MIN_VALUE_LONG);
-    public static final Uint32 MAX_VALUE = valueOf(MAX_VALUE_LONG);
+    private static final String CACHE_SIZE_PROPERTY = "org.opendaylight.yangtools.yang.common.Uint32.cache.size";
+    private static final int DEFAULT_CACHE_SIZE = 256;
 
     /**
-     * Tunable weak LRU cache for other values. By default it holds {@value #DEFAULT_LRU_SIZE} entries. This can be
-     * changed via {@value #LRU_SIZE_PROPERTY} system property.
+     * Tunable cache for values. By default it holds {@value #DEFAULT_CACHE_SIZE} entries. This can be
+     * changed via {@value #CACHE_SIZE_PROPERTY} system property.
      */
-    private static final int DEFAULT_LRU_SIZE = 1024;
-    private static final String LRU_SIZE_PROPERTY = "org.opendaylight.yangtools.yang.common.Uint32.LRU.size";
-    private static final int MAX_LRU_SIZE = 0xffffff;
-    private static final int LRU_SIZE;
+    private static final int CACHE_SIZE;
 
     static {
-        final int p = Integer.getInteger(LRU_SIZE_PROPERTY, DEFAULT_LRU_SIZE);
-        LRU_SIZE = p >= 0 ? Math.min(p, MAX_LRU_SIZE) : DEFAULT_LRU_SIZE;
+        final int p = Integer.getInteger(CACHE_SIZE_PROPERTY, DEFAULT_CACHE_SIZE);
+        CACHE_SIZE = p >= 0 ? Math.min(p, Integer.MAX_VALUE) : DEFAULT_CACHE_SIZE;
     }
 
-    private static final LoadingCache<Integer, Uint32> LRU = CacheBuilder.newBuilder().weakValues()
-        .maximumSize(LRU_SIZE).build(new CacheLoader<Integer, Uint32>() {
-                @Override
-                public Uint32 load(final Integer key) {
-                    return new Uint32(key);
-                }
-            });
+    private static final @NonNull Uint32[] CACHE;
+
+    static {
+        final Uint32[] c = new Uint32[CACHE_SIZE];
+        for (int i = 0; i < c.length; ++i) {
+            c[i] = new Uint32(i);
+        }
+        CACHE = c;
+    }
+
+    private static final Interner<Uint32> INTERNER = Interners.newWeakInterner();
+
+    public static final Uint32 ZERO = valueOf(0).intern();
+    public static final Uint32 ONE = valueOf(1).intern();
+    public static final Uint32 MAX_VALUE = valueOf(MAX_VALUE_LONG).intern();
 
     private final int value;
 
@@ -101,82 +90,154 @@ public class Uint32 extends Number implements CanonicalValue<Uint32> {
 
     private static Uint32 instanceFor(final int value) {
         final long longSlot = Integer.toUnsignedLong(value);
-        return longSlot < CACHE.length ? fromCache((int)longSlot, value) : fromCommon(value);
-    }
-
-    private static Uint32 fromCommon(final int value) {
-        for (Uint32 c : COMMON) {
-            if (c.value == value) {
-                return c;
-            }
-        }
-        return LRU.getUnchecked(value);
-    }
-
-    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) {
-                ret = CACHE[slot];
-                if (ret == null) {
-                    ret = new Uint32(value);
-                    CACHE[slot] = ret;
-                }
-            }
-        }
-        return ret;
+        return longSlot < CACHE.length ? CACHE[(int)longSlot] : new Uint32(value);
     }
 
+    /**
+     * Returns an {@code Uint32} corresponding to a given bit representation. The argument is interpreted as an
+     * unsigned 32-bit value.
+     *
+     * @param bits unsigned bit representation
+     * @return A Uint32 instance
+     */
     public static Uint32 fromIntBits(final int bits) {
         return instanceFor(bits);
     }
 
-    public static Uint32 fromUnsignedInteger(final UnsignedInteger uint) {
-        return instanceFor(uint.intValue());
-    }
-
+    /**
+     * Returns an {@code Uint32} corresponding to a given {@code byteVal}. The inverse operation is
+     * {@link #byteValue()}.
+     *
+     * @param byteVal byte value
+     * @return A Uint32 instance
+     * @throws IllegalArgumentException if byteVal is less than zero
+     */
     public static Uint32 valueOf(final byte byteVal) {
         checkArgument(byteVal >= MIN_VALUE_LONG, "Negative values are not allowed");
         return instanceFor(byteVal);
     }
 
+    /**
+     * Returns an {@code Uint32} corresponding to a given {@code shortVal}. The inverse operation is
+     * {@link #shortValue()}.
+     *
+     * @param shortVal short value
+     * @return A Uint32 instance
+     * @throws IllegalArgumentException if shortVal is less than zero
+     */
     public static Uint32 valueOf(final short shortVal) {
         checkArgument(shortVal >= MIN_VALUE_LONG, "Negative values are not allowed");
         return instanceFor(shortVal);
     }
 
+    /**
+     * Returns an {@code Uint32} corresponding to a given {@code intVal}. The inverse operation is {@link #intValue()}.
+     *
+     * @param intVal int value
+     * @return A Uint32 instance
+     * @throws IllegalArgumentException if intVal is less than zero
+     */
     public static Uint32 valueOf(final int intVal) {
-        checkArgument(intVal >= MIN_VALUE_LONG, "Value %s is outside of allowed range", intVal);
+        checkArgument(intVal >= MIN_VALUE_LONG, "Negative values are not allowed");
         return instanceFor(intVal);
     }
 
+    /**
+     * Returns an {@code Uint32} corresponding to a given {@code longVal}. The inverse operation is
+     * {@link #longValue()}.
+     *
+     * @param longVal long value
+     * @return A Uint8 instance
+     * @throws IllegalArgumentException if intVal is less than zero or greater than 4294967295
+     */
     public static Uint32 valueOf(final long longVal) {
         checkArgument(longVal >= MIN_VALUE_LONG && longVal <= MAX_VALUE_LONG, "Value %s is outside of allowed range",
                 longVal);
         return instanceFor((int)longVal);
     }
 
+    /**
+     * Returns an {@code Uint32} corresponding to a given {@code uint}.
+     *
+     * @param uint Uint8 value
+     * @return A Uint32 instance
+     * @throws NullPointerException if uint is null
+     */
     public static Uint32 valueOf(final Uint8 uint) {
         return instanceFor(uint.shortValue());
     }
 
+    /**
+     * Returns an {@code Uint32} corresponding to a given {@code uint}.
+     *
+     * @param uint Uint16 value
+     * @return A Uint32 instance
+     * @throws NullPointerException if uint is null
+     */
     public static Uint32 valueOf(final Uint16 uint) {
         return instanceFor(uint.intValue());
     }
 
+    /**
+     * Returns an {@code Uint32} corresponding to a given {@code uint}.
+     *
+     * @param uint Uint64 value
+     * @return A Uint32 instance
+     * @throws NullPointerException if uint is null
+     * @throws IllegalArgumentException if uint is greater than 4294967295
+     */
     public static Uint32 valueOf(final Uint64 uint) {
         return valueOf(uint.longValue());
     }
 
+    /**
+     * Returns an {@code Uint32} corresponding to a given {@code uint}.
+     *
+     * @param uint UnsignedInteger value
+     * @return A Uint32 instance
+     * @throws NullPointerException if uint is null
+     */
+    public static Uint32 valueOf(final UnsignedInteger uint) {
+        return instanceFor(uint.intValue());
+    }
+
+    /**
+     * Returns an {@code Uint32} holding the value of the specified {@code String}, parsed as an unsigned {@code long}
+     * value.
+     *
+     * @param string String to parse
+     * @return A Uint32 instance
+     * @throws NullPointerException if string is null
+     * @throws IllegalArgumentException if the parsed value is less than zero or greater than 4294967295
+     * @throws NumberFormatException if the string does not contain a parsable unsigned {@code long} value.
+     */
     public static Uint32 valueOf(final String string) {
         return valueOf(string, 10);
     }
 
+    /**
+     * Returns an {@code Uint32} holding the value of the specified {@code String}, parsed as an unsigned {@code long}
+     * value.
+     *
+     * @param string String to parse
+     * @param radix Radix to use
+     * @return A Uint32 instance
+     * @throws NullPointerException if string is null
+     * @throws IllegalArgumentException if the parsed value is less than zero or greater than 4294967295
+     * @throws NumberFormatException if the string does not contain a parsable unsigned {@code long} value, or if the
+     *                               {@code radix} is outside of allowed range.
+     */
     public static Uint32 valueOf(final String string, final int radix) {
         return instanceFor(Integer.parseUnsignedInt(string, radix));
     }
 
+    /**
+     * {@inheritDoc}
+     *
+     * <p>
+     * The inverse operation is {@link #fromIntBits(int)}. In case this value is greater than {@link Integer#MAX_VALUE},
+     * the returned value will be equal to {@code this - 2^32}.
+     */
     @Override
     public final int intValue() {
         return value;
@@ -197,6 +258,11 @@ public class Uint32 extends Number implements CanonicalValue<Uint32> {
         return longValue();
     }
 
+    /**
+     * Convert this value to an {@link UnsignedInteger}.
+     *
+     * @return An UnsignedInteger instance
+     */
     public final UnsignedInteger toUnsignedInteger() {
         return UnsignedInteger.fromIntBits(value);
     }
@@ -217,6 +283,15 @@ public class Uint32 extends Number implements CanonicalValue<Uint32> {
         return SUPPORT;
     }
 
+    /**
+     * Return an interned (shared) instance equivalent to this object. This may return the same object.
+     *
+     * @return A shared instance.
+     */
+    public final Uint32 intern() {
+        return value >= 0 && value < CACHE_SIZE ? this : INTERNER.intern(this);
+    }
+
     @Override
     public final int hashCode() {
         return Integer.hashCode(value);
index 8e069fc9cae0a1ab0da02a1dbb18bb83c5f81aa3..8896e2cf08da3c1661041b92b563203425ba3852 100644 (file)
@@ -10,11 +10,11 @@ package org.opendaylight.yangtools.yang.common;
 import static com.google.common.base.Preconditions.checkArgument;
 
 import com.google.common.annotations.Beta;
-import com.google.common.cache.CacheBuilder;
-import com.google.common.cache.CacheLoader;
-import com.google.common.cache.LoadingCache;
+import com.google.common.collect.Interner;
+import com.google.common.collect.Interners;
 import com.google.common.primitives.UnsignedLong;
 import java.math.BigInteger;
+import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
 import org.kohsuke.MetaInfServices;
@@ -48,48 +48,35 @@ public class Uint64 extends Number implements CanonicalValue<Uint64> {
     private static final long serialVersionUID = 1L;
     private static final long MIN_VALUE_LONG = 0;
 
+    private static final String CACHE_SIZE_PROPERTY = "org.opendaylight.yangtools.yang.common.Uint64.cache.size";
+    private static final int DEFAULT_CACHE_SIZE = 256;
+
     /**
-     * Cache of first 256 values.
-     */
-    private static final Uint64[] CACHE = new Uint64[256];
-    /**
-     * Commonly encountered values.
+     * Tunable cache for values. By default it holds {@value #DEFAULT_CACHE_SIZE} entries. This can be
+     * changed via {@value #CACHE_SIZE_PROPERTY} system property.
      */
-    private static final Uint64[] COMMON = {
-        new Uint64(Short.MAX_VALUE + 1L),
-        new Uint64(32768),
-        new Uint64(65535),
-        new Uint64(65536),
-        new Uint64(Integer.MAX_VALUE),
-        new Uint64(Integer.MAX_VALUE + 1L),
-        new Uint64(Long.MAX_VALUE),
-        new Uint64(-1L)
-    };
+    private static final long CACHE_SIZE;
 
-    public static final Uint64 MIN_VALUE = valueOf(MIN_VALUE_LONG);
-    public static final Uint64 MAX_VALUE = fromLongBits(-1);
+    static {
+        final int p = Integer.getInteger(CACHE_SIZE_PROPERTY, DEFAULT_CACHE_SIZE);
+        CACHE_SIZE = p >= 0 ? Math.min(p, Integer.MAX_VALUE) : DEFAULT_CACHE_SIZE;
+    }
 
-    /**
-     * Tunable weak LRU cache for other values. By default it holds {@value #DEFAULT_LRU_SIZE} entries. This can be
-     * changed via {@value #LRU_SIZE_PROPERTY} system property.
-     */
-    private static final int DEFAULT_LRU_SIZE = 1024;
-    private static final String LRU_SIZE_PROPERTY = "org.opendaylight.yangtools.yang.common.Uint64.LRU.size";
-    private static final int MAX_LRU_SIZE = 0xffffff;
-    private static final int LRU_SIZE;
+    private static final @NonNull Uint64[] CACHE;
 
     static {
-        final int p = Integer.getInteger(LRU_SIZE_PROPERTY, DEFAULT_LRU_SIZE);
-        LRU_SIZE = p >= 0 ? Math.min(p, MAX_LRU_SIZE) : DEFAULT_LRU_SIZE;
+        final Uint64[] c = new Uint64[(int) CACHE_SIZE];
+        for (int i = 0; i < c.length; ++i) {
+            c[i] = new Uint64(i);
+        }
+        CACHE = c;
     }
 
-    private static final LoadingCache<Long, Uint64> LRU = CacheBuilder.newBuilder().weakValues().maximumSize(LRU_SIZE)
-            .build(new CacheLoader<Long, Uint64>() {
-                @Override
-                public Uint64 load(final Long key) {
-                    return new Uint64(key);
-                }
-            });
+    private static final Interner<Uint64> INTERNER = Interners.newWeakInterner();
+
+    public static final Uint64 ZERO = valueOf(0).intern();
+    public static final Uint64 ONE = valueOf(1).intern();
+    public static final Uint64 MAX_VALUE = fromLongBits(-1).intern();
 
     private final long value;
 
@@ -103,93 +90,171 @@ public class Uint64 extends Number implements CanonicalValue<Uint64> {
 
     private static Uint64 instanceFor(final long value) {
         final int slot = (int)value;
-        return slot >= 0 && slot < CACHE.length ? fromCache(slot, value) : fromCommon(value);
-    }
-
-    private static Uint64 fromCommon(final long value) {
-        for (Uint64 c : COMMON) {
-            if (c.value == value) {
-                return c;
-            }
-        }
-        return LRU.getUnchecked(value);
-    }
-
-    private static Uint64 fromCache(final int slot, final long value) {
-        // FIXME: 4.0.0: use VarHandles here
-        Uint64 ret = CACHE[slot];
-        if (ret == null) {
-            synchronized (CACHE) {
-                ret = CACHE[slot];
-                if (ret == null) {
-                    ret = new Uint64(value);
-                    CACHE[slot] = ret;
-                }
-            }
-        }
-        return ret;
+        return slot >= 0 && slot < CACHE.length ? CACHE[slot] : new Uint64(value);
     }
 
+    /**
+     * Returns an {@code Uint64} corresponding to a given bit representation. The argument is interpreted as an
+     * unsigned 64-bit value.
+     *
+     * @param bits unsigned bit representation
+     * @return A Uint64 instance
+     */
     public static Uint64 fromLongBits(final long bits) {
         return instanceFor(bits);
     }
 
-    public static Uint64 fromUnsignedLong(final UnsignedLong ulong) {
-        return instanceFor(ulong.longValue());
-    }
-
+    /**
+     * Returns an {@code Uint64} corresponding to a given {@code byteVal}. The inverse operation is
+     * {@link #byteValue()}.
+     *
+     * @param byteVal byte value
+     * @return A Uint64 instance
+     * @throws IllegalArgumentException if byteVal is less than zero
+     */
     public static Uint64 valueOf(final byte byteVal) {
         checkArgument(byteVal >= MIN_VALUE_LONG, "Negative values are not allowed");
         return instanceFor(byteVal);
     }
 
+    /**
+     * Returns an {@code Uint64} corresponding to a given {@code shortVal}. The inverse operation is
+     * {@link #shortValue()}.
+     *
+     * @param shortVal short value
+     * @return A Uint64 instance
+     * @throws IllegalArgumentException if shortVal is less than zero
+     */
     public static Uint64 valueOf(final short shortVal) {
         checkArgument(shortVal >= MIN_VALUE_LONG, "Negative values are not allowed");
         return instanceFor(shortVal);
     }
 
+    /**
+     * Returns an {@code Uint64} corresponding to a given {@code intVal}. The inverse operation is {@link #intValue()}.
+     *
+     * @param intVal int value
+     * @return A Uint64 instance
+     * @throws IllegalArgumentException if intVal is less than zero
+     */
     public static Uint64 valueOf(final int intVal) {
-        checkArgument(intVal >= MIN_VALUE_LONG, "Value %s is outside of allowed range", intVal);
+        checkArgument(intVal >= MIN_VALUE_LONG, "Negative values are not allowed");
         return instanceFor(intVal);
     }
 
+    /**
+     * Returns an {@code Uint64} corresponding to a given {@code longVal}. The inverse operation is
+     * {@link #fromLongBits(long)}.
+     *
+     * @param longVal long value
+     * @return A Uint8 instance
+     * @throws IllegalArgumentException if intVal is less than zero
+     */
     public static Uint64 valueOf(final long longVal) {
-        checkArgument(longVal >= MIN_VALUE_LONG, "Value %s is outside of allowed range", longVal);
+        checkArgument(longVal >= MIN_VALUE_LONG, "Negative values are not allowed");
         return instanceFor(longVal);
     }
 
+    /**
+     * Returns an {@code Uint64} corresponding to a given {@code uint}.
+     *
+     * @param uint Uint8 value
+     * @return A Uint64 instance
+     * @throws NullPointerException if uint is null
+     */
     public static Uint64 valueOf(final Uint8 uint) {
         return instanceFor(uint.shortValue());
     }
 
+    /**
+     * Returns an {@code Uint64} corresponding to a given {@code uint}.
+     *
+     * @param uint Uint16 value
+     * @return A Uint64 instance
+     * @throws NullPointerException if uint is null
+     */
     public static Uint64 valueOf(final Uint16 uint) {
         return instanceFor(uint.intValue());
     }
 
+    /**
+     * Returns an {@code Uint64} corresponding to a given {@code uint}.
+     *
+     * @param uint Uint32 value
+     * @return A Uint64 instance
+     * @throws NullPointerException if uint is null
+     */
     public static Uint64 valueOf(final Uint32 uint) {
         return instanceFor(uint.longValue());
     }
 
-    public static Uint64 valueOf(final String string) {
-        return valueOf(string, 10);
-    }
-
-    public static Uint64 valueOf(final String string, final int radix) {
-        return instanceFor(Long.parseUnsignedLong(string, radix));
+    /**
+     * Returns an {@code Uint64} corresponding to a given {@code ulong}.
+     *
+     * @param ulong UnsignedLong value
+     * @return A Uint64 instance
+     * @throws NullPointerException if uint is null
+     */
+    public static Uint64 valueOf(final UnsignedLong ulong) {
+        return instanceFor(ulong.longValue());
     }
 
+    /**
+     * Returns an {@code Uint64} corresponding to a given {@code bigInt}.
+     *
+     * @param bigInt BigInteger value
+     * @return A Uint64 instance
+     * @throws NullPointerException if uint is null
+     * @throws IllegalArgumentException if bigInt is less than zero or greater than 18446744073709551615
+     */
     public static Uint64 valueOf(final BigInteger bigInt) {
         checkArgument(bigInt.signum() >= 0, "Negative values not allowed");
         checkArgument(bigInt.bitLength() <= Long.SIZE, "Value %s is outside of allowed range", bigInt);
-
         return instanceFor(bigInt.longValue());
     }
 
+    /**
+     * Returns an {@code Uint32} holding the value of the specified {@code String}, parsed as an unsigned {@code long}
+     * value.
+     *
+     * @param string String to parse
+     * @return A Uint64 instance
+     * @throws NullPointerException if string is null
+     * @throws IllegalArgumentException if the parsed value is less than zero or greater than 18446744073709551615
+     * @throws NumberFormatException if the string does not contain a parsable unsigned {@code long} value.
+     */
+    public static Uint64 valueOf(final String string) {
+        return valueOf(string, 10);
+    }
+
+    /**
+     * Returns an {@code Uint64} holding the value of the specified {@code String}, parsed as an unsigned {@code long}
+     * value.
+     *
+     * @param string String to parse
+     * @param radix Radix to use
+     * @return A Uint64 instance
+     * @throws NullPointerException if string is null
+     * @throws IllegalArgumentException if the parsed value is less than zero or greater than 18446744073709551615
+     * @throws NumberFormatException if the string does not contain a parsable unsigned {@code long} value, or if the
+     *                               {@code radix} is outside of allowed range.
+     */
+    public static Uint64 valueOf(final String string, final int radix) {
+        return instanceFor(Long.parseUnsignedLong(string, radix));
+    }
+
     @Override
     public final int intValue() {
         return (int)value;
     }
 
+    /**
+     * {@inheritDoc}
+     *
+     * <p>
+     * The inverse operation is {@link #fromLongBits(long)}. In case this value is greater than {@link Long#MAX_VALUE},
+     * the returned value will be equal to {@code this - 2^64}.
+     */
     @Override
     public final long longValue() {
         return value;
@@ -207,6 +272,11 @@ public class Uint64 extends Number implements CanonicalValue<Uint64> {
         return UnsignedLong.fromLongBits(value).doubleValue();
     }
 
+    /**
+     * Convert this value to an {@link UnsignedLong}.
+     *
+     * @return An UnsignedLong instance
+     */
     public final UnsignedLong toUnsignedLong() {
         return UnsignedLong.fromLongBits(value);
     }
@@ -227,6 +297,16 @@ public class Uint64 extends Number implements CanonicalValue<Uint64> {
         return SUPPORT;
     }
 
+    /**
+     * Return an interned (shared) instance equivalent to this object. This may return the same object.
+     *
+     * @return A shared instance.
+     */
+    public final Uint64 intern() {
+        return value >= 0 && value < CACHE_SIZE ? this : INTERNER.intern(this);
+    }
+
+
     @Override
     public final int hashCode() {
         return Long.hashCode(value);
index 8a239e427bc0fda8dfa2bc6f916bcac8a56d4e76..f3aaa4f480e54d91a459fe35ff63f8528164f360 100644 (file)
@@ -8,8 +8,10 @@
 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;
@@ -45,9 +47,19 @@ public class Uint8 extends Number implements CanonicalValue<Uint8> {
     private static final short MAX_VALUE_SHORT = 255;
 
     private static final long serialVersionUID = 1L;
-    private static final Uint8[] CACHE = new Uint8[MAX_VALUE_SHORT + 1];
 
-    public static final Uint8 MIN_VALUE = valueOf(MIN_VALUE_SHORT);
+    private static final @NonNull Uint8[] CACHE;
+
+    static {
+        final Uint8[] c = new Uint8[MAX_VALUE_SHORT + 1];
+        for (int i = MIN_VALUE_SHORT; i <= MAX_VALUE_SHORT; ++i) {
+            c[i] = new Uint8((byte)i);
+        }
+        CACHE = c;
+    }
+
+    public static final Uint8 ZERO = valueOf(MIN_VALUE_SHORT);
+    public static final Uint8 ONE = valueOf(1);
     public static final Uint8 MAX_VALUE = valueOf(MAX_VALUE_SHORT);
 
     private final byte value;
@@ -61,70 +73,146 @@ public class Uint8 extends Number implements CanonicalValue<Uint8> {
     }
 
     private static Uint8 instanceFor(final byte value) {
-        final int slot = Byte.toUnsignedInt(value);
-
-        // FIXME: 4.0.0: use VarHandles here
-        Uint8 ret = CACHE[slot];
-        if (ret == null) {
-            synchronized (CACHE) {
-                ret = CACHE[slot];
-                if (ret == null) {
-                    ret = new Uint8(value);
-                    CACHE[slot] = ret;
-                }
-            }
-        }
-
-        return ret;
+        return CACHE[Byte.toUnsignedInt(value)];
     }
 
+    /**
+     * Returns an {@code Uint8} corresponding to a given bit representation. The argument is interpreted as an
+     * unsigned 8-bit value.
+     *
+     * @param bits unsigned bit representation
+     * @return A Uint8 instance
+     */
     public static Uint8 fromByteBits(final byte bits) {
         return instanceFor(bits);
     }
 
+    /**
+     * Returns an {@code Uint8} corresponding to a given {@code byteVal}. The inverse operation is {@link #byteValue()}.
+     *
+     * @param byteVal byte value
+     * @return A Uint8 instance
+     * @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");
         return instanceFor(byteVal);
     }
 
+    /**
+     * Returns an {@code Uint8} corresponding to a given {@code shortVal}. The inverse operation is
+     * {@link #shortValue()}.
+     *
+     * @param shortVal short value
+     * @return A Uint8 instance
+     * @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));
     }
 
+    /**
+     * Returns an {@code Uint8} corresponding to a given {@code intVal}. The inverse operation is {@link #intValue()}.
+     *
+     * @param intVal int value
+     * @return A Uint8 instance
+     * @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));
     }
 
+    /**
+     * Returns an {@code Uint8} corresponding to a given {@code longVal}. The inverse operation is
+     * {@link #longValue()}.
+     *
+     * @param longVal long value
+     * @return A Uint8 instance
+     * @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));
     }
 
+    /**
+     * Returns an {@code Uint8} corresponding to a given {@code uint}.
+     *
+     * @param uint Uint16 value
+     * @return A Uint8 instance
+     * @throws NullPointerException if uint is null
+     * @throws IllegalArgumentException if uint is greater than 255.
+     */
     public static Uint8 valueOf(final Uint16 uint) {
         return valueOf(uint.intValue());
     }
 
+    /**
+     * Returns an {@code Uint8} corresponding to a given {@code uint}.
+     *
+     * @param uint Uint32 value
+     * @return A Uint8 instance
+     * @throws NullPointerException if uint is null
+     * @throws IllegalArgumentException if uint is greater than 255.
+     */
     public static Uint8 valueOf(final Uint32 uint) {
         return valueOf(uint.longValue());
     }
 
+    /**
+     * Returns an {@code Uint8} corresponding to a given {@code uint}.
+     *
+     * @param uint Uint64 value
+     * @return A Uint8 instance
+     * @throws NullPointerException if uint is null
+     * @throws IllegalArgumentException if uint is greater than 255.
+     */
     public static Uint8 valueOf(final Uint64 uint) {
         return valueOf(uint.longValue());
     }
 
+    /**
+     * Returns an {@code Uint8} holding the value of the specified {@code String}, parsed as an unsigned {@code short}
+     * value.
+     *
+     * @param string String to parse
+     * @return A Uint8 instance
+     * @throws NullPointerException if string is null
+     * @throws IllegalArgumentException if the parsed value is less than zero or greater than 255
+     * @throws NumberFormatException if the string does not contain a parsable unsigned {@code short} value.
+     */
     public static Uint8 valueOf(final String string) {
         return valueOf(string, 10);
     }
 
+    /**
+     * Returns an {@code Uint8} holding the value of the specified {@code String}, parsed as an unsigned {@code short}
+     * value.
+     *
+     * @param string String to parse
+     * @param radix Radix to use
+     * @return A Uint8 instance
+     * @throws NullPointerException if string is null
+     * @throws IllegalArgumentException if the parsed value is less than zero or greater than 255
+     * @throws NumberFormatException if the string does not contain a parsable unsigned {@code short} value, or if the
+     *                               {@code radix} is outside of allowed range.
+     */
     public static Uint8 valueOf(final String string, final int radix) {
-        return valueOf(Short.parseShort(string, radix));
+        return valueOf(Short.parseShort(requireNonNull(string), radix));
     }
 
+    /**
+     * {@inheritDoc}
+     *
+     * <p>
+     * The inverse operation is {@link #fromByteBits(byte)}. In case this value is greater than {@link Byte#MAX_VALUE},
+     * the returned value will be equal to {@code this - 2^8}.
+     */
     @Override
     public final byte byteValue() {
         return value;
index 0a301c9e20e17aa49b95cdfd4a7976f49338d03f..eafa32f2f771acf4fdca3f4854ada60ae6475b95 100644 (file)
@@ -93,7 +93,7 @@ public class Uint32Test {
         assertSame(Uint32.valueOf(10), Uint32.valueOf(Uint16.valueOf(10)));
         assertSame(Uint32.valueOf(20), Uint32.valueOf(Uint64.valueOf(20)));
 
-        assertSame(Uint32.valueOf(5), Uint32.fromUnsignedInteger(UnsignedInteger.fromIntBits(5)));
+        assertSame(Uint32.valueOf(5), Uint32.valueOf(UnsignedInteger.fromIntBits(5)));
         assertEquals(UnsignedInteger.fromIntBits(5), Uint32.valueOf(5).toUnsignedInteger());
     }
 
index 39e2014ddd651a8de88f819d89ea13ff1d3e6131..130c8b1751134e15b9db9029e5adb7c8cb06c5d6 100644 (file)
@@ -95,7 +95,7 @@ public class Uint64Test {
         assertSame(Uint64.valueOf(20), Uint64.valueOf(Uint32.valueOf(20)));
         assertEquals(Uint64.valueOf(30), Uint64.valueOf(new BigInteger("30")));
 
-        assertSame(Uint64.valueOf(5), Uint64.fromUnsignedLong(UnsignedLong.fromLongBits(5)));
+        assertSame(Uint64.valueOf(5), Uint64.valueOf(UnsignedLong.fromLongBits(5)));
         assertEquals(UnsignedLong.fromLongBits(5), Uint64.valueOf(5).toUnsignedLong());
     }