net.lucidviews.util
Class VariableNumber

java.lang.Object
  extended by java.lang.Number
      extended by net.lucidviews.util.VariableNumber
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
VariableDouble, VariableLong

public abstract class VariableNumber
extends Number

An abstract base class for Numbers that can change their value.

The mandate of a VariableNumber is to provide a number object that can change value, thereby removing the need to instantiate a new object each time the underlying value is changed.
However, it is important to respect the immutability of an object for encapsulation purposes.
When a software module has created a variable number (using its variable nature to alter its value during some processing algorithm, say) it may want to expose that number externally. At this point the number needs to lose its variability, to prevent other software modules changing its value.
It is for this reason the variable number class provides the functionality to secure the number, closing the object to any further change. Effectively making it read-only.

Since:
1.1
Version:
$Revision: 1.1 $
Author:
Stephen Battey
See Also:
Serialized Form

Field Summary
private  boolean secure
          Flag set once the value has been fixed, effectively making the number immutable.
 
Constructor Summary
VariableNumber()
           
 
Method Summary
 boolean isSecure()
          Check if the value of this number has been secured.
 void secure()
          Secure the value of this number.
 void setValue(byte value)
          Set the value of this number.
 void setValue(double value)
          Set the value of this number.
 void setValue(float value)
          Set the value of this number.
 void setValue(int value)
          Set the value of this number.
 void setValue(long value)
          Set the value of this number.
 void setValue(short value)
          Set the value of this number.
protected  void setValueInternal(byte value)
          An internal set command.
protected abstract  void setValueInternal(double value)
          An internal set command.
protected abstract  void setValueInternal(float value)
          An internal set command.
protected abstract  void setValueInternal(int value)
          An internal set command.
protected abstract  void setValueInternal(long value)
          An internal set command.
protected  void setValueInternal(short value)
          An internal set command.
protected  void verifyNotSecure()
          Check the secure flag, throwing an exception if the number is not in a variable state.
 
Methods inherited from class java.lang.Number
byteValue, doubleValue, floatValue, intValue, longValue, shortValue
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

secure

private boolean secure
Flag set once the value has been fixed, effectively making the number immutable.

Constructor Detail

VariableNumber

public VariableNumber()
Method Detail

secure

public void secure()
Secure the value of this number.
Once this method has been called, the value is fixed. Calls to the setValue methods will cause an exception.

See Also:
setValue(int), setValue(long), setValue(float), setValue(double), setValue(short), setValue(byte)

isSecure

public boolean isSecure()
Check if the value of this number has been secured.
This method can be used to guard against exceptions being thrown by the setValue methods.

Returns:
true if the value of this number can no longer be changed
See Also:
setValue(int), setValue(long), setValue(float), setValue(double), setValue(short), setValue(byte)

verifyNotSecure

protected void verifyNotSecure()
Check the secure flag, throwing an exception if the number is not in a variable state.

Throws:
IllegalStateException - if this number is no longer in a variable state

setValue

public final void setValue(int value)
Set the value of this number.
If the number is secure an exception will be thrown.

Parameters:
value - the new value for this number
Throws:
IllegalStateException - if this number is no longer in a variable state
See Also:
secure, secure()

setValue

public final void setValue(long value)
Set the value of this number.
If the number is secure an exception will be thrown.

Parameters:
value - the new value for this number
Throws:
IllegalStateException - if this number is no longer in a variable state
See Also:
secure, secure()

setValue

public final void setValue(float value)
Set the value of this number.
If the number is secure an exception will be thrown.

Parameters:
value - the new value for this number
Throws:
IllegalStateException - if this number is no longer in a variable state
See Also:
secure, secure()

setValue

public final void setValue(double value)
Set the value of this number.
If the number is secure an exception will be thrown.

Parameters:
value - the new value for this number
Throws:
IllegalStateException - if this number is no longer in a variable state
See Also:
secure, secure()

setValue

public final void setValue(short value)
Set the value of this number.
If the number is secure an exception will be thrown.

Parameters:
value - the new value for this number
Throws:
IllegalStateException - if this number is no longer in a variable state
See Also:
secure, secure()

setValue

public final void setValue(byte value)
Set the value of this number.
If the number is secure an exception will be thrown.

Parameters:
value - the new value for this number
Throws:
IllegalStateException - if this number is no longer in a variable state
See Also:
secure, secure()

setValueInternal

protected abstract void setValueInternal(int value)
An internal set command.
This function will only be called if the number is still in a variable state.

Parameters:
value - the new value for this number

setValueInternal

protected abstract void setValueInternal(long value)
An internal set command.
This function will only be called if the number is still in a variable state.

Parameters:
value - the new value for this number

setValueInternal

protected abstract void setValueInternal(float value)
An internal set command.
This function will only be called if the number is still in a variable state.

Parameters:
value - the new value for this number

setValueInternal

protected abstract void setValueInternal(double value)
An internal set command.
This function will only be called if the number is still in a variable state.

Parameters:
value - the new value for this number

setValueInternal

protected void setValueInternal(short value)
An internal set command.
This function will only be called if the number is still in a variable state.

Parameters:
value - the new value for this number

setValueInternal

protected void setValueInternal(byte value)
An internal set command.
This function will only be called if the number is still in a variable state.

Parameters:
value - the new value for this number