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.primitives.UnsignedInteger;
11 import com.google.common.primitives.UnsignedLong;
12 import java.math.BigInteger;
13 import org.eclipse.jdt.annotation.NonNullByDefault;
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:
22 * import static org.opendaylight.yangtools.yang.common.UintConversions.fromJava;
24 * Uint16 two = fromJava(32);
25 * Uint32 one = fromJava(32L);
30 public final class UintConversions {
31 private UintConversions() {
36 * Convert a {@code short} in range 0-255 to an Uint8.
39 * @return Uint8 object
40 * @throws IllegalArgumentException if value is less than zero or greater than 255
42 public static Uint8 fromJava(final short value) {
43 return Uint8.valueOf(value);
47 * Convert an {@code int} in range 0-65535 to a Uint16.
50 * @return Uint16 object
51 * @throws IllegalArgumentException if value is less than zero or greater than 65535.
53 public static Uint16 fromJava(final int value) {
54 return Uint16.valueOf(value);
58 * Convert a {@code long} in range 0-4294967295 to a Uint32.
61 * @return Uint32 object
62 * @throws IllegalArgumentException if value is less than zero or greater than 4294967295
64 public static Uint32 fromJava(final long value) {
65 return Uint32.valueOf(value);
69 * Convert a {@link BigInteger} in range 0-18446744073709551615 to an Uint64.
72 * @return Uint64 object
73 * @throws NullPointerException if value is null
74 * @throws IllegalArgumentException if value is less than zero or greater than 18446744073709551615
76 public static Uint64 fromJava(final BigInteger value) {
77 return Uint64.valueOf(value);
81 * Convert an {@link UnsignedInteger} to a Uint32.
84 * @return Uint32 object
85 * @throws NullPointerException if value is null
87 public static Uint32 fromGuava(final UnsignedInteger value) {
88 return Uint32.valueOf(value);
92 * Convert an {@link UnsignedLong} to a Uint64.
95 * @return Uint64 object
96 * @throws NullPointerException if value is null
98 public static Uint64 fromGuava(final UnsignedLong value) {
99 return Uint64.valueOf(value);
102 static void checkNonNegative(final byte value, final String maxValue) {
104 throwIAE(value, maxValue);
108 static void checkNonNegative(final short value, final String maxStr) {
110 throwIAE(value, maxStr);
114 static void checkNonNegative(final int value, final String maxStr) {
116 throwIAE(value, maxStr);
120 static void checkRange(final short value, final short max) {
121 if (value < 0 || value > max) {
122 throwIAE(value, max);
126 static void checkRange(final int value, final int max) {
127 if (value < 0 || value > max) {
128 throwIAE(value, max);
132 static void checkRange(final long value, final long max) {
133 if (value < 0 || value > max) {
134 throwIAE(value, max);
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 + "]].");
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 + "]].");