Class PyArgumentMap
- java.lang.Object
-
- java.util.AbstractMap<K,V>
-
- java.util.HashMap<java.lang.String,java.lang.Object>
-
- com.inductiveautomation.ignition.common.script.builtin.PyArgumentMap
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Cloneable
,java.util.Map<java.lang.String,java.lang.Object>
public class PyArgumentMap extends java.util.HashMap<java.lang.String,java.lang.Object>
- See Also:
- Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> java.util.Optional<T>
getAndCast(java.lang.String name, java.util.function.Function<java.lang.Object,T> castFunction)
Get a given argument from the argument map, without doing any automatic coercion.java.lang.Object
getArg(java.lang.String name)
java.lang.Object
getArg(java.lang.String name, java.lang.Object defaultValue)
java.lang.Boolean
getBooleanArg(java.lang.String name)
java.lang.Boolean
getBooleanArg(java.lang.String name, java.lang.Boolean def)
java.util.Date
getDateArg(java.lang.String name)
java.util.Date
getDateArg(java.lang.String name, java.util.Date def)
java.lang.Double
getDoubleArg(java.lang.String name)
java.lang.Double
getDoubleArg(java.lang.String name, java.lang.Double def)
java.util.Optional<java.lang.Boolean>
getIfBoolean(java.lang.String name)
Getname
if and only if it is already a Boolean; does not perform coercionjava.util.Optional<java.lang.Double>
getIfDouble(java.lang.String name)
Getname
if and only if it is already a Double; does not perform coercionjava.util.Optional<java.lang.Float>
getIfFloat(java.lang.String name)
Getname
if and only if it is already a Float; does not perform coercionjava.util.Optional<java.lang.Integer>
getIfInteger(java.lang.String name)
Getname
if and only if it is already an Integer; does not perform coercionjava.util.Optional<java.lang.Long>
getIfLong(java.lang.String name)
Getname
if and only if it is already a Long; does not perform coercionjava.util.Optional<org.python.core.PyObject>
getIfPyObject(java.lang.String name)
Getname
if and only if it is already a PyObject; does not perform coercionjava.util.Optional<java.lang.String>
getIfString(java.lang.String name)
Getname
if and only if it is already a String; does not perform coercionjava.lang.Integer
getIntArg(java.lang.String name)
java.lang.Integer
getIntArg(java.lang.String name, java.lang.Integer def)
java.lang.Long
getLongArg(java.lang.String name)
java.lang.Long
getLongArg(java.lang.String name, java.lang.Long def)
java.lang.String
getStringArg(java.lang.String name)
java.lang.String
getStringArg(java.lang.String name, java.lang.String def)
java.lang.String[]
getStringArray(java.lang.String name)
java.lang.String[]
getStringArray(java.lang.String name, java.lang.String[] def)
static PyArgumentMap
interpretPyArgs(org.python.core.PyObject[] args, java.lang.String[] keywords, java.lang.Class<?> callingClass, java.lang.String methodName)
Helper for scripting functions that want to use python's keyword-style function invocation.static PyArgumentMap
interpretPyArgs(org.python.core.PyObject[] args, java.lang.String[] keywords, java.lang.String[] expectedKeywords, java.lang.Class<?>[] expectedTypes)
-
Methods inherited from class java.util.HashMap
clear, clone, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, forEach, get, getOrDefault, isEmpty, keySet, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size, values
-
-
-
-
Method Detail
-
interpretPyArgs
public static PyArgumentMap interpretPyArgs(org.python.core.PyObject[] args, java.lang.String[] keywords, java.lang.Class<?> callingClass, java.lang.String methodName)
Helper for scripting functions that want to use python's keyword-style function invocation. This version uses reflection to look for the @KeywordArgs annotation to find the expected params and their types.
-
interpretPyArgs
public static PyArgumentMap interpretPyArgs(org.python.core.PyObject[] args, java.lang.String[] keywords, java.lang.String[] expectedKeywords, java.lang.Class<?>[] expectedTypes)
- Throws:
java.lang.ClassCastException
- if an argument failed to be coerced into the expected type based onexpectedTypes
-
getStringArray
@Nullable public java.lang.String[] getStringArray(java.lang.String name)
-
getStringArray
@Nullable public java.lang.String[] getStringArray(java.lang.String name, java.lang.String[] def)
-
getStringArg
@Nullable public java.lang.String getStringArg(java.lang.String name)
- Parameters:
name
- keyword name of argument to retrieve. If not invoked with kwargs, will return the parameter at the expected numeric index.- Returns:
- the argument as a string, or
null
if the invocation passed null. - Throws:
java.lang.ClassCastException
- if the argument is present but cannot be coerced to the expected type.
-
getStringArg
@Nullable public java.lang.String getStringArg(java.lang.String name, java.lang.String def)
- Parameters:
name
- keyword name of argument to retrieve. If not invoked with kwargs, will return the parameter at the expected numeric index.def
- a fallback value to be used if the specified keyword was not present in the PyArgumentMap- Returns:
- the argument as a String, or the fallback value
def
. - Throws:
java.lang.ClassCastException
- if the argument is present but cannot be coerced to the expected type.
-
getIntArg
@Nullable public java.lang.Integer getIntArg(java.lang.String name)
- Parameters:
name
- keyword name of argument to retrieve. If not invoked with kwargs, will return the parameter at the expected numeric index.- Returns:
- the argument as an Integer, or
null
if the invocation passed null. - Throws:
java.lang.ClassCastException
- if the argument is present but cannot be coerced to the expected type.
-
getIntArg
@Nullable public java.lang.Integer getIntArg(java.lang.String name, java.lang.Integer def)
- Parameters:
name
- keyword name of argument to retrieve. If not invoked with kwargs, will return the parameter at the expected numeric index.def
- a fallback value to be used if the specified keyword was not present in the PyArgumentMap- Returns:
- the argument as an Integer, or the fallback value
def
. - Throws:
java.lang.ClassCastException
- if the argument is present but cannot be coerced to the expected type.
-
getLongArg
@Nullable public java.lang.Long getLongArg(java.lang.String name)
- Parameters:
name
- keyword name of argument to retrieve. If not invoked with kwargs, will return the parameter at the expected numeric index.- Returns:
- the argument as a Long, or
null
if the invocation passed null. - Throws:
java.lang.ClassCastException
- if the argument is present but cannot be coerced to the expected type.
-
getLongArg
@Nullable public java.lang.Long getLongArg(java.lang.String name, java.lang.Long def)
- Parameters:
name
- keyword name of argument to retrieve. If not invoked with kwargs, will return the parameter at the expected numeric index.def
- a fallback value to be used if the specified keyword was not present in the PyArgumentMap- Returns:
- the argument as a Long, or the fallback value
def
. - Throws:
java.lang.ClassCastException
- if the argument is present but cannot be coerced to the expected type.
-
getBooleanArg
@Nullable public java.lang.Boolean getBooleanArg(java.lang.String name)
- Parameters:
name
- keyword name of argument to retrieve. If not invoked with kwargs, will return the parameter at the expected numeric index.- Returns:
- the argument as a Boolean, or
null
if the invocation passed null. - Throws:
java.lang.ClassCastException
- if the argument is present but cannot be coerced to the expected type.
-
getBooleanArg
@Nullable public java.lang.Boolean getBooleanArg(java.lang.String name, java.lang.Boolean def)
- Parameters:
name
- keyword name of argument to retrieve. If not invoked with kwargs, will return the parameter at the expected numeric index.def
- a fallback value to be used if the specified keyword was not present in the PyArgumentMap- Returns:
- the argument as a Boolean, or the fallback value
def
. - Throws:
java.lang.ClassCastException
- if the argument is present but cannot be coerced to the expected type.
-
getDoubleArg
@Nullable public java.lang.Double getDoubleArg(java.lang.String name)
- Parameters:
name
- keyword name of argument to retrieve. If not invoked with kwargs, will return the parameter at the expected numeric index.- Returns:
- the argument as a Double, or
null
if the invocation passed null. - Throws:
java.lang.ClassCastException
- if the argument is present but cannot be coerced to the expected type.
-
getDoubleArg
@Nullable public java.lang.Double getDoubleArg(java.lang.String name, java.lang.Double def)
- Parameters:
name
- keyword name of argument to retrieve. If not invoked with kwargs, will return the parameter at the expected numeric index.def
- a fallback value to be used if the specified keyword was not present in the PyArgumentMap- Returns:
- the argument as a Double, or the fallback value
def
. - Throws:
java.lang.ClassCastException
- if the argument is present but cannot be coerced to the expected type.
-
getDateArg
@Nullable public java.util.Date getDateArg(java.lang.String name)
- Parameters:
name
- keyword name of argument to retrieve. If not invoked with kwargs, will return the parameter at the expected numeric index.- Returns:
- the argument as a Date, or
null
if the invocation passed null. - Throws:
java.lang.ClassCastException
- if the argument is present but cannot be coerced to the expected type.
-
getDateArg
@Nullable public java.util.Date getDateArg(java.lang.String name, java.util.Date def)
- Parameters:
name
- keyword name of argument to retrieve. If not invoked with kwargs, will return the parameter at the expected numeric index.def
- a fallback value to be used if the specified keyword was not present in the PyArgumentMap- Returns:
- the argument as a Date, or the fallback value
def
. - Throws:
java.lang.ClassCastException
- if the argument is present but cannot be coerced to the expected type.
-
getArg
@Nullable public java.lang.Object getArg(java.lang.String name)
- Parameters:
name
- keyword name of argument to retrieve. If not invoked with kwargs, will return the parameter at the expected numeric index.- Returns:
- the argument without coercion, or
null
if the invocation passed null. - Throws:
java.lang.ClassCastException
- if the argument is present but cannot be coerced to the expected type.
-
getArg
@Nullable public java.lang.Object getArg(java.lang.String name, java.lang.Object defaultValue)
- Parameters:
name
- keyword name of argument to retrieve. If not invoked with kwargs, will return the parameter at the expected numeric index.defaultValue
- a fallback value to be used if the specified keyword was not present in the PyArgumentMap- Returns:
- the argument without coercion, or
defaultValue
if the map did not containname
- Throws:
java.lang.ClassCastException
- if the argument is present but cannot be coerced to the expected type.
-
getAndCast
public <T> java.util.Optional<T> getAndCast(java.lang.String name, java.util.function.Function<java.lang.Object,T> castFunction)
Get a given argument from the argument map, without doing any automatic coercion.- Parameters:
name
- argument to retrievecastFunction
- A function that converts an Object to typeT
. If caseFunction returns null or throws, getAndCast will return an empty Optional.- Returns:
- an Optional containing the result object, or nothing if the cast function failed.
-
getIfString
public java.util.Optional<java.lang.String> getIfString(java.lang.String name)
Getname
if and only if it is already a String; does not perform coercion
-
getIfBoolean
public java.util.Optional<java.lang.Boolean> getIfBoolean(java.lang.String name)
Getname
if and only if it is already a Boolean; does not perform coercion
-
getIfInteger
public java.util.Optional<java.lang.Integer> getIfInteger(java.lang.String name)
Getname
if and only if it is already an Integer; does not perform coercion
-
getIfLong
public java.util.Optional<java.lang.Long> getIfLong(java.lang.String name)
Getname
if and only if it is already a Long; does not perform coercion
-
getIfFloat
public java.util.Optional<java.lang.Float> getIfFloat(java.lang.String name)
Getname
if and only if it is already a Float; does not perform coercion
-
getIfDouble
public java.util.Optional<java.lang.Double> getIfDouble(java.lang.String name)
Getname
if and only if it is already a Double; does not perform coercion
-
getIfPyObject
public java.util.Optional<org.python.core.PyObject> getIfPyObject(java.lang.String name)
Getname
if and only if it is already a PyObject; does not perform coercion
-
-