/* File: UnsignedLong.java CVS Info: $Id: UnsignedLong.java,v 1 2000/07/10 22:48:51 laflam Exp $ Compiler: jdk 1.3 */ package mil.navy.nps.util; // package we belong to import java.io.*; import java.lang.*; /** *@version 1.0 *@author Don McGregor (http://www.web3d.org/WorkingGroups/vrtp/dis-java-vrml/) * *
* * This implements the SerializationInterface, which means it can be * written out to a stream in the standard DIS 64-bit format.
* * See comments in UnsignedByte for description of how the mapping between * negative values and unsigned positive values works.
* * 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.
* *
java mil.navy.nps.util.UnsignedLong
*
*UnsignedLong
object and initializes its value to 0.
*/
public UnsignedLong()
{ _data = 0;
}
/**
*Constructs a UnsignedLong
object from a signed byte,
*throws an exception if the parameter is out of range.
*
*@param pData >=0,<=MAX_UNSIGNEDLONG_VALUE
*@exception RuntimeException if pData
is out of range
*/
public UnsignedLong(byte pData)
{
if ( pData < 0 )
throw new
RuntimeException("Exception in UnsignedLong. Attempt to assign value beyond maximum range of an unsigned long.");
_data = pData;
}
/**
*Constructs a UnsignedLong
object from a signed short,
*throws an exception if the paraneter is out of range.
*
*@param pData >=0
*@exception RuntimeException if pData
is out of range
*/
public UnsignedLong(short pData)
{
if ( pData < 0 )
throw new
RuntimeException("Exception in UnsignedLong. Attempt to assign value beyond maximum range of an unsigned long.");
_data = pData;
}
/**
*Constructs an UnsignedLong
object from a signed int,
*throws an exception if the parameter is out of range.
*
*@param pData >=0
*@exception RuntimeException if pData
is out of range
*/
public UnsignedLong(int pData)
{
if (pData < 0)
throw new
RuntimeException("Exception in UnsignedLong. Attempt to assign value beyond maximum range of an unsigned long.");
_data = pData;
}
/**
*Constructs an UnsignedLong
object from a signed double,
*throws an exception if the parameter is out of range.
*
*@param pData >=0
*@exception RuntimeException if pData
is out of range
*/
public UnsignedLong(double pData)
{
if(pData < 0)
throw new
RuntimeException("Exception in UnsignedLong. Attempt to assign value beyond maximum range of an unsigned long.");
_data = (long) pData;
}
/**
*Constructs an UnsignedLong
object from a signed float,
*throws an exception if the parameter is out of range.
*
*@param pData >=0
*@exception RuntimeException if pData
is out of range
*/
public UnsignedLong(float pData)
{
if(pData < 0)
throw new
RuntimeException("Exception in UnsignedLong. Attempt to assign value beyond maximum range of an unsigned long.");
_data = (long) pData;
}
/**
*Constructs an UnsignedLong
object from a signed long,
*throws an exception if the parameter is out of range.
*
*@param pData >=0
*@exception RuntimeException if pData
is out of range
*/
public UnsignedLong(long pData)
{
if(pData < 0)
throw new
RuntimeException("Exception in UnsignedLong. Attempt to assign value beyond maximum range of an unsigned long.");
_data = pData;
}
/**
*Returns a the current value of this object as a float, after conversion.
*
*@return the current value of this object as a float
*/
public float floatValue()
{
double temp;
temp = (_data < 0 ? MAX_UNSIGNEDLONG_VALUE + _data + 1 : _data);
return (float)temp;
}
/**
*Returns the current value of this object as a double float,
*after conversion.
*
*@return the current value of this object as a double float
*/
public double doubleValue()
{
double temp;
temp = (_data < 0 ? MAX_UNSIGNEDLONG_VALUE + _data + 1 : _data);
return temp;
}
/**
*Returns the current value of this object as a long, after conversion.
*
*@return the current value of this object as a long
*/
public long longValue()
{
double temp;
temp = (_data < 0 ? MAX_UNSIGNEDLONG_VALUE + _data + 1 : _data);
return (long)temp;
}
/**
*Don't use this !
*
*IOException
occurs.
*
*@see SerializationInterface
*/
public void serialize(DataOutputStream pOutputStream)
{
try
{
pOutputStream.writeLong(_data);
}
catch (IOException ioError)
{
throw new
RuntimeException("Exception in UnsignedLong. Error writing to file.");
}
}
/**
*Reads an UnsignedLong input from DIS format.
*
*@param inputstream the input stream that builds the object.
*@exception RuntimeException if an IOException
occurs.
*
*@see SerializationInterface
*/
public void deSerialize(DataInputStream pInputStream)
{
try
{
_data = pInputStream.readLong();
}
catch (IOException ioError)
{
throw new
RuntimeException("Exception in UnsignedLong. Error reading from file.");
}
}
/**
*Returns a String object representing this UnsignedLong
value.
*
*@return a string representation of the value of this object in base 10.
*/
public String toString()
{
Long temp = new Long(this.longValue());
return temp.toString();
}
/**
*Makes a deep copy of the current UnsignedLong
object.
*
*@return the cloned object.
*@exception RuntimeException if cloning fails
*/
public Object clone()
{
Object newObject;
try{
newObject = super.clone(); // shallow copy
}
catch(CloneNotSupportedException cloneError)
{
throw new
RuntimeException("Exception in UnsignedByte. Error cloning object.");
}
return newObject;
}
/**
*Of debugging interest only. You shouldn't have to use this method.
*/
public static void debugTest()
{
// Some testing to make sure things work right.
UnsignedLong zero, one, two, three, four, five, six;
// test extreme values for bytes; zero, near the roll-over point, and the max value
System.out.println("Testing functionality of unsigned long class");
zero = new UnsignedLong((long)0);
one = new UnsignedLong((long)2147483647 );
two = new UnsignedLong((long)2147483648L);
three = new UnsignedLong((long)2147483649L );
four = new UnsignedLong((long)MAX_UNSIGNEDLONG_VALUE);
System.out.println("Values should be 0, 2147483647, 2147483648L, 2147483649L, 4294907295L:");
System.out.println(" >>> " + zero.toString() + " " + one.toString()
+ " " + two.toString() + " " + three.toString() + " " + four.toString());
System.out.println();
System.out.println("Causes runtime exception: five = new UnsignedLong(-1);");
try {
five = new UnsignedLong(-1);
}
catch (Exception e) { System.out.println(e); }
System.out.println();
System.out.println("Causes runtime exception: six = new UnsignedLong((long) MAX_UNSIGNEDLONG_VALUE + 1);");
try {
six = new UnsignedLong((long) MAX_UNSIGNEDLONG_VALUE + 1);
}
catch (Exception e) { System.out.println(e); }
}
public static void main (String[] args)
{
debugTest();
}
} // end of class UnsignedLong