2 * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.common;
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;
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:
23 * import static org.opendaylight.yangtools.yang.common.UintConversions.fromJava;
25 * Uint16 two = fromJava(32);
26 * Uint32 one = fromJava(32L);
30 * @author Robert Varga
34 public final class UintConversions {
35 private UintConversions() {
40 * Convert a {@code short} in range 0-255 to an Uint8.
43 * @return Uint8 object
44 * @throws IllegalArgumentException if value is less than zero or greater than 255
46 public static Uint8 fromJava(final short value) {
47 return Uint8.valueOf(value);
51 * Convert an {@code int} in range 0-65535 to a Uint16.
54 * @return Uint16 object
55 * @throws IllegalArgumentException if value is less than zero or greater than 65535.
57 public static Uint16 fromJava(final int value) {
58 return Uint16.valueOf(value);
62 * Convert a {@code long} in range 0-4294967295 to a Uint32.
65 * @return Uint32 object
66 * @throws IllegalArgumentException if value is less than zero or greater than 4294967295
68 public static Uint32 fromJava(final long value) {
69 return Uint32.valueOf(value);
73 * Convert a {@link BigInteger} in range 0-18446744073709551615 to an Uint64.
76 * @return Uint64 object
77 * @throws NullPointerException if value is null
78 * @throws IllegalArgumentException if value is less than zero or greater than 18446744073709551615
80 public static Uint64 fromJava(final BigInteger value) {
81 return Uint64.valueOf(value);
85 * Convert an {@link UnsignedInteger} to a Uint32.
88 * @return Uint32 object
89 * @throws NullPointerException if value is null
91 public static Uint32 fromGuava(final UnsignedInteger value) {
92 return Uint32.valueOf(value);
96 * Convert an {@link UnsignedLong} to a Uint64.
99 * @return Uint64 object
100 * @throws NullPointerException if value is null
102 public static Uint64 fromGuava(final UnsignedLong value) {
103 return Uint64.valueOf(value);
106 static void checkNonNegative(final byte value, final String maxValue) {
108 throwIAE(value, maxValue);
112 static void checkNonNegative(final short value, final String maxStr) {
114 throwIAE(value, maxStr);
118 static void checkNonNegative(final int value, final String maxStr) {
120 throwIAE(value, maxStr);
124 static void checkRange(final short value, final short max) {
125 if (value < 0 || value > max) {
126 throwIAE(value, max);
130 static void checkRange(final int value, final int max) {
131 if (value < 0 || value > max) {
132 throwIAE(value, max);
136 static void checkRange(final long value, final long max) {
137 if (value < 0 || value > max) {
138 throwIAE(value, max);
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 + "]].");
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 + "]].");
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 + "]].");
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 + "]].");