org.at4j.support.lang
Class UnsignedInteger

java.lang.Object
  extended by org.at4j.support.lang.UnsignedInteger
All Implemented Interfaces:
Serializable, Comparable<UnsignedInteger>

public final class UnsignedInteger
extends Object
implements Serializable, Comparable<UnsignedInteger>

This object represents an unsigned integer (four bytes or 32 bits) with a value between {code 0} and 4294967295. It is immutable.

Unsigned integers are created by calling any of the static creation methods of this class.

Since:
1.0
Author:
Karl Gustafsson
See Also:
SignedInteger, UnsignedByte, UnsignedShort, UnsignedLong, Serialized Form

Field Summary
static long MAX_VALUE
          The maximum value of an unsigned integer (4294967295).
static int MIN_VALUE
          The minimum value of an unsigned integer (0).
static UnsignedInteger ONE
          The value 1.
static int SIZE
          Each unsigned integer is four bytes long.
static UnsignedInteger ZERO
          The value 0.
 
Method Summary
 int compareTo(UnsignedInteger i2)
           
 boolean equals(Object o)
           
static UnsignedInteger fromBigEndianByteArray(byte[] barr)
          Create an unsigned integer value from a four bytes long, big-endian byte array.
static UnsignedInteger fromBigEndianByteArray(byte[] barr, int offset)
          Create an unsigned integer value from four bytes read from the given offset position in the supplied byte array.
static long fromBigEndianByteArrayToLong(byte[] barr, int offset)
          Create a long value representing the unsigned integer value in the byte array at the specified offset.
static long fromLittleEndianByteArrayToLong(byte[] barr, int offset)
          Create a long value representing the unsigned integer value in the byte array at the specified offset.
 byte[] getBigEndianByteArray()
          Get the unsigned integer value as a big-endian, four bytes long byte array.
 int hashCode()
           
 int intValue()
          Get the unsigned integer value converted to a signed integer.
 long longValue()
          Get the unsigned integer value represented as a long.
static UnsignedInteger readBigEndian(InputStream is)
          Create an unsigned integer value from four bytes read from the stream.
static UnsignedInteger readBigEndian(RandomAccess ra)
          Create an unsigned integer value from four bytes read starting from the current position in the supplied RandomAccess.
 String toString()
           
static UnsignedInteger valueOf(int value)
          Create a new unsigned integer.
static UnsignedInteger valueOf(long value)
          Create an unsigned integer from the supplied long value which must be between 0 and 4294967295 (inclusive).
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

SIZE

public static final int SIZE
Each unsigned integer is four bytes long.

See Also:
Constant Field Values

MAX_VALUE

public static final long MAX_VALUE
The maximum value of an unsigned integer (4294967295).

See Also:
Constant Field Values

MIN_VALUE

public static final int MIN_VALUE
The minimum value of an unsigned integer (0).

See Also:
Constant Field Values

ZERO

public static final UnsignedInteger ZERO
The value 0.


ONE

public static final UnsignedInteger ONE
The value 1.

Method Detail

valueOf

public static UnsignedInteger valueOf(int value)
Create a new unsigned integer. The supplied integer is treated as an unsigned value, which means that negative argument values will result in unsigned integer values between 2147483648 and 4294967295 (inclusive).

Parameters:
value - The signed integer value.
Returns:
An unsigned integer value.

valueOf

public static UnsignedInteger valueOf(long value)
                               throws IllegalArgumentException
Create an unsigned integer from the supplied long value which must be between 0 and 4294967295 (inclusive).

Parameters:
value - The value.
Returns:
The unsigned integer value.
Throws:
IllegalArgumentException - If the supplied value is not in the permitted range.

longValue

public long longValue()
Get the unsigned integer value represented as a long.

Returns:
The value.

intValue

public int intValue()
Get the unsigned integer value converted to a signed integer.

Returns:
The unsigned integer value converted to a signed integer.

