Removed unsigned. (use kotlin for this)

This commit is contained in:
Robinson 2023-01-24 19:41:39 +01:00
parent 9967f5294d
commit 088fab5488
No known key found for this signature in database
GPG Key ID: 8E7DB78588BD6F5C
7 changed files with 0 additions and 1474 deletions

View File

@ -62,16 +62,6 @@ licensing {
author(Extras.vendor)
url(Extras.url)
extra("Byte Utils (UByte, UInteger, ULong, Unsigned, UNumber, UShort)", License.APACHE_2) {
url("https://github.com/jOOQ/jOOQ/tree/master/jOOQ/src/main/java/org/jooq/types")
copyright(2017)
author("Data Geekery GmbH (http://www.datageekery.com)")
author("Lukas Eder")
author("Ed Schaller")
author("Jens Nerche")
author("Ivan Sokolov")
}
extra("Kryo Serialization", License.BSD_3) {
copyright(2020)
author("Nathan Sweet")

View File

@ -1,326 +0,0 @@
/*
* Copyright 2021 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
* Copyright (c) 2011-2017, Data Geekery GmbH (http://www.datageekery.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.bytes;
import java.io.ObjectStreamException;
import java.math.BigInteger;
/**
* The <code>unsigned byte</code> type
*
* @author Lukas Eder
* @author Ed Schaller
* @author Jens Nerche
*/
public final class UByte extends UNumber implements Comparable<UByte> {
/**
* Generated UID
*/
private static final long serialVersionUID = -6821055240959745390L;
/**
* Cached values
*/
private static final UByte[] VALUES = mkValues();
/**
* A constant holding the minimum value an <code>unsigned byte</code> can
* have, 0.
*/
public static final short MIN_VALUE = 0x00;
/**
* A constant holding the maximum value an <code>unsigned byte</code> can
* have, 2<sup>8</sup>-1.
*/
public static final short MAX_VALUE = 0xff;
/**
* A constant holding the minimum value an <code>unsigned byte</code> can
* have as UByte, 0.
*/
public static final UByte MIN = valueOf(0x00);
/**
* A constant holding the maximum value an <code>unsigned byte</code> can
* have as UByte, 2<sup>8</sup>-1.
*/
public static final UByte MAX = valueOf(0xff);
/**
* The value modelling the content of this <code>unsigned byte</code>
*/
private final short value;
/**
* Generate a cached value for each byte value.
*
* @return Array of cached values for UByte.
*/
private static final UByte[] mkValues() {
UByte[] ret = new UByte[256];
for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++)
ret[i & MAX_VALUE] = new UByte((byte) i);
return ret;
}
/**
* Get an instance of an <code>unsigned byte</code>
*
* @throws NumberFormatException If <code>value</code> does not contain a
* parsable <code>unsigned byte</code>.
*/
public static UByte valueOf(String value) throws NumberFormatException {
return valueOfUnchecked(rangeCheck(Short.parseShort(value)));
}
/**
* Get an instance of an <code>unsigned byte</code> by masking it with
* <code>0xFF</code> i.e. <code>(byte) -1</code> becomes
* <code>(ubyte) 255</code>
*/
public static UByte valueOf(byte value) {
return valueOfUnchecked((short) (value & MAX_VALUE));
}
/**
* Get the value of a short without checking the value.
*/
private static UByte valueOfUnchecked(short value) throws NumberFormatException {
return VALUES[value & MAX_VALUE];
}
/**
* Get an instance of an <code>unsigned byte</code>
*
* @throws NumberFormatException If <code>value</code> is not in the range
* of an <code>unsigned byte</code>
*/
public static UByte valueOf(short value) throws NumberFormatException {
return valueOfUnchecked(rangeCheck(value));
}
/**
* Get an instance of an <code>unsigned byte</code>
*
* @throws NumberFormatException If <code>value</code> is not in the range
* of an <code>unsigned byte</code>
*/
public static UByte valueOf(int value) throws NumberFormatException {
return valueOfUnchecked(rangeCheck(value));
}
/**
* Get an instance of an <code>unsigned byte</code>
*
* @throws NumberFormatException If <code>value</code> is not in the range
* of an <code>unsigned byte</code>
*/
public static UByte valueOf(long value) throws NumberFormatException {
return valueOfUnchecked(rangeCheck(value));
}
/**
* Create an <code>unsigned byte</code>
*
* @throws NumberFormatException If <code>value</code> is not in the range
* of an <code>unsigned byte</code>
*/
private UByte(long value) throws NumberFormatException {
this.value = rangeCheck(value);
}
/**
* Create an <code>unsigned byte</code>
*
* @throws NumberFormatException If <code>value</code> is not in the range
* of an <code>unsigned byte</code>
*/
private UByte(int value) throws NumberFormatException {
this.value = rangeCheck(value);
}
/**
* Create an <code>unsigned byte</code>
*
* @throws NumberFormatException If <code>value</code> is not in the range
* of an <code>unsigned byte</code>
*/
private UByte(short value) throws NumberFormatException {
this.value = rangeCheck(value);
}
/**
* Create an <code>unsigned byte</code> by masking it with <code>0xFF</code>
* i.e. <code>(byte) -1</code> becomes <code>(ubyte) 255</code>
*/
private UByte(byte value) {
this.value = (short) (value & MAX_VALUE);
}
/**
* Create an <code>unsigned byte</code>
*
* @throws NumberFormatException If <code>value</code> does not contain a
* parsable <code>unsigned byte</code>.
*/
private UByte(String value) throws NumberFormatException {
this.value = rangeCheck(Short.parseShort(value));
}
/**
* Throw exception if value out of range (short version)
*
* @param value Value to check
* @return value if it is in range
* @throws NumberFormatException if value is out of range
*/
private static short rangeCheck(short value) throws NumberFormatException {
if (value < MIN_VALUE || value > MAX_VALUE)
throw new NumberFormatException("Value is out of range : " + value);
return value;
}
/**
* Throw exception if value out of range (int version)
*
* @param value Value to check
* @return value if it is in range
* @throws NumberFormatException if value is out of range
*/
private static short rangeCheck(int value) throws NumberFormatException {
if (value < MIN_VALUE || value > MAX_VALUE)
throw new NumberFormatException("Value is out of range : " + value);
return (short) value;
}
/**
* Throw exception if value out of range (long version)
*
* @param value Value to check
* @return value if it is in range
* @throws NumberFormatException if value is out of range
*/
private static short rangeCheck(long value) throws NumberFormatException {
if (value < MIN_VALUE || value > MAX_VALUE)
throw new NumberFormatException("Value is out of range : " + value);
return (short) value;
}
/**
* Replace version read through deserialization with cached version. Note
* that this does not use the {@link #valueOfUnchecked(short)} as we have no
* guarantee that the value from the stream is valid.
*
* @return cached instance of this object's value
* @throws ObjectStreamException
*/
private Object readResolve() throws ObjectStreamException {
return valueOf(value);
}
@Override
public int intValue() {
return value;
}
@Override
public long longValue() {
return value;
}
@Override
public float floatValue() {
return value;
}
@Override
public double doubleValue() {
return value;
}
@Override
public int hashCode() {
return Short.valueOf(value).hashCode();
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj instanceof UByte)
return value == ((UByte) obj).value;
return false;
}
@Override
public String toString() {
return Short.valueOf(value).toString();
}
@Override
public String toHexString() {
return Integer.toHexString(this.value);
}
@Override
public int compareTo(UByte o) {
return (value < o.value ? -1 : (value == o.value ? 0 : 1));
}
@Override
public BigInteger toBigInteger() {
return BigInteger.valueOf(value);
}
public UByte add(UByte val) throws NumberFormatException {
return valueOf(value + val.value);
}
public UByte add(int val) throws NumberFormatException {
return valueOf(value + val);
}
public UByte subtract(final UByte val) {
return valueOf(value - val.value);
}
public UByte subtract(final int val) {
return valueOf(value - val);
}
}

