Updated Byte Collections to latest. Update copyright

This commit is contained in:
nathan 2019-03-25 15:49:14 +01:00
parent be03d84ae0
commit 1a585e1164
6 changed files with 362 additions and 316 deletions

View File

@ -1,37 +1,17 @@
/*
* Copyright (c) 2011-2013, Lukas Eder, lukas.eder@gmail.com
* All rights reserved.
* Copyright (c) 2011-2017, Data Geekery GmbH (http://www.datageekery.com)
*
* This software is licensed to you under the Apache License, Version 2.0
* (the "License"); You may obtain a copy of the License at
* 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
* http://www.apache.org/licenses/LICENSE-2.0
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* . Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* . Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* . Neither the name "jOOU" nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* 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.util.bytes;
@ -43,6 +23,7 @@ import java.math.BigInteger;
*
* @author Lukas Eder
* @author Ed Schaller
* @author Jens Nerche
*/
public final class UByte extends UNumber implements Comparable<UByte> {
@ -68,6 +49,18 @@ public final class UByte extends UNumber implements Comparable<UByte> {
*/
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>
*/
@ -81,9 +74,9 @@ public final class UByte extends UNumber implements Comparable<UByte> {
private static final UByte[] mkValues() {
UByte[] ret = new UByte[256];
for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) {
for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++)
ret[i & MAX_VALUE] = new UByte((byte) i);
}
return ret;
}
@ -199,9 +192,9 @@ public final class UByte extends UNumber implements Comparable<UByte> {
* @throws NumberFormatException if value is out of range
*/
private static short rangeCheck(short value) throws NumberFormatException {
if (value < MIN_VALUE || value > MAX_VALUE) {
if (value < MIN_VALUE || value > MAX_VALUE)
throw new NumberFormatException("Value is out of range : " + value);
}
return value;
}
@ -213,9 +206,9 @@ public final class UByte extends UNumber implements Comparable<UByte> {
* @throws NumberFormatException if value is out of range
*/
private static short rangeCheck(int value) throws NumberFormatException {
if (value < MIN_VALUE || value > MAX_VALUE) {
if (value < MIN_VALUE || value > MAX_VALUE)
throw new NumberFormatException("Value is out of range : " + value);
}
return (short) value;
}
@ -227,9 +220,9 @@ public final class UByte extends UNumber implements Comparable<UByte> {
* @throws NumberFormatException if value is out of range
*/
private static short rangeCheck(long value) throws NumberFormatException {
if (value < MIN_VALUE || value > MAX_VALUE) {
if (value < MIN_VALUE || value > MAX_VALUE)
throw new NumberFormatException("Value is out of range : " + value);
}
return (short) value;
}
@ -242,63 +235,72 @@ public final class UByte extends UNumber implements Comparable<UByte> {
* @throws ObjectStreamException
*/
private Object readResolve() throws ObjectStreamException {
return valueOf(this.value);
return valueOf(value);
}
@Override
public int intValue() {
return this.value;
return value;
}
@Override
public long longValue() {
return this.value;
return value;
}
@Override
public float floatValue() {
return this.value;
return value;
}
@Override
public double doubleValue() {
return this.value;
return value;
}
@Override
public int hashCode() {
return Short.valueOf(this.value).hashCode();
return Short.valueOf(value).hashCode();
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
if (this == obj)
return true;
}
if (obj instanceof UByte) {
return this.value == ((UByte) obj).value;
}
if (obj instanceof UByte)
return value == ((UByte) obj).value;
return false;
}
@Override
public String toString() {
return Short.valueOf(this.value).toString();
}
@Override
public String toHexString() {
return Integer.toHexString(this.value);
return Short.valueOf(value).toString();
}
@Override
public int compareTo(UByte o) {
return this.value < o.value ? -1 : this.value == o.value ? 0 : 1;
return (value < o.value ? -1 : (value == o.value ? 0 : 1));
}
@Override
public BigInteger toBigInteger() {
return BigInteger.valueOf(this.value);
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,49 +1,32 @@
/*
* Copyright (c) 2011-2013, Lukas Eder, lukas.eder@gmail.com
* All rights reserved.
* Copyright (c) 2011-2017, Data Geekery GmbH (http://www.datageekery.com)
*
* This software is licensed to you under the Apache License, Version 2.0
* (the "License"); You may obtain a copy of the License at
* 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
* http://www.apache.org/licenses/LICENSE-2.0
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* . Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* . Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* . Neither the name "jOOU" nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* 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.util.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();
@ -77,7 +60,19 @@ public final class UInteger extends UNumber implements Comparable<UInteger> {
* 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;
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>
@ -106,14 +101,14 @@ public final class UInteger extends UNumber implements Comparable<UInteger> {
// FIXME: should we log this somewhere?
return DEFAULT_PRECACHE_SIZE;
}
if (prop == null) {
if (prop == null)
return DEFAULT_PRECACHE_SIZE;
}
if (prop.length() <= 0) {
// empty value
// FIXME: should we log this somewhere?
// empty value
// FIXME: should we log this somewhere?
if (prop.length() <= 0)
return DEFAULT_PRECACHE_SIZE;
}
try {
propParsed = Long.parseLong(prop);
}
@ -122,14 +117,15 @@ public final class UInteger extends UNumber implements Comparable<UInteger> {
// FIXME: should we log this somewhere?
return DEFAULT_PRECACHE_SIZE;
}
// treat negative value as no cache...
if (propParsed < 0) {
if (propParsed < 0)
return 0;
}
if (propParsed > Integer.MAX_VALUE) {
// FIXME: should we log this somewhere
// FIXME: should we log this somewhere?
if (propParsed > Integer.MAX_VALUE)
return Integer.MAX_VALUE;
}
return (int) propParsed;
}
@ -142,13 +138,13 @@ public final class UInteger extends UNumber implements Comparable<UInteger> {
int precacheSize = getPrecacheSize();
UInteger[] ret;
if (precacheSize <= 0) {
if (precacheSize <= 0)
return null;
}
ret = new UInteger[precacheSize];
for (int i = 0; i < precacheSize; i++) {
for (int i = 0; i < precacheSize; i++)
ret[i] = new UInteger(i);
}
return ret;
}
@ -158,7 +154,7 @@ public final class UInteger extends UNumber implements Comparable<UInteger> {
* constructor without unnecessary value checks.
*
* @param value The value to wrap
* @param unused Unused paramater to distinguish between this and the
* @param unused Unused parameter to distinguish between this and the
* deprecated public constructor.
*/
private UInteger(long value, boolean unused) {
@ -172,9 +168,9 @@ public final class UInteger extends UNumber implements Comparable<UInteger> {
* @return Cached value if one exists. Null otherwise.
*/
private static UInteger getCached(long value) {
if (VALUES != null && value < VALUES.length) {
if (VALUES != null && value < VALUES.length)
return VALUES[(int) value];
}
return null;
}
@ -184,9 +180,9 @@ public final class UInteger extends UNumber implements Comparable<UInteger> {
private static UInteger valueOfUnchecked(long value) {
UInteger cached;
if ((cached = getCached(value)) != null) {
if ((cached = getCached(value)) != null)
return cached;
}
return new UInteger(value, true);
}
@ -256,9 +252,9 @@ public final class UInteger extends UNumber implements Comparable<UInteger> {
* @throws NumberFormatException if value is out of range
*/
private static long rangeCheck(long value) throws NumberFormatException {
if (value < MIN_VALUE || value > MAX_VALUE) {
if (value < MIN_VALUE || value > MAX_VALUE)
throw new NumberFormatException("Value is out of range : " + value);
}
return value;
}
@ -273,62 +269,76 @@ public final class UInteger extends UNumber implements Comparable<UInteger> {
UInteger cached;
// the value read could be invalid so check it
rangeCheck(this.value);
if ((cached = getCached(this.value)) != null) {
rangeCheck(value);
if ((cached = getCached(value)) != null)
return cached;
}
return this;
}
@Override
public int intValue() {
return (int) this.value;
return (int) value;
}
@Override
public long longValue() {
return this.value;
return value;
}
@Override
public float floatValue() {
return this.value;
return value;
}
@Override
public double doubleValue() {
return this.value;
return value;
}
@Override
public BigInteger toBigInteger() {
return BigInteger.valueOf(value);
}
@Override
public int hashCode() {
return Long.valueOf(this.value).hashCode();
return Long.valueOf(value).hashCode();
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
if (this == obj)
return true;
}
if (obj instanceof UInteger) {
return this.value == ((UInteger) obj).value;
}
if (obj instanceof UInteger)
return value == ((UInteger) obj).value;
return false;
}
@Override
public String toString() {
return Long.valueOf(this.value).toString();
}
@Override
public String toHexString() {
return Long.toHexString(this.value);
return Long.valueOf(value).toString();
}
@Override
public int compareTo(UInteger o) {
return this.value < o.value ? -1 : this.value == o.value ? 0 : 1;
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,37 +1,17 @@
/*
* Copyright (c) 2011-2013, Lukas Eder, lukas.eder@gmail.com
* All rights reserved.
* Copyright (c) 2011-2017, Data Geekery GmbH (http://www.datageekery.com)
*
* This software is licensed to you under the Apache License, Version 2.0
* (the "License"); You may obtain a copy of the License at
* 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
* http://www.apache.org/licenses/LICENSE-2.0
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* . Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* . Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* . Neither the name "jOOU" nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* 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.util.bytes;
@ -41,6 +21,8 @@ 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> {
@ -67,10 +49,22 @@ public final class ULong extends UNumber implements Comparable<ULong> {
*/
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 BigInteger value;
private final long value;
/**
* Create an <code>unsigned long</code>
@ -101,6 +95,12 @@ public final class ULong extends UNumber implements Comparable<ULong> {
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>
*
@ -108,8 +108,10 @@ public final class ULong extends UNumber implements Comparable<ULong> {
* of an <code>unsigned long</code>
*/
private ULong(BigInteger value) throws NumberFormatException {
this.value = value;
rangeCheck();
if (value.compareTo(MIN_VALUE) < 0 || value.compareTo(MAX_VALUE) > 0)
throw new NumberFormatException();
else
this.value = value.longValue();
}
/**
@ -118,12 +120,7 @@ public final class ULong extends UNumber implements Comparable<ULong> {
* <code>(uint) 18446744073709551615</code>
*/
private ULong(long value) {
if (value >= 0) {
this.value = BigInteger.valueOf(value);
}
else {
this.value = BigInteger.valueOf(value & Long.MAX_VALUE).add(MAX_VALUE_LONG);
}
this.value = value;
}
/**
@ -133,71 +130,140 @@ public final class ULong extends UNumber implements Comparable<ULong> {
* parsable <code>unsigned long</code>.
*/
private ULong(String value) throws NumberFormatException {
this.value = new BigInteger(value);
rangeCheck();
}
if (value == null)
throw new NumberFormatException("null");
private void rangeCheck() throws NumberFormatException {
if (this.value.compareTo(MIN_VALUE) < 0 || this.value.compareTo(MAX_VALUE) > 0) {
throw new NumberFormatException("Value is out of range : " + this.value);
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 this.value.intValue();
return (int) value;
}
@Override
public long longValue() {
return this.value.longValue();
return value;
}
@Override
public float floatValue() {
return this.value.floatValue();
if (value < 0)
return ((float) (value & Long.MAX_VALUE)) + Long.MAX_VALUE;
else
return value;
}
@Override
public double doubleValue() {
return this.value.doubleValue();
if (value < 0)
return ((double) (value & Long.MAX_VALUE)) + Long.MAX_VALUE;
else
return value;
}
@Override
public int hashCode() {
return this.value.hashCode();
return Long.valueOf(value).hashCode();
}
@Override
public boolean equals(Object obj) {
if (obj instanceof ULong) {
return this.value.equals(((ULong) obj).value);
}
if (obj instanceof ULong)
return value == ((ULong) obj).value;
return false;
}
/**
* Get this number as a {@link BigInteger}. This is a convenience method for
* calling <code>new BigInteger(toString())</code>
*/
@Override
public BigInteger toBigInteger() {
return this.value;
}
@Override
public String toString() {
return this.value.toString();
}
@Override
public String toHexString() {
return this.value.toString(16);
if (value >= 0)
return Long.toString(value);
else
return BigInteger.valueOf(value & Long.MAX_VALUE).add(MAX_VALUE_LONG).toString();
}
@Override
public int compareTo(ULong o) {
return this.value.compareTo(o.value);
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,37 +1,17 @@
/*
* Copyright (c) 2011-2013, Lukas Eder, lukas.eder@gmail.com
* All rights reserved.
* Copyright (c) 2011-2017, Data Geekery GmbH (http://www.datageekery.com)
*
* This software is licensed to you under the Apache License, Version 2.0
* (the "License"); You may obtain a copy of the License at
* 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
* http://www.apache.org/licenses/LICENSE-2.0
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* . Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* . Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* . Neither the name "jOOU" nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* 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.util.bytes;
@ -49,11 +29,6 @@ public abstract class UNumber extends Number {
*/
private static final long serialVersionUID = -7666221938815339843L;
/**
* Converts this number to a hex string representation
*/
public abstract String toHexString();
/**
* Get this number as a {@link BigInteger}. This is a convenience method for
* calling <code>new BigInteger(toString())</code>

View File

@ -1,44 +1,27 @@
/*
* Copyright (c) 2011-2013, Lukas Eder, lukas.eder@gmail.com
* All rights reserved.
* Copyright (c) 2011-2017, Data Geekery GmbH (http://www.datageekery.com)
*
* This software is licensed to you under the Apache License, Version 2.0
* (the "License"); You may obtain a copy of the License at
* 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
* http://www.apache.org/licenses/LICENSE-2.0
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* . Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* . Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* . Neither the name "jOOU" nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* 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.util.bytes;
import java.math.BigInteger;
/**
* The <code>unsigned short</code> type
*
* @author Lukas Eder
* @author Jens Nerche
*/
public final class UShort extends UNumber implements Comparable<UShort> {
@ -59,6 +42,18 @@ public final class UShort extends UNumber implements Comparable<UShort> {
*/
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>
*/
@ -125,57 +120,71 @@ public final class UShort extends UNumber implements Comparable<UShort> {
}
private void rangeCheck() throws NumberFormatException {
if (this.value < MIN_VALUE || this.value > MAX_VALUE) {
throw new NumberFormatException("Value is out of range : " + this.value);
}
if (value < MIN_VALUE || value > MAX_VALUE)
throw new NumberFormatException("Value is out of range : " + value);
}
@Override
public int intValue() {
return this.value;
return value;
}
@Override
public long longValue() {
return this.value;
return value;
}
@Override
public float floatValue() {
return this.value;
return value;
}
@Override
public double doubleValue() {
return this.value;
return value;
}
@Override
public BigInteger toBigInteger() {
return BigInteger.valueOf(value);
}
@Override
public int hashCode() {
return Integer.valueOf(this.value).hashCode();
return Integer.valueOf(value).hashCode();
}
@Override
public boolean equals(Object obj) {
if (obj instanceof UShort) {
return this.value == ((UShort) obj).value;
}
if (obj instanceof UShort)
return value == ((UShort) obj).value;
return false;
}
@Override
public String toString() {
return Integer.valueOf(this.value).toString();
}
@Override
public String toHexString() {
return Integer.toHexString(this.value);
return Integer.valueOf(value).toString();
}
@Override
public int compareTo(UShort o) {
return this.value < o.value ? -1 : this.value == o.value ? 0 : 1;
return (value < o.value ? -1 : (value == o.value ? 0 : 1));
}
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,37 +1,17 @@
/*
* Copyright (c) 2011-2013, Lukas Eder, lukas.eder@gmail.com
* All rights reserved.
* Copyright (c) 2011-2017, Data Geekery GmbH (http://www.datageekery.com)
*
* This software is licensed to you under the Apache License, Version 2.0
* (the "License"); You may obtain a copy of the License at
* 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
* http://www.apache.org/licenses/LICENSE-2.0
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* . Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* . Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* . Neither the name "jOOU" nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* 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.util.bytes;
@ -55,7 +35,8 @@ public final class Unsigned {
* parsable <code>unsigned byte</code>.
* @see UByte#valueOf(String)
*/
public static UByte ubyte(String value) throws NumberFormatException {
public static
UByte ubyte(String value) throws NumberFormatException {
return value == null ? null : UByte.valueOf(value);
}
@ -109,7 +90,8 @@ public final class Unsigned {
* parsable <code>unsigned short</code>.
* @see UShort#valueOf(String)
*/
public static UShort ushort(String value) throws NumberFormatException {
public static
UShort ushort(String value) throws NumberFormatException {
return value == null ? null : UShort.valueOf(value);
}
@ -142,7 +124,8 @@ public final class Unsigned {
* parsable <code>unsigned int</code>.
* @see UInteger#valueOf(String)
*/
public static UInteger uint(String value) throws NumberFormatException {
public static
UInteger uint(String value) throws NumberFormatException {
return value == null ? null : UInteger.valueOf(value);
}
@ -175,7 +158,8 @@ public final class Unsigned {
* parsable <code>unsigned long</code>.
* @see ULong#valueOf(String)
*/
public static ULong ulong(String value) throws NumberFormatException {
public static
ULong ulong(String value) throws NumberFormatException {
return value == null ? null : ULong.valueOf(value);
}