sleep.runtime
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 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
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(); }
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. |
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). |
int | intValue() the int value of this scalar |
long | longValue() the long value of this scalar |
Object | objectValue() the object value of this scalar |
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 |
void | setValue(ScalarType _value) set the value of this scalar container to a scalar value of some type |
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(Scalar newValue) clones the value from the specified scalar and gives this scalar a copy of the value |
String | stringValue() the string value of this scalar |
String | toString() |