View File

@ -1,364 +0,0 @@
/*
* Copyright 2021 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
* Copyright (c) 2011-2017, Data Geekery GmbH (http://www.datageekery.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.bytes;
import java.io.ObjectStreamException;
import java.math.BigInteger;
/**
* The <code>unsigned int</code> type
*
* @author Lukas Eder
* @author Ed Schaller
* @author Jens Nerche
*/
public final class UInteger extends UNumber implements Comparable<UInteger> {
private static final Class<UInteger> CLASS = UInteger.class;
private static final String CLASS_NAME = CLASS.getName();
/**
* System property name for the property to set the size of the pre-cache.
*/
private static final String PRECACHE_PROPERTY = CLASS_NAME + ".precacheSize";
/**
* Default size for the value cache.
*/
private static final int DEFAULT_PRECACHE_SIZE = 256;
/**
* Generated UID
*/
private static final long serialVersionUID = -6821055240959745390L;
/**
* Cached values
*/
private static final UInteger[] VALUES = mkValues();
/**
* A constant holding the minimum value an <code>unsigned int</code> can
* have, 0.
*/
public static final long MIN_VALUE = 0x00000000;
/**
* A constant holding the maximum value an <code>unsigned int</code> can
* have, 2<sup>32</sup>-1.
*/
public static final long MAX_VALUE = 0xffffffffL;
/**
* A constant holding the minimum value an <code>unsigned int</code> can
* have as UInteger, 0.
*/
public static final UInteger MIN = valueOf(MIN_VALUE);
/**
* A constant holding the maximum value an <code>unsigned int</code> can
* have as UInteger, 2<sup>32</sup>-1.
*/
public static final UInteger MAX = valueOf(MAX_VALUE);
/**
* The value modelling the content of this <code>unsigned int</code>
*/
private final long value;
/**
* Figure out the size of the precache.
*
* @return The parsed value of the system property
* {@link #PRECACHE_PROPERTY} or {@link #DEFAULT_PRECACHE_SIZE} if
* the property is not set, not a number or retrieving results in a
* {@link SecurityException}. If the parsed value is zero or
* negative no cache will be created. If the value is larger than
* {@link Integer#MAX_VALUE} then Integer#MAX_VALUE will be used.
*/
private static final int getPrecacheSize() {
String prop = null;
long propParsed;
try {
prop = System.getProperty(PRECACHE_PROPERTY);
}
catch (SecurityException e) {
// security manager stopped us so use default
// FIXME: should we log this somewhere?
return DEFAULT_PRECACHE_SIZE;
}
if (prop == null)
return DEFAULT_PRECACHE_SIZE;
// empty value
// FIXME: should we log this somewhere?
if (prop.length() <= 0)
return DEFAULT_PRECACHE_SIZE;
try {
propParsed = Long.parseLong(prop);
}
catch (NumberFormatException e) {
// not a valid number
// FIXME: should we log this somewhere?
return DEFAULT_PRECACHE_SIZE;
}
// treat negative value as no cache...
if (propParsed < 0)
return 0;
// FIXME: should we log this somewhere?
if (propParsed > Integer.MAX_VALUE)
return Integer.MAX_VALUE;
return (int) propParsed;
}
/**
* Generate a cached value for initial unsigned integer values.
*
* @return Array of cached values for UInteger
*/
private static final UInteger[] mkValues() {
int precacheSize = getPrecacheSize();
UInteger[] ret;
if (precacheSize <= 0)
return null;
ret = new UInteger[precacheSize];
for (int i = 0; i < precacheSize; i++)
ret[i] = new UInteger(i);
return ret;
}
/**
* Unchecked internal constructor. This serves two purposes: first it allows
* {@link #UInteger(long)} to stay deprecated without warnings and second
* constructor without unnecessary value checks.
*
* @param value The value to wrap
* @param unused Unused parameter to distinguish between this and the
* deprecated public constructor.
*/
private UInteger(long value, boolean unused) {
this.value = value;
}
/**
* Retrieve a cached value.
*
* @param value Cached value to retrieve
* @return Cached value if one exists. Null otherwise.
*/
private static UInteger getCached(long value) {
if (VALUES != null && value < VALUES.length)
return VALUES[(int) value];
return null;
}
/**
* Get the value of a long without checking the value.
*/
private static UInteger valueOfUnchecked(long value) {
UInteger cached;
if ((cached = getCached(value)) != null)
return cached;
return new UInteger(value, true);
}
/**
* Create an <code>unsigned int</code>
*
* @throws NumberFormatException If <code>value</code> does not contain a
* parsable <code>unsigned int</code>.
*/
public static UInteger valueOf(String value) throws NumberFormatException {
return valueOfUnchecked(rangeCheck(Long.parseLong(value)));
}
/**
* Create an <code>unsigned int</code> by masking it with
* <code>0xFFFFFFFF</code> i.e. <code>(int) -1</code> becomes
* <code>(uint) 4294967295</code>
*/
public static UInteger valueOf(int value) {
return valueOfUnchecked(value & MAX_VALUE);
}
/**
* Create an <code>unsigned int</code>
*
* @throws NumberFormatException If <code>value</code> is not in the range
* of an <code>unsigned byte</code>
*/
public static UInteger valueOf(long value) throws NumberFormatException {
return valueOfUnchecked(rangeCheck(value));
}
/**
* Create an <code>unsigned int</code>
*
* @throws NumberFormatException If <code>value</code> is not in the range
* of an <code>unsigned int</code>
*/
private UInteger(long value) throws NumberFormatException {
this.value = rangeCheck(value);
}
/**
* Create an <code>unsigned int</code> by masking it with
* <code>0xFFFFFFFF</code> i.e. <code>(int) -1</code> becomes
* <code>(uint) 4294967295</code>
*/
private UInteger(int value) {
this.value = value & MAX_VALUE;
}
/**
* Create an <code>unsigned int</code>
*
* @throws NumberFormatException If <code>value</code> does not contain a
* parsable <code>unsigned int</code>.
*/
private UInteger(String value) throws NumberFormatException {
this.value = rangeCheck(Long.parseLong(value));
}
/**
* Throw exception if value out of range (long version)
*
* @param value Value to check
* @return value if it is in range
* @throws NumberFormatException if value is out of range
*/
private static long rangeCheck(long value) throws NumberFormatException {
if (value < MIN_VALUE || value > MAX_VALUE)
throw new NumberFormatException("Value is out of range : " + value);
return value;
}
/**
* Replace version read through deserialization with cached version.
*
* @return cached instance of this object's value if one exists, otherwise
* this object
* @throws ObjectStreamException
*/
private Object readResolve() throws ObjectStreamException {
UInteger cached;
// the value read could be invalid so check it
rangeCheck(value);
if ((cached = getCached(value)) != null)
return cached;
return this;
}
@Override
public int intValue() {
return (int) value;
}
@Override
public long longValue() {
return value;
}
@Override
public float floatValue() {
return value;
}
@Override
public double doubleValue() {
return value;
}
@Override
public BigInteger toBigInteger() {
return BigInteger.valueOf(value);
}
@Override
public int hashCode() {
return Long.valueOf(value).hashCode();
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj instanceof UInteger)
return value == ((UInteger) obj).value;
return false;
}
@Override
public String toString() {
return Long.valueOf(value).toString();
}
@Override
public String toHexString() {
return Long.toHexString(this.value);
}
@Override
public int compareTo(UInteger o) {
return (value < o.value ? -1 : (value == o.value ? 0 : 1));
}
public UInteger add(final UInteger val) {
return valueOf(value + val.value);
}
public UInteger add(final int val) {
return valueOf(value + val);
}
public UInteger subtract(final UInteger val) {
return valueOf(value - val.value);
}
public UInteger subtract(final int val) {
return valueOf(value - val);
}
}

