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