com.faunos.util.io
Enum Word

java.lang.Object
  extended by java.lang.Enum<Word>
      extended by com.faunos.util.io.Word
All Implemented Interfaces:
Serializable, Comparable<Word>

public enum Word
extends Enum<Word>

An enumeration of the data widths. As of this writing, the data widths correspond to that of the primitives: byte, short, int, and long.

The point of this enum is to provide a data-width-neutral way of manipulating integral values.

Note that the write methods generally truncate the high bits (beyond the word width) of the input argument.

Notes

--8/18/08
BF: 3 or 6 byte word type worthwhile (?)

--8/23/08
BF: Moved from skwish to commons

Author:
Babak Farhang

Enum Constant Summary
BYTE
           
INT
           
LONG
           
SHORT
           
 
Method Summary
static Word forWidth(int width)
           
 long fromNativeBits(long bits)
          Returns value of the given bit field interpreted in this word format.
 long getHiBit()
          Returns the high bit of this Word as a bit mask, with the high bit on (1) and the rest of the bits zero..
 long getHiBitComplement()
          Returns the complement of the high bit for this Word.
 long getMaxValue()
          Returns the maximum signed value the Word can represent.
 long getMinValue()
          Returns the maximum signed value the Word can represent.
 byte getWidth()
          Returns the width of this word in bytes.
 boolean inRange(long value)
          Determines whether the given value can be represented using an integral value of this word width.
 long read(ByteBuffer in)
          Reads width amount of bytes from the given buffer and interprets it as a signed value (big endian two's complement) and returns the result
 long toNativeBits(long value)
          Returns this enum instance's native, 2's complement representation of the specified signed value.
static Word valueOf(String name)
          Returns the enum constant of this type with the specified name.
static Word[] values()
          Returns an array containing the constants of this enum type, in the order they're declared.
 void write(ByteBuffer out, byte value)
          Writes the given value to the given out buffer.
 void write(ByteBuffer out, int value)
          Writes the given value to the given out buffer.
 void write(ByteBuffer out, long value)
          Writes the given value to the given out buffer.
 void write(ByteBuffer out, short value)
          Writes the given value to the given out buffer.
 
Methods inherited from class java.lang.Enum
clone, compareTo, equals, getDeclaringClass, hashCode, name, ordinal, toString, valueOf
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Enum Constant Detail

BYTE

public static final Word BYTE

SHORT

public static final Word SHORT

INT

public static final Word INT

LONG

public static final Word LONG
Method Detail

values

public static final Word[] values()
Returns an array containing the constants of this enum type, in the order they're declared. This method may be used to iterate over the constants as follows:
for(Word c : Word.values())
        System.out.println(c);

Returns:
an array containing the constants of this enum type, in the order they're declared

valueOf

public static Word valueOf(String name)
Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)

Parameters:
name - the name of the enum constant to be returned.
Returns:
the enum constant with the specified name
Throws:
IllegalArgumentException - if this enum type has no constant with the specified name

getWidth

public byte getWidth()
Returns the width of this word in bytes.


getMaxValue

public long getMaxValue()
Returns the maximum signed value the Word can represent.


getMinValue

public long getMinValue()
Returns the maximum signed value the Word can represent.


getHiBit

public long getHiBit()
Returns the high bit of this Word as a bit mask, with the high bit on (1) and the rest of the bits zero.. For example, the BYTE enum instance returns 0x80L.

Returns:
1L << (8 * getWidth() - 1)

toNativeBits

public long toNativeBits(long value)
Returns this enum instance's native, 2's complement representation of the specified signed value. (For nonnegative values that are in range, this method just returns the same value.) The returned value is bit-wise equivalent to the value's representation in this word format.

Note that if the given value is not in range, then the value's high bytes will be truncated and ignored.

See Also:
fromNativeBits(long), getHiBit()

fromNativeBits

public long fromNativeBits(long bits)
Returns value of the given bit field interpreted in this word format. The returned result is just the signed value obtained on truncating the given bits to this word width.

See Also:
toNativeBits(long), getHiBit()

getHiBitComplement

public long getHiBitComplement()
Returns the complement of the high bit for this Word.

Returns:
~(1L << (8 * getWidth() - 1))

read

public long read(ByteBuffer in)
Reads width amount of bytes from the given buffer and interprets it as a signed value (big endian two's complement) and returns the result

Parameters:
in - the buffer read from. The position is advanced by the width of this Word
Returns:
the interpreted signed value

write

public void write(ByteBuffer out,
                  byte value)
Writes the given value to the given out buffer. The position of the buffer is advanced by width amount.


write

public void write(ByteBuffer out,
                  short value)
Writes the given value to the given out buffer. The position of the buffer is advanced by width amount.


write

public void write(ByteBuffer out,
                  int value)
Writes the given value to the given out buffer. The position of the buffer is advanced by width amount.


write

public void write(ByteBuffer out,
                  long value)
Writes the given value to the given out buffer. The position of the buffer is advanced by width amount.


inRange

public boolean inRange(long value)
Determines whether the given value can be represented using an integral value of this word width.

Returns:
value >= minValue && value <= maxValue

forWidth

public static Word forWidth(int width)


SourceForge.net Logo