public final class Predefined extends java.lang.Object
Predefined
provides the module of predefined functions in Golo. The provided module is imported by
default.Modifier and Type | Method and Description |
---|---|
static java.lang.Object[] |
array(java.lang.Object... values)
array constructor.
|
static java.lang.Class<?> |
arrayTypeOf(java.lang.Object klass)
Returns an array class given a type class.
|
static java.lang.Object |
asFunctionalInterface(java.lang.Object type,
java.lang.Object func)
Turns a function reference into an instance of a Java 8 functional interface.
|
static java.lang.Object |
asInterfaceInstance(java.lang.Object interfaceClass,
java.lang.Object target)
Turns a function reference into an instance of a single-method interface.
|
static java.math.BigDecimal |
bigDecimalValue(java.lang.Object obj)
Gives the BigDecimal value of some number or String object.
|
static java.math.BigInteger |
bigIntegerValue(java.lang.Object obj)
Gives the BigInteger value of some number or String object.
|
static java.lang.Object |
box(java.lang.Object obj)
Returns a box containing the given object.
|
static java.lang.Object |
charValue(java.lang.Object obj)
Gives the Character value of some number or String object.
|
static java.lang.String |
currentDir()
Return current path of execution.
|
static java.lang.Object |
doubleValue(java.lang.Object obj)
Gives the Double value of some number or String object.
|
static boolean |
fileExists(java.lang.Object file)
Check if a file exists.
|
static java.lang.Object |
fileToText(java.lang.Object file,
java.lang.Object encoding)
Reads the content of a text file.
|
static java.lang.Object |
floatValue(java.lang.Object obj)
Gives the Float value of some number or String object.
|
static FunctionReference |
fun(java.lang.Object name,
java.lang.Object module)
Obtains the first reference to a function.
|
static FunctionReference |
fun(java.lang.Object name,
java.lang.Object module,
java.lang.Object arity)
Obtains the first reference to a function.
|
static FunctionReference |
fun(java.lang.Object name,
java.lang.Object module,
java.lang.Object arity,
java.lang.Object varargs)
Obtains a reference to a function.
|
static java.lang.Object |
intValue(java.lang.Object obj)
Gives the Integer value of some number or String object.
|
static boolean |
isArray(java.lang.Object object)
Checks if an object is a (JVM) array or not.
|
static boolean |
isClosure(java.lang.Object object)
Test whether an object is a closure or not.
|
static java.util.List<java.lang.Object> |
list(java.lang.Object... values)
Varargs version of a list constructor.
|
static java.lang.Object |
longValue(java.lang.Object obj)
Gives the Long value of some number or String object.
|
static java.util.Map<java.lang.Object,java.lang.Object> |
map(Tuple... items)
Varargs version of a map constructor.
|
static java.lang.Object |
mapEntry(java.lang.Object key,
java.lang.Object value)
Makes a key / value pair.
|
static java.lang.Object |
newTypedArray(java.lang.Class<?> type,
int length)
Makes a new typed JVM array.
|
static java.lang.Class<?> |
objectArrayType()
Function to obtain the
Object[].class reference. |
static void |
print(java.lang.Object obj)
Prints to the standard console.
|
static void |
println(java.lang.Object obj)
Prints to the standard console, including a newline.
|
static void |
raise(java.lang.Object message)
Raises a
RuntimeException with a message. |
static void |
raise(java.lang.Object message,
java.lang.Object cause)
Raises a
RuntimeException with a message and a cause. |
static java.lang.Object |
range(java.lang.Object to)
Makes an range object starting from the default value.
|
static java.lang.Object |
range(java.lang.Object from,
java.lang.Object to)
Makes an range object between two bounds.
|
static java.lang.String |
readln()
Reads the next line of characters from the console.
|
static java.lang.String |
readln(java.lang.String message)
Reads the next line of characters from the console.
|
static java.lang.String |
readPassword()
Reads a password from the console with echoing disabled.
|
static java.lang.String |
readPassword(java.lang.String message)
Reads a password from the console with echoing disabled.
|
static java.lang.Object |
removeByIndex(java.util.List<?> lst,
java.lang.Integer idx)
Removes an element of a List by index.
|
static void |
require(java.lang.Object condition,
java.lang.Object errorMessage)
Requires that a condition be
true . |
static void |
requireNotNull(java.lang.Object obj)
Requires that an object is not the
null reference. |
static java.lang.Object |
reversedRange(java.lang.Object from)
Makes an decreasing range object up to the default value.
|
static java.lang.Object |
reversedRange(java.lang.Object from,
java.lang.Object to)
Makes an decreasing range object between two bounds.
|
static char[] |
secureReadPassword()
Reads a password from the console with echoing disabled, returning an
char[] array. |
static char[] |
secureReadPassword(java.lang.String message)
Reads a password from the console with echoing disabled, returning an
char[] array. |
static java.util.Set<java.lang.Object> |
set(java.lang.Object... values)
Varargs version of a set constructor.
|
static void |
sleep(long ms)
Sleep on the current thread.
|
static java.lang.String |
str(java.lang.Object... args)
Create a
String by concatenating all arguments. |
static void |
textToFile(java.lang.Object text,
java.lang.Object file)
Writes some text to a file.
|
static void |
textToFile(java.lang.Object text,
java.lang.Object file,
java.lang.Object charset)
Writes some text to a file using the given
Charset . |
static Tuple |
tuple(java.lang.Object... values)
Tuple constructor.
|
static java.lang.String |
uuid()
Return a universally unique identifier as String.
|
static java.util.List<java.lang.Object> |
vector(java.lang.Object... values)
Varargs version of a vector constructor.
|
public static void raise(java.lang.Object message)
RuntimeException
with a message.message
- the exception description.java.lang.RuntimeException
- (always)public static void raise(java.lang.Object message, java.lang.Object cause)
RuntimeException
with a message and a cause.message
- the exception description.cause
- the exception cause.java.lang.RuntimeException
- (always)public static void print(java.lang.Object obj)
obj
- the object to be printed.public static void println(java.lang.Object obj)
obj
- obj the object to be printed.public static java.lang.String readln() throws java.io.IOException
java.io.IOException
public static java.lang.String readln(java.lang.String message) throws java.io.IOException
message
- displays a prompt message.java.io.IOException
public static java.lang.String readPassword() throws java.io.IOException
java.io.IOException
public static java.lang.String readPassword(java.lang.String message) throws java.io.IOException
message
- displays a prompt message.java.io.IOException
public static char[] secureReadPassword() throws java.io.IOException
char[]
array.java.io.IOException
public static char[] secureReadPassword(java.lang.String message) throws java.io.IOException
char[]
array.message
- displays a prompt message.java.io.IOException
public static void requireNotNull(java.lang.Object obj) throws java.lang.AssertionError
null
reference.obj
- the object to test against null
.java.lang.AssertionError
- if obj
is null
.public static void require(java.lang.Object condition, java.lang.Object errorMessage) throws java.lang.IllegalArgumentException, java.lang.AssertionError
true
.condition
- the condition, must be a Boolean
.errorMessage
- the error message, must be a String
.java.lang.IllegalArgumentException
- if the arguments are of the wrong type.java.lang.AssertionError
- if condition
is false
.public static java.lang.Object newTypedArray(java.lang.Class<?> type, int length)
java.lang.reflect.Array.newInstance
.type
- the array type.length
- the array length.public static java.lang.Object range(java.lang.Object from, java.lang.Object to)
java.lang.Collection
(immutable), so they can be used in Golo foreach
loops.from
- the lower-bound (inclusive) as an Integer
, Long
, Character
or BigInteger
.to
- the upper-bound (exclusive) as an Integer
, Long
, Character
or BigInteger
.Collection
public static java.lang.Object range(java.lang.Object to)
The default value is 0 for numbers and 'A' for chars.
to
- the upper-bound (exclusive) as an Integer
, Long
, Character
or BigInteger
.range(java.lang.Object, java.lang.Object)
public static java.lang.Object reversedRange(java.lang.Object from, java.lang.Object to)
java.lang.Collection
, so
they can be used in Golo foreach
loops.from
- the upper-bound (inclusive) as an Integer
, Long
, Character
or BigInteger
.to
- the lower-bound (exclusive) as an Integer
, Long
, Character
or BigInteger
.range(java.lang.Object, java.lang.Object)
public static java.lang.Object reversedRange(java.lang.Object from)
The default value is 0 for numbers and 'A' for chars.
from
- the upper-bound (inclusive) as an Integer
, Long
, Character
or BigInteger
.reversedRange(java.lang.Object, java.lang.Object)
,
range(java.lang.Object, java.lang.Object)
public static java.lang.Object mapEntry(java.lang.Object key, java.lang.Object value)
key
- the key.value
- the value.AbstractMap.SimpleEntry
AbstractMap.SimpleEntry
public static java.lang.Object asInterfaceInstance(java.lang.Object interfaceClass, java.lang.Object target)
interfaceClass
- the target single-method interface class.target
- the implementation function reference.interfaceClass
.MethodHandleProxies.asInterfaceInstance(Class, java.lang.invoke.MethodHandle)
public static java.lang.Object asFunctionalInterface(java.lang.Object type, java.lang.Object func) throws java.lang.Throwable
type
- the functional interface class.func
- the implementation function.type
.java.lang.Throwable
- if the adaptation fails.GoloAdapter
public static boolean isClosure(java.lang.Object object)
object
- the object.true
if object
is an instance of FunctionReference
false
otherwise.public static FunctionReference fun(java.lang.Object name, java.lang.Object module, java.lang.Object arity, java.lang.Object varargs) throws java.lang.Throwable
name
- the function name.module
- the function enclosing module (a Java class).arity
- the function arity, where a negative value means that any arity will do.varargs
- if the functions has variable arity.java.lang.NoSuchMethodException
- if the target function could not be found.java.lang.IllegalArgumentException
- if the argument types are not of types (String, Class, Integer)
.java.lang.Throwable
- if an error occurs.public static FunctionReference fun(java.lang.Object name, java.lang.Object module, java.lang.Object arity) throws java.lang.Throwable
This is the same as calling fun(name, module, arity, false)
.
java.lang.Throwable
fun(Object, Object, Object, Object)
public static FunctionReference fun(java.lang.Object name, java.lang.Object module) throws java.lang.Throwable
This is the same as calling fun(name, module, -1)
.
java.lang.Throwable
fun(Object, Object, Object)
public static java.lang.Object fileToText(java.lang.Object file, java.lang.Object encoding) throws java.lang.Throwable
file
- the file to read from as an instance of either String
, File
or Path
.encoding
- the file encoding as a String
or Charset
.String
.java.lang.Throwable
public static void textToFile(java.lang.Object text, java.lang.Object file) throws java.lang.Throwable
"-"
, the content is written to standard output.text
- the text to write.file
- the file to write to as an instance of either String
, File
or Path
.java.lang.Throwable
public static void textToFile(java.lang.Object text, java.lang.Object file, java.lang.Object charset) throws java.lang.Throwable
Charset
.
The file and parents directories are created if they does not exist. The file is overwritten if it already exists. If the file is "-"
, the content is written to standard output.text
- the text to write.file
- the file to write to as an instance of either String
, File
or Path
.charset
- the charset to encode the text in.java.lang.Throwable
public static boolean fileExists(java.lang.Object file)
file
- the file to read from as an instance of either String
, File
or Path
.public static java.lang.String currentDir() throws java.lang.Throwable
java.lang.Throwable
public static void sleep(long ms) throws java.lang.InterruptedException
ms
- time in milliseconds.java.lang.InterruptedException
- in case the thread gets interrupted.public static java.lang.String uuid()
public static boolean isArray(java.lang.Object object)
object
- the object to check.true
if object
is an array, false
otherwise or if null
.public static java.lang.Class<?> objectArrayType()
Object[].class
reference.Object[].class
public static java.lang.Class<?> arrayTypeOf(java.lang.Object klass) throws java.lang.ClassNotFoundException
klass
- the array type.klass
, i.e., klass[]
.java.lang.ClassNotFoundException
- if the type could not be found.public static java.lang.Object charValue(java.lang.Object obj) throws java.lang.IllegalArgumentException
obj
- a boxed number or String value.java.lang.IllegalArgumentException
- if obj
is not a number or a String.public static java.lang.Object intValue(java.lang.Object obj) throws java.lang.IllegalArgumentException
obj
- a boxed number or String value.java.lang.IllegalArgumentException
- if obj
is not a number or a String.public static java.lang.Object longValue(java.lang.Object obj) throws java.lang.IllegalArgumentException
obj
- a boxed number or String value.java.lang.IllegalArgumentException
- if obj
is not a number or a String.public static java.lang.Object doubleValue(java.lang.Object obj) throws java.lang.IllegalArgumentException
obj
- a boxed number or String value.java.lang.IllegalArgumentException
- if obj
is not a number or a String.public static java.lang.Object floatValue(java.lang.Object obj) throws java.lang.IllegalArgumentException
obj
- a boxed number or String value.java.lang.IllegalArgumentException
- if obj
is not a number or a String.public static java.math.BigDecimal bigDecimalValue(java.lang.Object obj) throws java.lang.IllegalArgumentException
obj
- a boxed number or String value.java.lang.IllegalArgumentException
- if obj
is not a number or a String.public static java.math.BigInteger bigIntegerValue(java.lang.Object obj) throws java.lang.IllegalArgumentException
obj
- a boxed number or String value.java.lang.IllegalArgumentException
- if obj
is not a number or a String.public static java.lang.String str(java.lang.Object... args)
String
by concatenating all arguments.
For instance:
let s = str("The answer", " is ", 2 * 21)
This is functionally equivalent to:
let s = ["The answer", " is ", 2 * 21]: join("")
public static java.lang.Object removeByIndex(java.util.List<?> lst, java.lang.Integer idx)
lst
- the list to remove the element from.idx
- an Integer representing the index of the element to remove.java.lang.IndexOutOfBoundsException
- if idx
public static java.lang.Object box(java.lang.Object obj)
Useful whenever an explicit reference is needed, for instance to create a closure with internal mutable state:
function counter = |init| {
let current = box(init)
return -> current: getAndSet(current: get() + 1)
}
let c = counter(3)
c() # 3
c() # 4
c() # 5
obj
- the object to reference.java.util.concurrent.atomic.AtomicReference
instance wrapping the objectpublic static java.util.List<java.lang.Object> list(java.lang.Object... values)
public static java.util.Set<java.lang.Object> set(java.lang.Object... values)
public static java.lang.Object[] array(java.lang.Object... values)
public static java.util.List<java.lang.Object> vector(java.lang.Object... values)
public static Tuple tuple(java.lang.Object... values)