org.at4j.support.lang
Class UnsignedShort

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

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

This object represents an unsigned short value (two bytes or 16 bits) with a value between {code 0} and 65535. It is immutable.

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

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

Field Summary
static int MAX_VALUE
          The maximum value of an unsigned short (65535).
static int MIN_VALUE
          The minimum value of an unsigned short (0).
static UnsignedShort ONE
          The value 1.
static UnsignedShort ONE_THOUSAND
          The value 1000.
static int SIZE
          Each unsigned short is two bytes long.
static UnsignedShort ZERO
          The value 0.
 
Method Summary
 int compareTo(UnsignedShort s2)
           
 boolean equals(Object o)
           
static UnsignedShort fromBigEndianByteArray(byte[] barr)
          Create an unsigned short value from a two bytes long, big-endian byte array.
static UnsignedShort fromBigEndianByteArray(byte[] barr, int offset)
          Create an unsigned short value from two bytes read from the given offset position in the supplied byte array.
 byte[] getBigEndianByteArray()
          Get the unsigned short value as a big-endian, two bytes long byte array.
 int hashCode()
           
 int intValue()
          Get the unsigned short value.
static UnsignedShort readBigEndian(InputStream is)
          Create an unsigned short value from two bytes read from the stream.
static UnsignedShort readBigEndian(RandomAccess ra)
          Create an unsigned short value from two bytes read starting from the current position in the supplied RandomAccess.
 String toString()
           
static UnsignedShort valueOf(int value)
          Create an unsigned short from the supplied integer value which must be between 0 and 65535 (inclusive).
static UnsignedShort valueOf(short value)
          Create a new unsigned short.
 
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 short is two bytes long.

See Also:
Constant Field Values

MAX_VALUE

public static final int MAX_VALUE
The maximum value of an unsigned short (65535).

See Also:
Constant Field Values

MIN_VALUE

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

See Also:
Constant Field Values

ZERO

public static final UnsignedShort ZERO
The value 0.


ONE

public static final UnsignedShort ONE
The value 1.


ONE_THOUSAND

public static final UnsignedShort ONE_THOUSAND
The value 1000.

Method Detail

valueOf

public static UnsignedShort valueOf(short value)
Create a new unsigned short. The supplied short is treated as an unsigned value, which means that negative argument values will result in unsigned short values between 32768 and 65535 (inclusive).

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

valueOf

public static UnsignedShort valueOf(int value)
                             throws IllegalArgumentException
Create an unsigned short from the supplied integer value which must be between 0 and 65535 (inclusive).

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

intValue

public int intValue()
Get the unsigned short value.

Returns:
The value.

getBigEndianByteArray

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

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

fromBigEndianByteArray

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

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

fromBigEndianByteArray

public static UnsignedShort fromBigEndianByteArray(byte[] barr,
                                                   int offset)
                                            throws ArrayIndexOutOfBoundsException
Create an unsigned short value from two 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 short.
Throws:
ArrayIndexOutOfBoundsException - If the supplied array is too short or if the offset is negative.
See Also:
fromBigEndianByteArray(byte[])

readBigEndian

public static UnsignedShort readBigEndian(RandomAccess ra)
                                   throws UnexpectedEofException,
                                          WrappedIOException
Create an unsigned short value from two 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 two positions by this method.
Returns:
An unsigned short.
Throws:
UnexpectedEofException - If the end of the file is reached before two bytes have been read.
WrappedIOException - On I/O errors.

readBigEndian

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

Parameters:
is - The stream to read from.
Returns:
An unsigned short.
Throws:
UnexpectedEofException - If the end of the stream is reached before two 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(UnsignedShort s2)
Specified by:
compareTo in interface Comparable<UnsignedShort>

toString

public String toString()
Overrides:
toString in class Object