sleep.runtime

Class Scalar

Implemented Interfaces:
Serializable

public class Scalar
extends Object
implements Serializable

A scalar is the universal data type for sleep variables. Scalars can have numerical values of integer, double, or long. Scalars can have a string value. Scalars can also contain a reference to a scalar array, scalar hash, or a generic Java object.

Numerical and String values are stored as ScalarTypes. Arrays and Hashes are stored in ScalarArray and ScalarHash containers respectively.

Instantiating a Scalar

Instantiating a Scalar is most easily done using the sleep.runtime.SleepUtils class. The SleepUtils class contains several static methods for creating a Scalar object from data.

The general pattern for this is a SleepUtils.getScalar(data) methods. There are static getScalar() methods that take a double, int, long, Object, or a String as a parameter.

There are even methods for wrapping java data structures into a scalar array or scalar hash. Methods also exist to copy data from one scalar into another new scalar.

Examples:

 Scalar anInt   = SleepUtils.getScalar(3); // create an int scalar
 Scalar aDouble = SleepUtils.getScalar(4.5); // create a double scalar
 Scalar aString = SleepUtils.getScalar("hello"); // string scalar
 Scalar anArray = SleepUtils.getArrayWrapper(new LinkedList(); // array scalar
 

Working with Scalar Arrays

To add a value to a Scalar array:

 Scalar arrayScalar = SleepUtils.getArray(); // empty array
 arrayScalar.getArray().add(SleepUtils.getScalar("value"), 0);
 

To iterate through all of the values in a Scalar array:

 Iterator i = arrayScalar.getArray().scalarIterator();
 while (i.hasNext())
 {
     Scalar temp = (Scalar)i.next();
 }
 

Working with Scalar Hashes

To add a value to a Scalar hashtable:

 Scalar hashScalar = SleepUtils.getHashScalar(); // blank hashtable
 Scalar temp = hashScalar.getHash().getAt(SleepUtils.getScalar("key"));
 temp.setValue(SleepUtils.getScalar("value"));
 

The second line obtains a Scalar for "key". The returned Scalar is just a container. It is possible to set the value of the returned scalar using the setValue method.

Internally scalar values in sleep are passed by value. Methods like setValue inside of the Scalar class take care of copying the value. Externally though Scalar objects are passed by reference. When you call getAt() in the ScalarHash you are obtaining a reference to a Scalar inside of the hashtable. When you change the value of the Scalar you obtained, you change the value of the Scalar in the hashtable.

See Also:
SleepUtils, ScalarType, ScalarArray, ScalarHash

Field Summary

protected ScalarArray
array
protected ScalarHash
hash
protected ScalarType
value

Method Summary

double
doubleValue()
the double value of this scalar
ScalarType
getActualValue()
Returns the actual non-array/non-hash value this scalar contains.
ScalarArray
getArray()
returns a scalar array referenced by this scalar iff this scalar contains an array reference
ScalarHash
getHash()
returns a scalar hash referenced by this scalar iff this scalar contains a hash reference
ScalarType
getValue()
Returns the container for the scalars value.
int
intValue()
the int value of this scalar
long
longValue()
the long value of this scalar
Object
objectValue()
the object value of this scalar
void
setValue(Scalar newValue)
clones the value from the specified scalar and gives this scalar a copy of the value
void
setValue(ScalarArray _array)
set the value of this scalar container to a scalar array
void
setValue(ScalarHash _hash)
set the value of this scalar container to a scalar hash
void
setValue(ScalarType _value)
set the value of this scalar container to a scalar value of some type
String
stringValue()
the string value of this scalar
String
toString()

Field Details

array

protected ScalarArray array

hash

protected ScalarHash hash

value

protected ScalarType value

Method Details

doubleValue

public double doubleValue()
the double value of this scalar

getActualValue

public ScalarType getActualValue()
Returns the actual non-array/non-hash value this scalar contains. This is mainly for use by internal sleep classes that do not want to accidentally convert a hash/array to a string.

getArray

public ScalarArray getArray()
returns a scalar array referenced by this scalar iff this scalar contains an array reference

getHash

public ScalarHash getHash()
returns a scalar hash referenced by this scalar iff this scalar contains a hash reference

getValue

public ScalarType getValue()
Returns the container for the scalars value. If this is an array or hash scalar then they will be converted to a string scalar and returned. If this scalar is completely null then null will be returned which will mess up the interpreter somewhere

intValue

public int intValue()
the int value of this scalar

longValue

public long longValue()
the long value of this scalar

objectValue

public Object objectValue()
the object value of this scalar

setValue

public void setValue(Scalar newValue)
clones the value from the specified scalar and gives this scalar a copy of the value

setValue

public void setValue(ScalarArray _array)
set the value of this scalar container to a scalar array

setValue

public void setValue(ScalarHash _hash)
set the value of this scalar container to a scalar hash

setValue

public void setValue(ScalarType _value)
set the value of this scalar container to a scalar value of some type

stringValue

public String stringValue()
the string value of this scalar

toString

public String toString()