getBigEndianByteArray

public byte[] getBigEndianByteArray()
Get the unsigned integer value as a big-endian, four bytes long byte array.

Returns:
The value represented as a big-endian byte array.

fromBigEndianByteArray

public static UnsignedInteger fromBigEndianByteArray(byte[] barr)
                                              throws IllegalArgumentException
Create an unsigned integer value from a four bytes long, big-endian byte array.

Parameters:
barr - The byte array. It must be four bytes long.
Returns:
The unsigned integer.
Throws:
IllegalArgumentException - If the supplied byte array is not four bytes long.
See Also:
fromBigEndianByteArray(byte[], int), fromBigEndianByteArrayToLong(byte[], int)

fromBigEndianByteArray

public static UnsignedInteger fromBigEndianByteArray(byte[] barr,
                                                     int offset)
                                              throws ArrayIndexOutOfBoundsException
Create an unsigned integer value from four bytes read from the given offset position in the supplied byte array. The most significant byte is the last byte read.

Parameters:
barr - The byte array to read from.
offset - The offset in the byte array where the least significant (first) byte is.
Returns:
An unsigned integer.
Throws:
ArrayIndexOutOfBoundsException - If the supplied array is too short or if the offset is negative.
See Also:
fromBigEndianByteArray(byte[]), fromBigEndianByteArrayToLong(byte[], int)

fromBigEndianByteArrayToLong

public static long fromBigEndianByteArrayToLong(byte[] barr,
                                                int offset)
                                         throws ArrayIndexOutOfBoundsException
Create a long value representing the unsigned integer value in the byte array at the specified offset. The most significant byte is the last byte read.

Parameters:
barr - The byte array to read from.
offset - The offset in the byte array where the least significant (first) byte is.
Returns:
A long representing the unsigned integer.
Throws:
ArrayIndexOutOfBoundsException - If the supplied array is too short or if the offset is negative.
Since:
1.1
See Also:
fromBigEndianByteArray(byte[]), fromBigEndianByteArray(byte[], int), fromLittleEndianByteArrayToLong(byte[], int)

fromLittleEndianByteArrayToLong

public static long fromLittleEndianByteArrayToLong(byte[] barr,
                                                   int offset)
                                            throws ArrayIndexOutOfBoundsException
Create a long value representing the unsigned integer value in the byte array at the specified offset. The most significant byte is the first byte read.

Parameters:
barr - The byte array to read from.
offset - The offset in the byte array where the most significant (first) byte is.
Returns:
A long representing the unsigned integer.
Throws:
ArrayIndexOutOfBoundsException - If the supplied array is too short or if the offset is negative.
Since:
1.1
See Also:
fromBigEndianByteArrayToLong(byte[], int)

readBigEndian

public static UnsignedInteger readBigEndian(RandomAccess ra)
                                     throws UnexpectedEofException,
                                            WrappedIOException
Create an unsigned integer value from four bytes read starting from the current position in the supplied RandomAccess. The most significant byte is the last byte read.

Parameters:
ra - The RandomAccess to read from. The current position is advanced four positions by this method.
Returns:
An unsigned integer.
Throws:
UnexpectedEofException - If the end of the file is reached before four bytes have been read.
WrappedIOException - On I/O errors.

readBigEndian

public static UnsignedInteger readBigEndian(InputStream is)
                                     throws UnexpectedEofException,
                                            WrappedIOException
Create an unsigned integer value from four bytes read from the stream. The most significant byte is the last byte read.

Parameters:
is - The stream to read from.
Returns:
An unsigned integer.
Throws:
UnexpectedEofException - If the end of the stream is reached before four bytes have been read.
WrappedIOException - On I/O errors.

equals

public boolean equals(Object o)
Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object

compareTo

public int compareTo(UnsignedInteger i2)
Specified by:
compareTo in interface Comparable<UnsignedInteger>

toString

public String toString()
Overrides:
toString in class Object