sleep.runtime

Class Scalar

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 ScalarArrayarray
protected ScalarHashhash
protected ScalarTypevalue
Method Summary
doubledoubleValue()
the double value of this scalar
ScalarTypegetActualValue()
Returns the actual non-array/non-hash value this scalar contains.
ScalarArraygetArray()
returns a scalar array referenced by this scalar iff this scalar contains an array reference
ScalarHashgetHash()
returns a scalar hash referenced by this scalar iff this scalar contains a hash reference
ScalarTypegetValue()
Returns the container for the scalars value.
Objectidentity()
returns an identity value for this scalar. the identity value is used in set operations. basically any scalar values that are handled by reference (object,s arrays, and hashes) use their reference as their identity. other values used their string value as their identity (doubles that do not have a decimal point will be converted to longs).
intintValue()
the int value of this scalar
longlongValue()
the long value of this scalar
ObjectobjectValue()
the object value of this scalar
booleansameAs(Scalar other)
compares two scalars in terms of their identity. scalars that hold references (array, object, and hash) are compared by reference where other values are compared by their string value. doubles with a round value will be converted to a long
voidsetValue(ScalarType _value)
set the value of this scalar container to a scalar value of some type
voidsetValue(ScalarArray _array)
set the value of this scalar container to a scalar array
voidsetValue(ScalarHash _hash)
set the value of this scalar container to a scalar hash
voidsetValue(Scalar newValue)
clones the value from the specified scalar and gives this scalar a copy of the value
StringstringValue()
the string value of this scalar
StringtoString()

Field Detail

array

protected ScalarArray array

hash

protected ScalarHash hash

value

protected ScalarType value

Method Detail

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

identity

public Object identity()
returns an identity value for this scalar. the identity value is used in set operations. basically any scalar values that are handled by reference (object,s arrays, and hashes) use their reference as their identity. other values used their string value as their identity (doubles that do not have a decimal point will be converted to longs).

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

sameAs

public boolean sameAs(Scalar other)
compares two scalars in terms of their identity. scalars that hold references (array, object, and hash) are compared by reference where other values are compared by their string value. doubles with a round value will be converted to a long

setValue

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

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(Scalar newValue)
clones the value from the specified scalar and gives this scalar a copy of the value

stringValue

public String stringValue()
the string value of this scalar

toString

public String toString()