org.joou.ULong Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of joou-java-6 Show documentation
Show all versions of joou-java-6 Show documentation
jOOU is a recursive acronym that stands for jOOU Object Oriented Unsigned Types. It is a simple wrapper for UByte, UShort, UInteger, ULong types
The newest version!
/*
* Copyright (c) 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 org.joou;
import java.math.BigInteger;
/**
* The unsigned long
type
*
* @author Lukas Eder
* @author Jens Nerche
* @author Ivan Sokolov
*/
public final class ULong extends UNumber implements Comparable {
/**
* Generated UID
*/
private static final long serialVersionUID = -6821055240959745390L;
/**
* A constant holding the minimum value an unsigned long
can
* have, 0.
*/
public static final BigInteger MIN_VALUE = BigInteger.ZERO;
/**
* A constant holding the maximum value an unsigned long
can
* have, 264-1.
*/
public static final BigInteger MAX_VALUE = new BigInteger("18446744073709551615");
/**
* A constant holding the maximum value + 1 an signed long
can
* have, 263.
*/
public static final BigInteger MAX_VALUE_LONG = new BigInteger("9223372036854775808");
/**
* A constant holding the minimum value an unsigned long
can
* have as ULong, 0.
*/
public static final ULong MIN = valueOf(MIN_VALUE.longValue());
/**
* A constant holding the maximum value + 1 an signed long
can
* have as ULong, 263.
*/
public static final ULong MAX = valueOf(MAX_VALUE);
/**
* The value modelling the content of this unsigned long
*/
private final long value;
/**
* Create an unsigned long
*
* @throws NumberFormatException If value
does not contain a
* parsable unsigned long
.
*/
public static ULong valueOf(String value) throws NumberFormatException {
return new ULong(value);
}
/**
* Create an unsigned long
by masking it with
* 0xFFFFFFFFFFFFFFFF
i.e. (long) -1
becomes
* (uint) 18446744073709551615
*/
public static ULong valueOf(long value) {
return new ULong(value);
}
/**
* Create an unsigned long
*
* @throws NumberFormatException If value
is not in the range
* of an unsigned long
*/
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 unsigned long
*
* @throws NumberFormatException If value
is not in the range
* of an unsigned long
*/
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 unsigned long
by masking it with
* 0xFFFFFFFFFFFFFFFF
i.e. (long) -1
becomes
* (uint) 18446744073709551615
*/
private ULong(long value) {
this.value = value;
}
/**
* Create an unsigned long
*
* @throws NumberFormatException If value
does not contain a
* parsable unsigned long
.
*/
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 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);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy