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);