sleep.runtime
Class Scalar

java.lang.Object
  extended by sleep.runtime.Scalar
All Implemented Interfaces:
java.io.Serializable

public class Scalar
extends java.lang.Object
implements java.io.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, Serialized Form

Field Summary
protected  ScalarArray array
           
protected  ScalarHash hash
           
protected  ScalarType value
           
 
Constructor Summary
Scalar()
           
 
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.
 java.lang.Object identity()
          returns an identity value for this scalar.
 int intValue()
          the int value of this scalar
 long longValue()
          the long value of this scalar
 java.lang.Object objectValue()
          the object value of this scalar
 boolean sameAs(Scalar other)
          compares two scalars in terms of their identity.
 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
 java.lang.String stringValue()
          the string value of this scalar
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

value

protected ScalarType value

array

protected ScalarArray array

hash

protected ScalarHash hash
Constructor Detail

Scalar

public Scalar()
Method Detail

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.


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


stringValue

public java.lang.String stringValue()
the string value of this scalar


intValue

public int intValue()
the int value of this scalar


doubleValue

public double doubleValue()
the double value of this scalar


longValue

public long longValue()
the long value of this scalar


objectValue

public java.lang.Object objectValue()
the object value of this scalar


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


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


identity

public java.lang.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).


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


toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

setValue

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