return intValue() < CACHE_SIZE ? this : INTERNER.intern(this);
}
+ /**
+ * Convert this value to an {@code int}.
+ *
+ * @return An int
+ */
+ public final int toJava() {
+ return intValue();
+ }
+
@Override
public final int hashCode() {
return Short.hashCode(value);
*
* @param longVal long value
* @return A Uint8 instance
- * @throws IllegalArgumentException if intVal is less than zero or greater than 4294967295
+ * @throws IllegalArgumentException if longVal is less than zero or greater than 4294967295
*/
public static Uint32 valueOf(final long longVal) {
checkArgument(longVal >= MIN_VALUE_LONG && longVal <= MAX_VALUE_LONG, "Value %s is outside of allowed range",
return longValue();
}
- /**
- * Convert this value to an {@link UnsignedInteger}.
- *
- * @return An UnsignedInteger instance
- */
- public final UnsignedInteger toUnsignedInteger() {
- return UnsignedInteger.fromIntBits(value);
- }
-
@Override
@SuppressWarnings("checkstyle:parameterName")
public final int compareTo(final Uint32 o) {
return value >= 0 && value < CACHE_SIZE ? this : INTERNER.intern(this);
}
+ /**
+ * Convert this value to a {@code long}.
+ *
+ * @return A long
+ */
+ public final long toJava() {
+ return longValue();
+ }
+
+ /**
+ * Convert this value to an {@link UnsignedInteger}.
+ *
+ * @return An UnsignedInteger instance
+ */
+ public final UnsignedInteger toGuava() {
+ return UnsignedInteger.fromIntBits(value);
+ }
+
@Override
public final int hashCode() {
return Integer.hashCode(value);
*
* @param longVal long value
* @return A Uint8 instance
- * @throws IllegalArgumentException if intVal is less than zero
+ * @throws IllegalArgumentException if longVal is less than zero
*/
public static Uint64 valueOf(final long longVal) {
checkArgument(longVal >= MIN_VALUE_LONG, "Negative values are not allowed");
*
* @param ulong UnsignedLong value
* @return A Uint64 instance
- * @throws NullPointerException if uint is null
+ * @throws NullPointerException if ulong is null
*/
public static Uint64 valueOf(final UnsignedLong ulong) {
return instanceFor(ulong.longValue());
*
* @param bigInt BigInteger value
* @return A Uint64 instance
- * @throws NullPointerException if uint is null
+ * @throws NullPointerException if bigInt is null
* @throws IllegalArgumentException if bigInt is less than zero or greater than 18446744073709551615
*/
public static Uint64 valueOf(final BigInteger bigInt) {
return UnsignedLong.fromLongBits(value).doubleValue();
}
- /**
- * Convert this value to an {@link UnsignedLong}.
- *
- * @return An UnsignedLong instance
- */
- public final UnsignedLong toUnsignedLong() {
- return UnsignedLong.fromLongBits(value);
- }
-
@Override
@SuppressWarnings("checkstyle:parameterName")
public final int compareTo(final Uint64 o) {
return value >= 0 && value < CACHE_SIZE ? this : INTERNER.intern(this);
}
+ /**
+ * Convert this value to a {@link BigInteger}.
+ *
+ * @return A BigInteger instance
+ */
+ public final BigInteger toJava() {
+ // FIXME: ditch the Guava transition
+ return toGuava().bigIntegerValue();
+ }
+
+ /**
+ * Convert this value to an {@link UnsignedLong}.
+ *
+ * @return An UnsignedLong instance
+ */
+ public final UnsignedLong toGuava() {
+ return UnsignedLong.fromLongBits(value);
+ }
@Override
public final int hashCode() {
return SUPPORT;
}
+ /**
+ * Convert this value to a {@code short}.
+ *
+ * @return A short
+ */
+ public final short toJava() {
+ return shortValue();
+ }
+
@Override
public final int hashCode() {
return Byte.hashCode(value);
--- /dev/null
+/*
+ * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.common;
+
+import com.google.common.annotations.Beta;
+import com.google.common.primitives.UnsignedInteger;
+import com.google.common.primitives.UnsignedLong;
+import java.math.BigInteger;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+
+/**
+ * Utility methods for converting Java and Guava integer types to their {@link Uint8}, {@link Uint16}, {@link Uint32}
+ * and {@link Uint64} equivalents. While individual types provide these through their {@code valueOf()} methods, this
+ * class allows dealing with multiple types through a static import:
+ *
+ * <pre>
+ * <code>
+ * import static org.opendaylight.yangtools.yang.common.UintConversions.fromJava;
+ *
+ * Uint16 two = fromJava(32);
+ * Uint32 one = fromJava(32L);
+ * </code>
+ * </pre>
+ *
+ * @author Robert Varga
+ */
+@Beta
+@NonNullByDefault
+public final class UintConversions {
+ private UintConversions() {
+ // Hidden on purpose
+ }
+
+ /**
+ * Convert a {@code short} in range 0-255 to an Uint8.
+ *
+ * @param value value
+ * @return Uint8 object
+ * @throws IllegalArgumentException if value is less than zero or greater than 255
+ */
+ public static Uint8 fromJava(final short value) {
+ return Uint8.valueOf(value);
+ }
+
+ /**
+ * Convert an {@code int} in range 0-65535 to a Uint16.
+ *
+ * @param value value
+ * @return Uint16 object
+ * @throws IllegalArgumentException if value is less than zero or greater than 65535.
+ */
+ public static Uint16 fromJava(final int value) {
+ return Uint16.valueOf(value);
+ }
+
+ /**
+ * Convert a {@code long} in range 0-4294967295 to a Uint32.
+ *
+ * @param value value
+ * @return Uint32 object
+ * @throws IllegalArgumentException if value is less than zero or greater than 4294967295
+ */
+ public static Uint32 fromJava(final long value) {
+ return Uint32.valueOf(value);
+ }
+
+ /**
+ * Convert a {@link BigInteger} in range 0-18446744073709551615 to an Uint64.
+ *
+ * @param value value
+ * @return Uint64 object
+ * @throws NullPointerException if value is null
+ * @throws IllegalArgumentException if value is less than zero or greater than 18446744073709551615
+ */
+ public static Uint64 fromJava(final BigInteger value) {
+ return Uint64.valueOf(value);
+ }
+
+ /**
+ * Convert an {@link UnsignedInteger} to a Uint32.
+ *
+ * @param value value
+ * @return Uint32 object
+ * @throws NullPointerException if value is null
+ */
+ public static Uint32 fromGuava(final UnsignedInteger value) {
+ return Uint32.valueOf(value);
+ }
+
+ /**
+ * Convert an {@link UnsignedLong} to a Uint64.
+ *
+ * @param value value
+ * @return Uint64 object
+ * @throws NullPointerException if value is null
+ */
+ public static Uint64 fromGuava(final UnsignedLong value) {
+ return Uint64.valueOf(value);
+ }
+}
assertSame(Uint32.valueOf(20), Uint32.valueOf(Uint64.valueOf(20)));
assertSame(Uint32.valueOf(5), Uint32.valueOf(UnsignedInteger.fromIntBits(5)));
- assertEquals(UnsignedInteger.fromIntBits(5), Uint32.valueOf(5).toUnsignedInteger());
+ assertEquals(UnsignedInteger.fromIntBits(5), Uint32.valueOf(5).toGuava());
}
@Test
assertEquals(Uint64.valueOf(30), Uint64.valueOf(new BigInteger("30")));
assertSame(Uint64.valueOf(5), Uint64.valueOf(UnsignedLong.fromLongBits(5)));
- assertEquals(UnsignedLong.fromLongBits(5), Uint64.valueOf(5).toUnsignedLong());
+ assertEquals(UnsignedLong.fromLongBits(5), Uint64.valueOf(5).toGuava());
}
@Test