View File

@ -1,288 +0,0 @@
/*
* Copyright 2021 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
* Copyright (c) 2011-2017, Data Geekery GmbH (http://www.datageekery.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.bytes;
import java.math.BigInteger;
/**
* The <code>unsigned long</code> type
*
* @author Lukas Eder
* @author Jens Nerche
* @author Ivan Sokolov
*/
public final class ULong extends UNumber implements Comparable<ULong> {
/**
* Generated UID
*/
private static final long serialVersionUID = -6821055240959745390L;
/**
* A constant holding the minimum value an <code>unsigned long</code> can
* have, 0.
*/
public static final BigInteger MIN_VALUE = BigInteger.ZERO;
/**
* A constant holding the maximum value an <code>unsigned long</code> can
* have, 2<sup>64</sup>-1.
*/
public static final BigInteger MAX_VALUE = new BigInteger("18446744073709551615");
/**
* A constant holding the maximum value + 1 an <code>signed long</code> can
* have, 2<sup>63</sup>.
*/
public static final BigInteger MAX_VALUE_LONG = new BigInteger("9223372036854775808");
/**
* A constant holding the minimum value an <code>unsigned long</code> can
* have as ULong, 0.
*/
public static final ULong MIN = valueOf(MIN_VALUE.longValue());
/**
* A constant holding the maximum value + 1 an <code>signed long</code> can
* have as ULong, 2<sup>63</sup>.
*/
public static final ULong MAX = valueOf(MAX_VALUE);
/**
* The value modelling the content of this <code>unsigned long</code>
*/
private final long value;
/**
* Create an <code>unsigned long</code>
*
* @throws NumberFormatException If <code>value</code> does not contain a
* parsable <code>unsigned long</code>.
*/
public static ULong valueOf(String value) throws NumberFormatException {
return new ULong(value);
}
/**
* Create an <code>unsigned long</code> by masking it with
* <code>0xFFFFFFFFFFFFFFFF</code> i.e. <code>(long) -1</code> becomes
* <code>(uint) 18446744073709551615</code>
*/
public static ULong valueOf(long value) {
return new ULong(value);
}
/**
* Create an <code>unsigned long</code>
*
* @throws NumberFormatException If <code>value</code> is not in the range
* of an <code>unsigned long</code>
*/
public static ULong valueOf(BigInteger value) throws NumberFormatException {
return new ULong(value);
}
public static int compare(long x, long y) {
x += Long.MIN_VALUE;
y += Long.MIN_VALUE;
return (x < y) ? -1 : ((x == y) ? 0 : 1);
}
/**
* Create an <code>unsigned long</code>
*
* @throws NumberFormatException If <code>value</code> is not in the range
* of an <code>unsigned long</code>
*/
private ULong(BigInteger value) throws NumberFormatException {
if (value.compareTo(MIN_VALUE) < 0 || value.compareTo(MAX_VALUE) > 0)
throw new NumberFormatException();
else
this.value = value.longValue();
}
/**
* Create an <code>unsigned long</code> by masking it with
* <code>0xFFFFFFFFFFFFFFFF</code> i.e. <code>(long) -1</code> becomes
* <code>(uint) 18446744073709551615</code>
*/
private ULong(long value) {
this.value = value;
}
/**
* Create an <code>unsigned long</code>
*
* @throws NumberFormatException If <code>value</code> does not contain a
* parsable <code>unsigned long</code>.
*/
private ULong(String value) throws NumberFormatException {
if (value == null)
throw new NumberFormatException("null");
int length = value.length();
if (length == 0)
throw new NumberFormatException("Empty input string");
if (value.charAt(0) == '-')
throw new NumberFormatException(
String.format("Illegal leading minus sign on unsigned string %s", value));
if (length <= 18) {
this.value = Long.parseLong(value, 10);
return;
}
final long first = Long.parseLong(value.substring(0, length - 1), 10);
final int second = Character.digit(value.charAt(length - 1), 10);
if (second < 0)
throw new NumberFormatException("Bad digit at end of " + value);
long result = first * 10 + second;
if (compare(result, first) < 0)
throw new NumberFormatException(
String.format("String value %s exceeds range of unsigned long", value));
this.value = result;
}
@Override
public int intValue() {
return (int) value;
}
@Override
public long longValue() {
return value;
}
@Override
public float floatValue() {
if (value < 0)
return ((float) (value & Long.MAX_VALUE)) + Long.MAX_VALUE;
else
return value;
}
@Override
public double doubleValue() {
if (value < 0)
return ((double) (value & Long.MAX_VALUE)) + Long.MAX_VALUE;
else
return value;
}
@Override
public int hashCode() {
return Long.valueOf(value).hashCode();
}
@Override
public boolean equals(Object obj) {
if (obj instanceof ULong)
return value == ((ULong) obj).value;
return false;
}
@Override
public String toString() {
if (value >= 0)
return Long.toString(value);
else
return BigInteger.valueOf(value & Long.MAX_VALUE).add(MAX_VALUE_LONG).toString();
}
@Override
public String toHexString() {
return Long.toHexString(this.value);
}
@Override
public int compareTo(ULong o) {
return compare(value, o.value);
}
public ULong add(ULong val) throws NumberFormatException {
if (value < 0 && val.value < 0)
throw new NumberFormatException();
final long result = value + val.value;
if ((value < 0 || val.value < 0) && result >= 0)
throw new NumberFormatException();
return valueOf(result);
}
public ULong add(int val) throws NumberFormatException {
return add((long) val);
}
public ULong add(long val) throws NumberFormatException {
if (val < 0)
return subtract(Math.abs(val));
final long result = value + val;
if (value < 0 && result >= 0)
throw new NumberFormatException();
return valueOf(result);
}
public ULong subtract(final ULong val) {
if (this.compareTo(val) < 0)
throw new NumberFormatException();
final long result = value - val.value;
if (value < 0 && result >= 0)
throw new NumberFormatException();
return valueOf(result);
}
public ULong subtract(final int val) {
return subtract((long) val);
}
public ULong subtract(final long val) {
if (val < 0)
return add(-val);
if (compare(value, val) < 0)
throw new NumberFormatException();
final long result = value - val;
if (value < 0 && result >= 0)
throw new NumberFormatException();
return valueOf(result);
}
}

