|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.lang.Number
mil.navy.nps.util.UnsignedByte
Note that you can't retrieve the value of this as a byte, simply because a negative value for an unsigned byte (as the signed value 242 would be) is invalid, and can't be represented. So we have to promote the bloody thing to an int.
Hmmmmm....should the internal representation be a short or a byte? I vote for byte, since it requires less storage in memory, and we might have a lot of these floating about. But we're constantly promoting it to an int or short anyway...
This implementation works by exploiting the characteristics of how numbers are stored internally. Just about all systems, except for a few obsolete machines, use "two's-complement" format for storing integer values. A signed byte will have this sequence as more and more bits are flipped on:
0,1,2,3....125,126,127,-128,-127,-127,...-1
The values roll over from (2^7)-1 to -(2^7) at the midpoint. Adding
one to 127 will result in -128, adding two results in -127, etc. We
can fake up unsigned bytes by exploiting this behavior. The maximum
an unsigned byte can represent is 255 (2^8 - 1) so we can translate
between a negative value and its unsigned positive representation with
this formula:
which means that -128 is mapped to 128, -126 to 129, and so on.
255 + _data + 1
This implements the Cloneable interface, which means that when we clone() something we can do a bitwise field copy (shallow copy). This needs to change if we ever have any actual objects contained in this class.
UnsignedShort
,
UnsignedInt
,
SerializationInterface
,
Serialized FormField Summary | |
static short |
MAX_UNSIGNED_BYTE_VALUE
|
Constructor Summary | |
UnsignedByte()
Contructs a new UnsignedByte object and intializes its value to 0. |
|
UnsignedByte(byte pData)
Constructs an UnsignedByte object from a signed byte,
throws an exception if the paraneter is out of range. |
|
UnsignedByte(int pData)
Constructs an UnsignedByte object from an int,
throws an exception if the parameter is out of range. |
|
UnsignedByte(short pData)
Constructs an UnsignedByte object from a short,
throws an exception if the parameter is out of range. |
Method Summary | |
java.lang.Object |
clone()
Makes a deep copy of the current UnsignedByte object. |
void |
debugTest()
Of debugging interest only. |
void |
deSerialize(java.io.DataInputStream pInputStream)
Reads an UnsignedByte in from DIS format. |
double |
doubleValue()
Returns the current value of this object as a double float, after conversion. |
float |
floatValue()
Returns a the current value of this object as a float, after conversion. |
int |
intValue()
Returns the current value of this object as an int, after conversion. |
long |
longValue()
Returns the current value of this object as a long, after conversion. |
void |
serialize(java.io.DataOutputStream pOutputStream)
Writes out an UnsignedByte to an output stream. |
java.lang.String |
toString()
Returns a String object representing this UnsignedByte value. |
Methods inherited from class java.lang.Number |
byteValue, shortValue |
Methods inherited from class java.lang.Object |
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
public static final short MAX_UNSIGNED_BYTE_VALUE
Constructor Detail |
public UnsignedByte()
UnsignedByte
object and intializes its value to 0.
public UnsignedByte(byte pData)
UnsignedByte
object from a signed byte,
throws an exception if the paraneter is out of range.
pData
- >=0
java.lang.RuntimeException
- if pData
is out of rangepublic UnsignedByte(short pData)
UnsignedByte
object from a short,
throws an exception if the parameter is out of range.
pData
- >=0, <=MAX_UNSIGNED_BYTE_VALUE
java.lang.RuntimeException
- if the parameter is out of rangepublic UnsignedByte(int pData)
UnsignedByte
object from an int,
throws an exception if the parameter is out of range.
pData
- >=0, <=MAX_UNSIGNED_BYTE_VALUE
java.lang.RuntimeException
- if pData
is out of rangeMethod Detail |
public double doubleValue()
public float floatValue()
public int intValue()
public long longValue()
public void serialize(java.io.DataOutputStream pOutputStream)
serialize
in interface SerializationInterface
a
- runtime Exception if an IOException
occurs.SerializationInterface
public void deSerialize(java.io.DataInputStream pInputStream)
deSerialize
in interface SerializationInterface
java.lang.RuntimeException
- if an IOException
occurs.SerializationInterface
public java.lang.String toString()
UnsignedByte
value.
public void debugTest()
public java.lang.Object clone()
UnsignedByte
object.
java.lang.RuntimeException
- if cloning fails
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |