return instanceFor(Integer.parseUnsignedInt(requireNonNull(string), radix));
}
+ /**
+ * Returns an {@code Uint32} 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 Uint32 instance
+ */
+ public static Uint32 saturatedOf(final byte byteVal) {
+ return byteVal < 0 ? Uint32.ZERO : instanceFor(byteVal);
+ }
+
+ /**
+ * Returns an {@code Uint32} corresponding to a given {@code shortVal} if it is representable. If the value is
+ * negative {@link #ZERO} will be returned.
+ *
+ * @param shortVal short value
+ * @return A Uint32 instance
+ */
+ public static Uint32 saturatedOf(final short shortVal) {
+ return shortVal < 0 ? Uint32.ZERO : instanceFor(shortVal);
+ }
+
+ /**
+ * Returns an {@code Uint32} corresponding to a given {@code intVal} if it is representable. If the value is
+ * negative {@link #ZERO} will be returned.
+ *
+ * @param intVal int value
+ * @return A Uint32 instance
+ */
+ public static Uint32 saturatedOf(final int intVal) {
+ return intVal < 0 ? Uint32.ZERO : instanceFor(intVal);
+ }
+
+ /**
+ * Returns an {@code Uint32} 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 4294967295, {@link #MAX_VALUE} will be
+ * returned.
+ *
+ * @param longVal long value
+ * @return A Uint32 instance
+ */
+ public static Uint32 saturatedOf(final long longVal) {
+ if (longVal < 0) {
+ return Uint32.ZERO;
+ }
+ if (longVal >= MAX_VALUE_LONG) {
+ return Uint32.MAX_VALUE;
+ }
+ return instanceFor((int) longVal);
+ }
+
/**
* {@inheritDoc}
*
return Uint64.fromLongBits(longValue());
}
+ public final Uint8 toSaturatedUint8() {
+ return Uint8.saturatedOf(toJava());
+ }
+
+ public final Uint16 toSaturatedUint16() {
+ return Uint16.saturatedOf(toJava());
+ }
+
+ // FIXME: more saturated conversions
+
@Override
public final int hashCode() {
return Integer.hashCode(value);