return valueOf(Short.parseShort(requireNonNull(string), radix));
}
+ /**
+ * Returns an {@code Uint8} corresponding to a given {@code byteVal} if it is representable. If the value is
+ * negative {@link #ZERO} will be returned.
+ *
+ * @param byteVal byte value
+ * @return A Uint8 instance
+ */
+ public static Uint8 saturatedOf(final byte byteVal) {
+ return byteVal <= 0 ? Uint8.ZERO : instanceFor(byteVal);
+ }
+
+ /**
+ * Returns an {@code Uint8} corresponding to a given {@code shortVal} if it is representable. If the value is
+ * negative {@link #ZERO} will be returned. If the value is greater than 255, {@link #MAX_VALUE} will be returned.
+ *
+ * @param shortVal short value
+ * @return A Uint8 instance
+ */
+ public static Uint8 saturatedOf(final short shortVal) {
+ if (shortVal <= 0) {
+ return Uint8.ZERO;
+ }
+ if (shortVal >= MAX_VALUE_SHORT) {
+ return Uint8.MAX_VALUE;
+ }
+ return instanceFor((byte) shortVal);
+ }
+
+ /**
+ * Returns an {@code Uint8} corresponding to a given {@code intVal} if it is representable. If the value is
+ * negative {@link #ZERO} will be returned. If the value is greater than 255, {@link #MAX_VALUE} will be returned.
+ *
+ * @param intVal int value
+ * @return A Uint8 instance
+ */
+ public static Uint8 saturatedOf(final int intVal) {
+ if (intVal <= 0) {
+ return Uint8.ZERO;
+ }
+ if (intVal >= MAX_VALUE_SHORT) {
+ return Uint8.MAX_VALUE;
+ }
+ return instanceFor((byte) intVal);
+ }
+
+ /**
+ * Returns an {@code Uint8} corresponding to a given {@code longVal} if it is representable. If the value is
+ * negative {@link #ZERO} will be returned. If the value is greater than 255, {@link #MAX_VALUE} will be returned.
+ *
+ * @param longVal long value
+ * @return A Uint8 instance
+ */
+ public static Uint8 saturatedOf(final long longVal) {
+ if (longVal <= 0) {
+ return Uint8.ZERO;
+ }
+ if (longVal >= MAX_VALUE_SHORT) {
+ return Uint8.MAX_VALUE;
+ }
+ return instanceFor((byte) longVal);
+ }
+
/**
* {@inheritDoc}
*
return shortValue();
}
+ /**
+ * Convert this value to a {@code Uint16}.
+ *
+ * @return A Uint16
+ */
+ public final Uint16 toUint16() {
+ return Uint16.fromShortBits(shortValue());
+ }
+
+ /**
+ * Convert this value to a {@code Uint32}.
+ *
+ * @return A Uint32
+ */
+ public final Uint32 toUint32() {
+ return Uint32.fromIntBits(intValue());
+ }
+
+ /**
+ * Convert this value to a {@code Uint64}.
+ *
+ * @return A Uint64
+ */
+ public final Uint64 toUint64() {
+ return Uint64.fromLongBits(longValue());
+ }
+
@Override
public final int hashCode() {
return Byte.hashCode(value);