View File

@ -1,59 +0,0 @@
/*
* Copyright 2021 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
* Copyright (c) 2011-2017, Data Geekery GmbH (http://www.datageekery.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.bytes;
import java.math.BigInteger;
/**
* A base type for unsigned numbers.
*
* @author Lukas Eder
*/
public abstract class UNumber extends Number {
/**
* Generated UID
*/
private static final long serialVersionUID = -7666221938815339843L;
/**
* Get this number as a {@link BigInteger}. This is a convenience method for
* calling <code>new BigInteger(toString())</code>
*/
public BigInteger toBigInteger() {
return new BigInteger(toString());
}
/**
* Converts this number to a hex string representation
*/
public abstract String toHexString();
}

View File

@ -1,211 +0,0 @@
/*
* Copyright 2021 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
* Copyright (c) 2011-2017, Data Geekery GmbH (http://www.datageekery.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.bytes;
import java.math.BigInteger;
/**
* The <code>unsigned short</code> type
*
* @author Lukas Eder
* @author Jens Nerche
*/
@SuppressWarnings("unused")
public final class UShort extends dorkbox.bytes.UNumber implements Comparable<UShort> {
/**
* Generated UID
*/
private static final long serialVersionUID = -6821055240959745390L;
/**
* A constant holding the minimum value an <code>unsigned short</code> can
* have, 0.
*/
public static final int MIN_VALUE = 0x0000;
/**
* A constant holding the maximum value an <code>unsigned short</code> can
* have, 2<sup>16</sup>-1.
*/
public static final int MAX_VALUE = 0xffff;
/**
* A constant holding the minimum value an <code>unsigned short</code> can
* have as UShort, 0.
*/
public static final UShort MIN = valueOf(MIN_VALUE);
/**
* A constant holding the maximum value an <code>unsigned short</code> can
* have as UShort, 2<sup>16</sup>-1.
*/
public static final UShort MAX = valueOf(MAX_VALUE);
/**
* The value modelling the content of this <code>unsigned short</code>
*/
private final int value;
/**
* Create an <code>unsigned short</code>
*
* @throws NumberFormatException If <code>value</code> does not contain a
* parsable <code>unsigned short</code>.
*/
public static UShort valueOf(String value) throws NumberFormatException {
return new UShort(value);
}
/**
* Create an <code>unsigned short</code> by masking it with
* <code>0xFFFF</code> i.e. <code>(short) -1</code> becomes
* <code>(ushort) 65535</code>
*/
public static UShort valueOf(short value) {
return new UShort(value);
}
/**
* Create an <code>unsigned short</code>
*
* @throws NumberFormatException If <code>value</code> is not in the range
* of an <code>unsigned short</code>
*/
public static UShort valueOf(int value) throws NumberFormatException {
return new UShort(value);
}
/**
* Create an <code>unsigned short</code>
*
* @throws NumberFormatException If <code>value</code> is not in the range
* of an <code>unsigned short</code>
*/
private UShort(int value) throws NumberFormatException {
this.value = value;
rangeCheck();
}
/**
* Create an <code>unsigned short</code> by masking it with
* <code>0xFFFF</code> i.e. <code>(short) -1</code> becomes
* <code>(ushort) 65535</code>
*/
private UShort(short value) {
this.value = value & MAX_VALUE;
}
/**
* Create an <code>unsigned short</code>
*
* @throws NumberFormatException If <code>value</code> does not contain a
* parsable <code>unsigned short</code>.
*/
private UShort(String value) throws NumberFormatException {
this.value = Integer.parseInt(value);
rangeCheck();
}
private void rangeCheck() throws NumberFormatException {
if (value < MIN_VALUE || value > MAX_VALUE)
throw new NumberFormatException("Value is out of range : " + value);
}
@Override
public int intValue() {
return value;
}
@Override
public long longValue() {
return value;
}
@Override
public float floatValue() {
return value;
}
@Override
public double doubleValue() {
return value;
}
@Override
public BigInteger toBigInteger() {
return BigInteger.valueOf(value);
}
@Override
public int hashCode() {
return Integer.valueOf(value).hashCode();
}
@Override
public boolean equals(Object obj) {
if (obj instanceof UShort)
return value == ((UShort) obj).value;
return false;
}
@Override
public String toString() {
return Integer.valueOf(value).toString();
}
@Override
public String toHexString() {
return Integer.toHexString(this.value);
}
@Override
public int compareTo(UShort o) {
return (Integer.compare(value, o.value));
}
public UShort add(UShort val) throws NumberFormatException {
return valueOf(value + val.value);
}
public UShort add(int val) throws NumberFormatException {
return valueOf(value + val);
}
public UShort subtract(final UShort val) {
return valueOf(value - val.value);
}
public UShort subtract(final int val) {
return valueOf(value - val);
}
}

