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