Tag CanonicalValueSupport services
[yangtools.git] / yang / yang-common / src / main / java / org / opendaylight / yangtools / yang / common / Uint8.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 org.eclipse.jdt.annotation.NonNullByDefault;
14 import org.eclipse.jdt.annotation.Nullable;
15 import org.kohsuke.MetaInfServices;
16 import org.opendaylight.yangtools.concepts.Variant;
17
18 /**
19  * Dedicated type for YANG's 'type uint8' type.
20  *
21  * @author Robert Varga
22  */
23 @Beta
24 @NonNullByDefault
25 public class Uint8 extends Number implements CanonicalValue<Uint8> {
26     @MetaInfServices(value = CanonicalValueSupport.class)
27     public static final class Support extends AbstractCanonicalValueSupport<Uint8> {
28         public Support() {
29             super(Uint8.class);
30         }
31
32         @Override
33         public Variant<Uint8, CanonicalValueViolation> fromString(final String str) {
34             try {
35                 return Variant.ofFirst(Uint8.valueOf(str));
36             } catch (IllegalArgumentException e) {
37                 return CanonicalValueViolation.variantOf(e);
38             }
39         }
40     }
41
42     private static final CanonicalValueSupport<Uint8> SUPPORT = new Support();
43
44     static final short MIN_VALUE = 0;
45     static final short MAX_VALUE = 255;
46
47     private static final long serialVersionUID = 1L;
48     private static final Uint8[] CACHE = new Uint8[MAX_VALUE + 1];
49
50     private final byte value;
51
52     private Uint8(final byte value) {
53         this.value = value;
54     }
55
56     protected Uint8(final Uint8 other) {
57         this.value = other.value;
58     }
59
60     private static Uint8 instanceFor(final byte value) {
61         final int slot = Byte.toUnsignedInt(value);
62
63         Uint8 ret = CACHE[slot];
64         if (ret == null) {
65             synchronized (CACHE) {
66                 ret = CACHE[slot];
67                 if (ret == null) {
68                     ret = new Uint8(value);
69                     CACHE[slot] = ret;
70                 }
71             }
72         }
73
74         return ret;
75     }
76
77     public static Uint8 fromByteBits(final byte bits) {
78         return instanceFor(bits);
79     }
80
81     public static Uint8 valueOf(final byte byteVal) {
82         checkArgument(byteVal >= MIN_VALUE, "Negative values are not allowed");
83         return instanceFor(byteVal);
84     }
85
86     public static Uint8 valueOf(final short shortVal) {
87         checkArgument(shortVal >= MIN_VALUE && shortVal <= MAX_VALUE, "Value %s is outside of allowed range", shortVal);
88         return instanceFor((byte)(shortVal & 0xff));
89     }
90
91     public static Uint8 valueOf(final int intVal) {
92         checkArgument(intVal >= MIN_VALUE && intVal <= MAX_VALUE, "Value %s is outside of allowed range", intVal);
93         return instanceFor((byte)(intVal & 0xff));
94     }
95
96     public static Uint8 valueOf(final long longVal) {
97         checkArgument(longVal >= MIN_VALUE && longVal <= MAX_VALUE, "Value %s is outside of allowed range", longVal);
98         return instanceFor((byte)(longVal & 0xff));
99     }
100
101     public static Uint8 valueOf(final Uint16 uint) {
102         return valueOf(uint.intValue());
103     }
104
105     public static Uint8 valueOf(final Uint32 uint) {
106         return valueOf(uint.longValue());
107     }
108
109     public static Uint8 valueOf(final Uint64 uint) {
110         return valueOf(uint.longValue());
111     }
112
113     public static Uint8 valueOf(final String string) {
114         return valueOf(string, 10);
115     }
116
117     public static Uint8 valueOf(final String string, final int radix) {
118         return valueOf(Short.parseShort(string, radix));
119     }
120
121     @Override
122     public final byte byteValue() {
123         return value;
124     }
125
126     @Override
127     public final int intValue() {
128         return Byte.toUnsignedInt(value);
129     }
130
131     @Override
132     public final long longValue() {
133         return Byte.toUnsignedLong(value);
134     }
135
136     @Override
137     public final float floatValue() {
138         return intValue();
139     }
140
141     @Override
142     public final double doubleValue() {
143         return intValue();
144     }
145
146     @Override
147     @SuppressWarnings("checkstyle:parameterName")
148     public final int compareTo(final Uint8 o) {
149         return intValue() - o.intValue();
150     }
151
152     @Override
153     public final String toCanonicalString() {
154         return String.valueOf(intValue());
155     }
156
157     @Override
158     public final CanonicalValueSupport<Uint8> support() {
159         return SUPPORT;
160     }
161
162     @Override
163     public final int hashCode() {
164         return Byte.hashCode(value);
165     }
166
167     @Override
168     public final boolean equals(final @Nullable Object obj) {
169         return this == obj || obj instanceof Uint8 && value == ((Uint8)obj).value;
170     }
171
172     @Override
173     public final String toString() {
174         return toCanonicalString();
175     }
176
177     private Object readResolve() {
178         return instanceFor(value);
179     }
180 }