Convert base types to implement CanonicalValue
[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.eclipse.jdt.annotation.NonNullByDefault;
17 import org.eclipse.jdt.annotation.Nullable;
18
19 /**
20  * Dedicated type for YANG's 'type uint16' type.
21  *
22  * @author Robert Varga
23  */
24 @Beta
25 @NonNullByDefault
26 public class Uint16 extends Number implements CanonicalValue<Uint16> {
27     private static final class Support extends AbstractCanonicalValueSupport<Uint16> {
28         Support() {
29             super(Uint16.class);
30         }
31
32         @Override
33         public Uint16 fromString(final String str) {
34             return Uint16.valueOf(str);
35         }
36     }
37
38     private static final CanonicalValueSupport<Uint16> SUPPORT = new Support();
39     private static final long serialVersionUID = 1L;
40     private static final int MIN_VALUE = 0;
41     private static final int MAX_VALUE = 65535;
42
43     /**
44      * Cache of first 256 values.
45      */
46     private static final Uint16[] CACHE = new Uint16[Uint8.MAX_VALUE];
47
48     /**
49      * Commonly encountered values.
50      */
51     private static final Uint16[] COMMON = new Uint16[] {
52         new Uint16(Short.MAX_VALUE),
53         new Uint16((short)32768),
54         new Uint16((short)65535),
55     };
56
57     /**
58      * Tunable weak LRU cache for other values. By default it holds {@value #DEFAULT_LRU_SIZE} entries. This can be
59      * changed via {@value #LRU_SIZE_PROPERTY} system property.
60      */
61     private static final int DEFAULT_LRU_SIZE = 1024;
62     private static final String LRU_SIZE_PROPERTY = "org.opendaylight.yangtools.yang.common.Uint16.LRU.size";
63     private static final int MAX_LRU_SIZE = MAX_VALUE + 1;
64     private static final int LRU_SIZE;
65
66     static {
67         final int p = Integer.getInteger(LRU_SIZE_PROPERTY, DEFAULT_LRU_SIZE);
68         LRU_SIZE = p >= 0 ? Math.min(p, MAX_LRU_SIZE) : DEFAULT_LRU_SIZE;
69     }
70
71     private static final LoadingCache<Short, Uint16> LRU = CacheBuilder.newBuilder().weakValues().maximumSize(LRU_SIZE)
72             .build(new CacheLoader<Short, Uint16>() {
73                 @Override
74                 public Uint16 load(final Short key) {
75                     return new Uint16(key);
76                 }
77             });
78
79     private final short value;
80
81     Uint16(final short value) {
82         this.value = value;
83     }
84
85     protected Uint16(final Uint16 other) {
86         this.value = other.value;
87     }
88
89     private static Uint16 instanceFor(final short value) {
90         final int slot = Short.toUnsignedInt(value);
91         if (slot >= CACHE.length) {
92             for (Uint16 c : COMMON) {
93                 if (c.value == value) {
94                     return c;
95                 }
96             }
97
98             return LRU.getUnchecked(value);
99         }
100
101         Uint16 ret = CACHE[slot];
102         if (ret == null) {
103             synchronized (CACHE) {
104                 ret = CACHE[slot];
105                 if (ret == null) {
106                     ret = new Uint16(value);
107                     CACHE[slot] = ret;
108                 }
109             }
110         }
111
112         return ret;
113     }
114
115     public static Uint16 fromShortBits(final short bits) {
116         return instanceFor(bits);
117     }
118
119     public static Uint16 valueOf(final byte byteVal) {
120         checkArgument(byteVal >= MIN_VALUE, "Negative values are not allowed");
121         return instanceFor(byteVal);
122     }
123
124     public static Uint16 valueOf(final short shortVal) {
125         checkArgument(shortVal >= MIN_VALUE, "Negative values are not allowed");
126         return instanceFor(shortVal);
127     }
128
129     public static Uint16 valueOf(final int intVal) {
130         checkArgument(intVal >= MIN_VALUE && intVal <= MAX_VALUE, "Value %s is outside of allowed range", intVal);
131         return instanceFor((short)(intVal & 0xffff));
132     }
133
134     public static Uint16 valueOf(final long longVal) {
135         checkArgument(longVal >= MIN_VALUE && longVal <= MAX_VALUE, "Value %s is outside of allowed range", longVal);
136         return instanceFor((short)(longVal & 0xffff));
137     }
138
139     public static Uint16 valueOf(final Uint8 uint) {
140         return instanceFor(uint.shortValue());
141     }
142
143     public static Uint16 valueOf(final Uint32 uint) {
144         return valueOf(uint.longValue());
145     }
146
147     public static Uint16 valueOf(final Uint64 uint) {
148         return valueOf(uint.longValue());
149     }
150
151     public static Uint16 valueOf(final String string) {
152         return valueOf(string, 10);
153     }
154
155     public static Uint16 valueOf(final String string, final int radix) {
156         return valueOf(Integer.parseInt(string, radix));
157     }
158
159     @Override
160     public final short shortValue() {
161         return value;
162     }
163
164     @Override
165     public final int intValue() {
166         return Short.toUnsignedInt(value);
167     }
168
169     @Override
170     public final long longValue() {
171         return Short.toUnsignedLong(value);
172     }
173
174     @Override
175     public final float floatValue() {
176         return intValue();
177     }
178
179     @Override
180     public final double doubleValue() {
181         return intValue();
182     }
183
184     @Override
185     @SuppressWarnings("checkstyle:parameterName")
186     public final int compareTo(final Uint16 o) {
187         return Integer.compare(intValue(), o.intValue());
188     }
189
190     @Override
191     public final String toCanonicalString() {
192         return String.valueOf(intValue());
193     }
194
195     @Override
196     public final CanonicalValueSupport<Uint16> support() {
197         return SUPPORT;
198     }
199
200     @Override
201     public final int hashCode() {
202         return Short.hashCode(value);
203     }
204
205     @Override
206     public final boolean equals(final @Nullable Object obj) {
207         return this == obj || obj instanceof Uint16 && value == ((Uint16)obj).value;
208     }
209
210     @Override
211     public final String toString() {
212         return toCanonicalString();
213     }
214
215     private Object readResolve() {
216         return instanceFor(value);
217     }
218 }