Merge branch 'master' of ../controller
[yangtools.git] / yang / yang-common / src / main / java / org / opendaylight / yangtools / yang / common / UintConversions.java
1 /*
2  * Copyright (c) 2019 PANTHEON.tech, 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 com.google.common.annotations.Beta;
11 import com.google.common.primitives.UnsignedInteger;
12 import com.google.common.primitives.UnsignedLong;
13 import java.math.BigInteger;
14 import org.eclipse.jdt.annotation.NonNullByDefault;
15
16 /**
17  * Utility methods for converting Java and Guava integer types to their {@link Uint8}, {@link Uint16}, {@link Uint32}
18  * and {@link Uint64} equivalents. While individual types provide these through their {@code valueOf()} methods, this
19  * class allows dealing with multiple types through a static import:
20  *
21  * <pre>
22  *   <code>
23  *     import static org.opendaylight.yangtools.yang.common.UintConversions.fromJava;
24  *
25  *     Uint16 two = fromJava(32);
26  *     Uint32 one = fromJava(32L);
27  *   </code>
28  * </pre>
29  *
30  * @author Robert Varga
31  */
32 @Beta
33 @NonNullByDefault
34 public final class UintConversions {
35     private UintConversions() {
36         // Hidden on purpose
37     }
38
39     /**
40      * Convert a {@code short} in range 0-255 to an Uint8.
41      *
42      * @param value value
43      * @return Uint8 object
44      * @throws IllegalArgumentException if value is less than zero or greater than 255
45      */
46     public static Uint8 fromJava(final short value) {
47         return Uint8.valueOf(value);
48     }
49
50     /**
51      * Convert an {@code int} in range 0-65535 to a Uint16.
52      *
53      * @param value value
54      * @return Uint16 object
55      * @throws IllegalArgumentException if value is less than zero or greater than 65535.
56      */
57     public static Uint16 fromJava(final int value) {
58         return Uint16.valueOf(value);
59     }
60
61     /**
62      * Convert a {@code long} in range 0-4294967295 to a Uint32.
63      *
64      * @param value value
65      * @return Uint32 object
66      * @throws IllegalArgumentException if value is less than zero or greater than 4294967295
67      */
68     public static Uint32 fromJava(final long value) {
69         return Uint32.valueOf(value);
70     }
71
72     /**
73      * Convert a {@link BigInteger} in range 0-18446744073709551615 to an Uint64.
74      *
75      * @param value value
76      * @return Uint64 object
77      * @throws NullPointerException if value is null
78      * @throws IllegalArgumentException if value is less than zero or greater than 18446744073709551615
79      */
80     public static Uint64 fromJava(final BigInteger value) {
81         return Uint64.valueOf(value);
82     }
83
84     /**
85      * Convert an {@link UnsignedInteger} to a Uint32.
86      *
87      * @param value value
88      * @return Uint32 object
89      * @throws NullPointerException if value is null
90      */
91     public static Uint32 fromGuava(final UnsignedInteger value) {
92         return Uint32.valueOf(value);
93     }
94
95     /**
96      * Convert an {@link UnsignedLong} to a Uint64.
97      *
98      * @param value value
99      * @return Uint64 object
100      * @throws NullPointerException if value is null
101      */
102     public static Uint64 fromGuava(final UnsignedLong value) {
103         return Uint64.valueOf(value);
104     }
105
106     static void checkNonNegative(final byte value, final String maxValue) {
107         if (value < 0) {
108             throwIAE(value, maxValue);
109         }
110     }
111
112     static void checkNonNegative(final short value, final String maxStr) {
113         if (value < 0) {
114             throwIAE(value, maxStr);
115         }
116     }
117
118     static void checkNonNegative(final int value, final String maxStr) {
119         if (value < 0) {
120             throwIAE(value, maxStr);
121         }
122     }
123
124     static void checkRange(final short value, final short max) {
125         if (value < 0 || value > max) {
126             throwIAE(value, max);
127         }
128     }
129
130     static void checkRange(final int value, final int max) {
131         if (value < 0 || value > max) {
132             throwIAE(value, max);
133         }
134     }
135
136     static void checkRange(final long value, final long max) {
137         if (value < 0 || value > max) {
138             throwIAE(value, max);
139         }
140     }
141
142     static void throwIAE(final String value, final long max) {
143         // "Invalid range: 65536, expected: [[0..65535]]."
144         throw new IllegalArgumentException("Invalid range: " + value + ", expected: [[0.." + max + "]].");
145     }
146
147     private static void throwIAE(final int value, final int max) {
148         // "Invalid range: 65536, expected: [[0..65535]]."
149         throw new IllegalArgumentException("Invalid range: " + value + ", expected: [[0.." + max + "]].");
150     }
151
152     private static void throwIAE(final long value, final long max) {
153         // "Invalid range: 65536, expected: [[0..65535]]."
154         throw new IllegalArgumentException("Invalid range: " + value + ", expected: [[0.." + max + "]].");
155     }
156
157     private static void throwIAE(final int value, final String max) {
158         // "Invalid range: 65536, expected: [[0..65535]]."
159         throw new IllegalArgumentException("Invalid range: " + value + ", expected: [[0.." + max + "]].");
160     }
161 }