Package sleep.runtime
Class SleepUtils
- java.lang.Object
-
- sleep.runtime.SleepUtils
-
public class SleepUtils extends java.lang.ObjectThis class contains generalized utilities for instantiating/wrapping data into the sleep Scalar type. Included for free are methods for executing sleep blocks as well.- See Also:
Block,SleepClosure,Scalar,ScalarType,ScalarArray,ScalarHash
-
-
Field Summary
Fields Modifier and Type Field Description protected static ScalarTypenullScalara shared instance of the dreaded null scalar...static intSLEEP_RELEASEA date stamp of this Sleep release in YYYYMMDD formatstatic java.lang.StringSLEEP_VERSIONA string description of this Sleep release
-
Constructor Summary
Constructors Constructor Description SleepUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidaddKeyword(java.lang.String keyword)registers "keyword" as a keyword with the parser.static java.lang.Stringdescribe(java.util.Stack arguments)returns a comma separated list of descriptions of the scalars in the specified argument stack.static java.lang.Stringdescribe(Scalar scalar)returns a string description of the specified scalar.static ScalargetAccessOrderedHashScalar()returns an empty access ordered hashmap scalarstatic java.util.StackgetArgumentStack(java.util.Map pairs)Generate a java.util.Stack of sleep.bridges.KeyValuePair arguments from a Map.static ScalargetArrayScalar()returns a Scalar variable containing a SLEEP array as its value (everything in SLEEP is stored as a Scalarstatic ScalargetArrayScalar(ScalarArray value)Creates a scalar with the specified ScalarArray as the valuestatic ScalargetArrayWrapper(java.util.Collection dataStructure)An easy way to make your programs data structures available in the script environment.static ScalargetEmptyScalar()returns the null scalar, which will always be equal to 0, "", and null simultaneously.static SleepClosuregetFunctionFromScalar(Scalar value)extracts a callable Function from the passed in Scalar.static SleepClosuregetFunctionFromScalar(Scalar value, ScriptInstance script)extracts a callable Function from the passed in Scalar.static ScalargetHashScalar()returns an empty hashmap scalarstatic ScalargetHashScalar(ScalarHash value)Creates a scalar with the specified ScalarHash as the valuestatic ScalargetHashWrapper(java.util.Map dataStructure)An easy way to make your programs data structures available in the script environment.static IOObjectgetIOHandle(java.io.InputStream in, java.io.OutputStream out)creates an IO handle scalar suitable for use with the sleep IO API.static ScalargetIOHandleScalar(java.io.InputStream in, java.io.OutputStream out)creates an IO handle scalar suitable for use with the sleep IO API.static java.util.IteratorgetIterator(Scalar temp, ScriptInstance script)Returns a scalar iterator depending grabbed from the Scalar.static java.util.ListgetListFromArray(Scalar array)Generate a java.util.List from a scalar array.static java.util.ListgetListFromArray(ScalarArray array)Generate a java.util.List from a scalar array.static java.util.MapgetMapFromHash(Scalar map)Generate a java.util.Map from a scalar hash.static java.util.MapgetMapFromHash(ScalarHash map)Generate a java.util.Map from a scalar hash.static ScalargetOrderedHashScalar()returns an empty insertion ordered hashmap scalarstatic ScalargetScalar(boolean x)if x is true, the value will be 1, if x is false the value will be the empty scalarstatic ScalargetScalar(byte[] x)constructs a string scalar with value x interpreted as an array of unsigned bytesstatic ScalargetScalar(byte[] x, int length)constructs a string scalar with value x interpreted as an array of unsigned bytesstatic ScalargetScalar(double x)returns a double scalar with value xstatic ScalargetScalar(float x)returns a double scalar (coverted from the float) with value xstatic ScalargetScalar(int x)returns an int scalar with value xstatic ScalargetScalar(long x)returns a long scalar with value xstatic ScalargetScalar(short x)returns an int scalar (coverted from the short) with value xstatic ScalargetScalar(java.lang.Object x)returns an object scalar with value xstatic ScalargetScalar(java.lang.String x)returns a string scalar with value xstatic ScalargetScalar(Scalar x)Forces a copy of the value of the passed in scalar to be made.static booleanisEmptyScalar(Scalar value)returns true if the passed in scalar value is equivalent to the empty scalar or nullstatic booleanisFunctionScalar(Scalar value)Determines if the passed in scalar represents a "function" value.static booleanisTrueScalar(Scalar value)check if the scalar is true using Sleep's definition of truth.static java.lang.ObjectnewInstance(java.lang.Class initializeMe, SleepClosure closure, ScriptInstance owner)Creates a proxy instance of the specified class (limited to interfaces at this time) that is backed with the specified closurestatic java.lang.ObjectnewInstance(java.lang.Class initializeMe, Block code, ScriptInstance owner)Creates a proxy instance of the specified class (limited to interfaces at this time) that is backed with the specified block of code (made into a closure)static BlockParseCode(java.lang.String code)Parses the specified code into a runnable block.static voidremoveScalar(java.util.Iterator collection, Scalar value)Iterates over the specified collection and removes all items that are the same as the specified scalar value.static ScalarrunCode(SleepClosure closure, java.lang.String message, ScriptInstance script, java.util.Stack locals)"safely" runs a closure.static ScalarrunCode(Block code, ScriptEnvironment env)"safely" run a snippet of code.static ScalarrunCode(Function func, java.lang.String name, ScriptInstance script, java.util.Stack locals)"safely" runs a "Function" of code.static ScalarrunCode(ScriptInstance owner, Block code)"safely" run a snippet of code.static ScalarrunCode(ScriptInstance script, Block code, java.util.HashMap vars)"safely" run a snippet of code.
-
-
-
Field Detail
-
SLEEP_RELEASE
public static final int SLEEP_RELEASE
A date stamp of this Sleep release in YYYYMMDD format- See Also:
- Constant Field Values
-
SLEEP_VERSION
public static final java.lang.String SLEEP_VERSION
A string description of this Sleep release- See Also:
- Constant Field Values
-
nullScalar
protected static ScalarType nullScalar
a shared instance of the dreaded null scalar...
-
-
Method Detail
-
addKeyword
public static void addKeyword(java.lang.String keyword)
registers "keyword" as a keyword with the parser. This is a necessity if you have extra non standard bridges in sleep
-
ParseCode
public static Block ParseCode(java.lang.String code) throws YourCodeSucksException
Parses the specified code into a runnable block.- Throws:
YourCodeSucksException
-
removeScalar
public static void removeScalar(java.util.Iterator collection, Scalar value)Iterates over the specified collection and removes all items that are the same as the specified scalar value. Certain scalars (ints, doubles, etc.) are compared by string representation where as others (Object, Hash, Array) are compared by reference.
-
runCode
public static Scalar runCode(Block code, ScriptEnvironment env)
"safely" run a snippet of code. The snippet is executed as if it was an inline function.- Parameters:
code- the block of code we want to executeenv- the environment to run the code in- Returns:
- the scalar returned by the executed code (if their is a return value), null otherwise.
-
runCode
public static Scalar runCode(SleepClosure closure, java.lang.String message, ScriptInstance script, java.util.Stack locals)
"safely" runs a closure.- Parameters:
closure- the SleepClosure object we want to executemessage- the $0 parameter (aka the message) to pass to this closure objectscript- the script we want to execute the function withinlocals- a stack of scalars representing the arguments to this Function (first arg on top)- Returns:
- the scalar returned by the executed code or the sleep empty scalar if there is no return value (never returns null)
-
runCode
public static Scalar runCode(Function func, java.lang.String name, ScriptInstance script, java.util.Stack locals)
"safely" runs a "Function" of code.- Parameters:
func- the Function object we want to executename- the name of the function we are executing (can be anything, depending on the function object)script- the script we want to execute the function withinlocals- a stack of scalars representing the arguments to this Function (first arg on top)- Returns:
- the scalar returned by the executed code or the sleep empty scalar if there is no return value (never returns null)
-
runCode
public static Scalar runCode(ScriptInstance script, Block code, java.util.HashMap vars)
"safely" run a snippet of code. The main thing this function does is clear the return value before returning the value to the caller. This is important because the return value (if there is one) would not get cleared otherwise. Kind of important.- Parameters:
script- the owning script instance of this block of codecode- the block of code we want to executevars- a hashmap containing Scalar objects that should be installed into the local scope. The keys should be Strings representing the $names for each of the Scalar variables. This value can be null.- Returns:
- the scalar returned by the executed code (if their is a return value), null otherwise.
-
runCode
public static Scalar runCode(ScriptInstance owner, Block code)
"safely" run a snippet of code. The main thing this function does is clear the return value before returning the value to the caller. This is important because the return value (if there is one) would not get cleared otherwise. Kind of important.- Parameters:
owner- the owning script instance of this block of codecode- the block of code we want to execute- Returns:
- the scalar returned by the executed code (if their is a return value), null otherwise.
-
getArrayWrapper
public static Scalar getArrayWrapper(java.util.Collection dataStructure)
An easy way to make your programs data structures available in the script environment. Using this wrapper method will make the contents of your Set data structure available in a read only way to scripters using your program. Values your data structure returns are turned into Scalar strings using the toString() method of the object. If you need something more specific than this then take a look at the source code for sleep.runtime.SetWrapper which implements sleep.runtime.ScalarArray.
-
getHashWrapper
public static Scalar getHashWrapper(java.util.Map dataStructure)
An easy way to make your programs data structures available in the script environment. Using this wrapper method will make the contents of your Map'd data structure available in a read only way to scripters using your program. Keys are automatically turned into strings and values your data structure give back are turned into Scalar strings using the toString() method of the object. If you need something more specific than this then take a look at the source code for sleep.runtime.MapWrapper which implements sleep.runtime.ScalarHash.
-
getHashScalar
public static Scalar getHashScalar(ScalarHash value)
Creates a scalar with the specified ScalarHash as the value
-
getArrayScalar
public static Scalar getArrayScalar(ScalarArray value)
Creates a scalar with the specified ScalarArray as the value
-
getArrayScalar
public static Scalar getArrayScalar()
returns a Scalar variable containing a SLEEP array as its value (everything in SLEEP is stored as a Scalar
-
getMapFromHash
public static java.util.Map getMapFromHash(Scalar map)
Generate a java.util.Map from a scalar hash. Keys will be Java strings. Values will be the Java object equivalents of the data stored in the scalar hash.
-
getMapFromHash
public static java.util.Map getMapFromHash(ScalarHash map)
Generate a java.util.Map from a scalar hash. Keys will be Java strings. Values will be the Java object equivalents of the data stored in the scalar hash.
-
getArgumentStack
public static java.util.Stack getArgumentStack(java.util.Map pairs)
Generate a java.util.Stack of sleep.bridges.KeyValuePair arguments from a Map. Assumes the keys are Strings and the values are already Scalar values.
-
getIterator
public static java.util.Iterator getIterator(Scalar temp, ScriptInstance script)
Returns a scalar iterator depending grabbed from the Scalar. The scalar can contain an array, a function, or a java.util.Iterator object.
-
getListFromArray
public static java.util.List getListFromArray(Scalar array)
Generate a java.util.List from a scalar array. Values will be the Java object equivalents of the data stored in the scalar array.
-
getListFromArray
public static java.util.List getListFromArray(ScalarArray array)
Generate a java.util.List from a scalar array. Values will be the Java object equivalents of the data stored in the scalar array.
-
getEmptyScalar
public static Scalar getEmptyScalar()
returns the null scalar, which will always be equal to 0, "", and null simultaneously. The instance of the null scalar is shared since the null scalar "value" is not modifiable.
-
isEmptyScalar
public static boolean isEmptyScalar(Scalar value)
returns true if the passed in scalar value is equivalent to the empty scalar or null
-
isFunctionScalar
public static boolean isFunctionScalar(Scalar value)
Determines if the passed in scalar represents a "function" value. True iff the scalar contains a closure reference.
-
getFunctionFromScalar
public static SleepClosure getFunctionFromScalar(Scalar value)
extracts a callable Function from the passed in Scalar. Returns null if value does not contain a function. Calling isFunctionScalar before this method is highly recommended.
-
getFunctionFromScalar
public static SleepClosure getFunctionFromScalar(Scalar value, ScriptInstance script)
extracts a callable Function from the passed in Scalar. The passed in Scalar can be either a SleepClosure scalar or a string scalar specifying a function name. This method exists for backwards compatability of old jIRCii scripts.
-
getIOHandleScalar
public static Scalar getIOHandleScalar(java.io.InputStream in, java.io.OutputStream out)
creates an IO handle scalar suitable for use with the sleep IO API. The passed in streams can each be null if necessary.
-
getIOHandle
public static IOObject getIOHandle(java.io.InputStream in, java.io.OutputStream out)
creates an IO handle scalar suitable for use with the sleep IO API. The passed in streams can each be null if necessary.
-
newInstance
public static java.lang.Object newInstance(java.lang.Class initializeMe, SleepClosure closure, ScriptInstance owner)Creates a proxy instance of the specified class (limited to interfaces at this time) that is backed with the specified closure
-
newInstance
public static java.lang.Object newInstance(java.lang.Class initializeMe, Block code, ScriptInstance owner)Creates a proxy instance of the specified class (limited to interfaces at this time) that is backed with the specified block of code (made into a closure)
-
describe
public static java.lang.String describe(java.util.Stack arguments)
returns a comma separated list of descriptions of the scalars in the specified argument stack. This is used by the debugging mechanism to format arguments to strings based on their scalar type.
-
describe
public static java.lang.String describe(Scalar scalar)
returns a string description of the specified scalar. Used by debugging mechanism to format scalars based on their value type, i.e. strings are enclosed in single quotes, objects in brackets, $null is displayed as $null, etc.
-
getHashScalar
public static Scalar getHashScalar()
returns an empty hashmap scalar
-
getOrderedHashScalar
public static Scalar getOrderedHashScalar()
returns an empty insertion ordered hashmap scalar
-
getAccessOrderedHashScalar
public static Scalar getAccessOrderedHashScalar()
returns an empty access ordered hashmap scalar
-
getScalar
public static Scalar getScalar(int x)
returns an int scalar with value x
-
getScalar
public static Scalar getScalar(short x)
returns an int scalar (coverted from the short) with value x
-
getScalar
public static Scalar getScalar(float x)
returns a double scalar (coverted from the float) with value x
-
getScalar
public static Scalar getScalar(double x)
returns a double scalar with value x
-
getScalar
public static Scalar getScalar(Scalar x)
Forces a copy of the value of the passed in scalar to be made. Sleep scalars in general are passed by *value*. When passing a scalar, a new scalar should be made with a copy of the old scalars value. Object scalars are passed by reference but this copying mechanism handles that. If you are ever storing scalars in a data structure call this method to get a copy. Otherwise chaos might ensue.
-
getScalar
public static Scalar getScalar(long x)
returns a long scalar with value x
-
getScalar
public static Scalar getScalar(byte[] x)
constructs a string scalar with value x interpreted as an array of unsigned bytes
-
getScalar
public static Scalar getScalar(byte[] x, int length)
constructs a string scalar with value x interpreted as an array of unsigned bytes
-
getScalar
public static Scalar getScalar(java.lang.String x)
returns a string scalar with value x
-
getScalar
public static Scalar getScalar(java.lang.Object x)
returns an object scalar with value x
-
getScalar
public static Scalar getScalar(boolean x)
if x is true, the value will be 1, if x is false the value will be the empty scalar
-
isTrueScalar
public static boolean isTrueScalar(Scalar value)
check if the scalar is true using Sleep's definition of truth. A scalar is considered true if it is not $null and it is not equal to a representation of 0
-
-