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