View File

@ -1,216 +0,0 @@
/*
* Copyright 2021 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
* Copyright (c) 2011-2017, Data Geekery GmbH (http://www.datageekery.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.bytes;
import java.math.BigInteger;
/**
* A utility class for static access to unsigned number functionality.
* <p>
* It essentially contains factory methods for unsigned number wrappers. In
* future versions, it will also contain some arithmetic methods, handling
* regular arithmetic and bitwise operations
*
* @author Lukas Eder
*/
@SuppressWarnings("unused")
public final class Unsigned {
/**
* Create an <code>unsigned byte</code>
*
* @throws NumberFormatException If <code>value</code> does not contain a
* parsable <code>unsigned byte</code>.
* @see UByte#valueOf(String)
*/
public static
dorkbox.bytes.UByte ubyte(String value) throws NumberFormatException {
return value == null ? null : dorkbox.bytes.UByte.valueOf(value);
}
/**
* Create an <code>unsigned byte</code> by masking it with <code>0xFF</code>
* i.e. <code>(byte) -1</code> becomes <code>(ubyte) 255</code>
*
* @see UByte#valueOf(byte)
*/
public static
dorkbox.bytes.UByte ubyte(byte value) {
return dorkbox.bytes.UByte.valueOf(value);
}
/**
* Create an <code>unsigned byte</code>
*
* @throws NumberFormatException If <code>value</code> is not in the range
* of an <code>unsigned byte</code>
* @see UByte#valueOf(short)
*/
public static
dorkbox.bytes.UByte ubyte(short value) throws NumberFormatException {
return dorkbox.bytes.UByte.valueOf(value);
}
/**
* Create an <code>unsigned byte</code>
*
* @throws NumberFormatException If <code>value</code> is not in the range
* of an <code>unsigned byte</code>
* @see UByte#valueOf(short)
*/
public static
dorkbox.bytes.UByte ubyte(int value) throws NumberFormatException {
return dorkbox.bytes.UByte.valueOf(value);
}
/**
* Create an <code>unsigned byte</code>
*
* @throws NumberFormatException If <code>value</code> is not in the range
* of an <code>unsigned byte</code>
* @see UByte#valueOf(short)
*/
public static
dorkbox.bytes.UByte ubyte(long value) throws NumberFormatException {
return dorkbox.bytes.UByte.valueOf(value);
}
/**
* Create an <code>unsigned short</code>
*
* @throws NumberFormatException If <code>value</code> does not contain a
* parsable <code>unsigned short</code>.
* @see UShort#valueOf(String)
*/
public static
UShort ushort(String value) throws NumberFormatException {
return value == null ? null : UShort.valueOf(value);
}
/**
* Create an <code>unsigned short</code> by masking it with
* <code>0xFFFF</code> i.e. <code>(short) -1</code> becomes
* <code>(ushort) 65535</code>
*
* @see UShort#valueOf(short)
*/
public static UShort ushort(short value) {
return UShort.valueOf(value);
}
/**
* Create an <code>unsigned short</code>
*
* @throws NumberFormatException If <code>value</code> is not in the range
* of an <code>unsigned short</code>
* @see UShort#valueOf(int)
*/
public static UShort ushort(int value) throws NumberFormatException {
return UShort.valueOf(value);
}
/**
* Create an <code>unsigned int</code>
*
* @throws NumberFormatException If <code>value</code> does not contain a
* parsable <code>unsigned int</code>.
* @see UInteger#valueOf(String)
*/
public static
dorkbox.bytes.UInteger uint(String value) throws NumberFormatException {
return value == null ? null : dorkbox.bytes.UInteger.valueOf(value);
}
/**
* Create an <code>unsigned int</code> by masking it with
* <code>0xFFFFFFFF</code> i.e. <code>(int) -1</code> becomes
* <code>(uint) 4294967295</code>
*
* @see UInteger#valueOf(int)
*/
public static
dorkbox.bytes.UInteger uint(int value) {
return dorkbox.bytes.UInteger.valueOf(value);
}
/**
* Create an <code>unsigned int</code>
*
* @throws NumberFormatException If <code>value</code> is not in the range
* of an <code>unsigned int</code>
* @see UInteger#valueOf(long)
*/
public static
dorkbox.bytes.UInteger uint(long value) throws NumberFormatException {
return dorkbox.bytes.UInteger.valueOf(value);
}
/**
* Create an <code>unsigned long</code>
*
* @throws NumberFormatException If <code>value</code> does not contain a
* parsable <code>unsigned long</code>.
* @see ULong#valueOf(String)
*/
public static
dorkbox.bytes.ULong ulong(String value) throws NumberFormatException {
return value == null ? null : dorkbox.bytes.ULong.valueOf(value);
}
/**
* Create an <code>unsigned long</code> by masking it with
* <code>0xFFFFFFFFFFFFFFFF</code> i.e. <code>(long) -1</code> becomes
* <code>(uint) 18446744073709551615</code>
*
* @see ULong#valueOf(long)
*/
public static
dorkbox.bytes.ULong ulong(long value) {
return dorkbox.bytes.ULong.valueOf(value);
}
/**
* Create an <code>unsigned long</code>
*
* @throws NumberFormatException If <code>value</code> is not in the range
* of an <code>unsigned long</code>
* @see ULong#valueOf(BigInteger)
*/
public static
dorkbox.bytes.ULong ulong(BigInteger value) throws NumberFormatException {
return dorkbox.bytes.ULong.valueOf(value);
}
/**
* No instances
*/
private Unsigned() {}
}