BUG-4661: Introduce Decimal64, Empty, Uint{8,16,32,64}
[yangtools.git] / yang / yang-common / src / main / java / org / opendaylight / yangtools / yang / common / Uint16.java
1 /*
2  * Copyright (c) 2015 Pantheon Technologies s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.common;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.cache.CacheBuilder;
14 import com.google.common.cache.CacheLoader;
15 import com.google.common.cache.LoadingCache;
16 import org.opendaylight.yangtools.concepts.Immutable;
17
18 /**
19  * Dedicated type for YANG's 'type uint16' type.
20  *
21  * @author Robert Varga
22  */
23 @Beta
24 public final class Uint16 extends Number implements Comparable<Uint16>, Immutable {
25     private static final long serialVersionUID = 1L;
26     private static final int MIN_VALUE = 0;
27     private static final int MAX_VALUE = 65535;
28
29     /**
30      * Cache of first 256 values.
31      */
32     private static final Uint16[] CACHE = new Uint16[Uint8.MAX_VALUE];
33
34     /**
35      * Commonly encountered values.
36      */
37     private static final Uint16[] COMMON = new Uint16[] {
38         new Uint16(Short.MAX_VALUE),
39         new Uint16((short)32768),
40         new Uint16((short)65535),
41     };
42
43     /**
44      * Tunable weak LRU cache for other values. By default it holds {@value #DEFAULT_LRU_SIZE} entries. This can be
45      * changed via {@value #LRU_SIZE_PROPERTY} system property.
46      */
47     private static final int DEFAULT_LRU_SIZE = 1024;
48     private static final String LRU_SIZE_PROPERTY = "org.opendaylight.yangtools.yang.common.Uint16.LRU.size";
49     private static final int MAX_LRU_SIZE = MAX_VALUE + 1;
50     private static final int LRU_SIZE;
51
52     static {
53         final int p = Integer.getInteger(LRU_SIZE_PROPERTY, DEFAULT_LRU_SIZE);
54         LRU_SIZE = p >= 0 ? Math.min(p, MAX_LRU_SIZE) : DEFAULT_LRU_SIZE;
55     }
56
57     private static final LoadingCache<Short, Uint16> LRU = CacheBuilder.newBuilder().weakValues().maximumSize(LRU_SIZE)
58             .build(new CacheLoader<Short, Uint16>() {
59                 @Override
60                 public Uint16 load(final Short key) {
61                     return new Uint16(key);
62                 }
63             });
64
65     private final short value;
66
67     private Uint16(final short value) {
68         this.value = value;
69     }
70
71     private static Uint16 instanceFor(final short value) {
72         final int slot = Short.toUnsignedInt(value);
73         if (slot >= CACHE.length) {
74             for (Uint16 c : COMMON) {
75                 if (c.value == value) {
76                     return c;
77                 }
78             }
79
80             return LRU.getUnchecked(value);
81         }
82
83         Uint16 ret = CACHE[slot];
84         if (ret == null) {
85             synchronized (CACHE) {
86                 ret = CACHE[slot];
87                 if (ret == null) {
88                     ret = new Uint16(value);
89                     CACHE[slot] = ret;
90                 }
91             }
92         }
93
94         return ret;
95     }
96
97     public static Uint16 fromShortBits(final short bits) {
98         return instanceFor(bits);
99     }
100
101     public static Uint16 valueOf(final byte byteVal) {
102         checkArgument(byteVal >= MIN_VALUE, "Negative values are not allowed");
103         return instanceFor(byteVal);
104     }
105
106     public static Uint16 valueOf(final short shortVal) {
107         checkArgument(shortVal >= MIN_VALUE, "Negative values are not allowed");
108         return instanceFor(shortVal);
109     }
110
111     public static Uint16 valueOf(final int intVal) {
112         checkArgument(intVal >= MIN_VALUE && intVal <= MAX_VALUE, "Value %s is outside of allowed range", intVal);
113         return instanceFor((short)(intVal & 0xffff));
114     }
115
116     public static Uint16 valueOf(final long longVal) {
117         checkArgument(longVal >= MIN_VALUE && longVal <= MAX_VALUE, "Value %s is outside of allowed range", longVal);
118         return instanceFor((short)(longVal & 0xffff));
119     }
120
121     public static Uint16 valueOf(final Uint8 uint) {
122         return instanceFor(uint.shortValue());
123     }
124
125     public static Uint16 valueOf(final Uint32 uint) {
126         return valueOf(uint.longValue());
127     }
128
129     public static Uint16 valueOf(final String string) {
130         return valueOf(string, 10);
131     }
132
133     public static Uint16 valueOf(final String string, final int radix) {
134         // FIXME: implement this
135         throw new UnsupportedOperationException();
136     }
137
138     @Override
139     public short shortValue() {
140         return value;
141     }
142
143     @Override
144     public int intValue() {
145         return Short.toUnsignedInt(value);
146     }
147
148     @Override
149     public long longValue() {
150         return Short.toUnsignedLong(value);
151     }
152
153     @Override
154     public float floatValue() {
155         return intValue();
156     }
157
158     @Override
159     public double doubleValue() {
160         return intValue();
161     }
162
163     @Override
164     @SuppressWarnings("checkstyle:parameterName")
165     public int compareTo(final Uint16 o) {
166         return Integer.compare(intValue(), o.intValue());
167     }
168
169     @Override
170     public int hashCode() {
171         return Short.hashCode(value);
172     }
173
174     @Override
175     public boolean equals(final Object obj) {
176         if (this == obj) {
177             return true;
178         }
179
180         return obj instanceof Uint16 && value == ((Uint16)obj).value;
181     }
182
183     @Override
184     public String toString() {
185         return String.valueOf(intValue());
186     }
187
188     private Object readResolve() {
189         return instanceFor(value);
190     }
191 }