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 static PyArgumentMapempty()<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.ObjectgetArg(java.lang.String name)java.lang.ObjectgetArg(java.lang.String name, java.lang.Object defaultValue)java.lang.BooleangetBooleanArg(java.lang.String name)java.lang.BooleangetBooleanArg(java.lang.String name, java.lang.Boolean def)java.util.DategetDateArg(java.lang.String name)java.util.DategetDateArg(java.lang.String name, java.util.Date def)java.lang.DoublegetDoubleArg(java.lang.String name)java.lang.DoublegetDoubleArg(java.lang.String name, java.lang.Double def)java.util.Optional<java.lang.Boolean>getIfBoolean(java.lang.String name)Getnameif and only if it is already a Boolean; does not perform coercionjava.util.Optional<java.lang.Double>getIfDouble(java.lang.String name)Getnameif and only if it is already a Double; does not perform coercionjava.util.Optional<java.lang.Float>getIfFloat(java.lang.String name)Getnameif and only if it is already a Float; does not perform coercionjava.util.Optional<java.lang.Integer>getIfInteger(java.lang.String name)Getnameif and only if it is already an Integer; does not perform coercionjava.util.Optional<java.lang.Long>getIfLong(java.lang.String name)Getnameif and only if it is already a Long; does not perform coercionjava.util.Optional<org.python.core.PyObject>getIfPyObject(java.lang.String name)Getnameif and only if it is already a PyObject; does not perform coercionjava.util.Optional<java.lang.String>getIfString(java.lang.String name)Getnameif and only if it is already a String; does not perform coercionjava.lang.IntegergetIntArg(java.lang.String name)java.lang.IntegergetIntArg(java.lang.String name, java.lang.Integer def)java.lang.LonggetLongArg(java.lang.String name)java.lang.LonggetLongArg(java.lang.String name, java.lang.Long def)java.lang.StringgetStringArg(java.lang.String name)java.lang.StringgetStringArg(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 PyArgumentMapinterpretPyArgs(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 PyArgumentMapinterpretPyArgs(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
-
empty
public static PyArgumentMap empty()
-
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
nullif 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
nullif 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
nullif 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
nullif 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
nullif 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
nullif 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
nullif 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
defaultValueif 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)
Getnameif 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)
Getnameif 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)
Getnameif 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)
Getnameif 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)
Getnameif 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)
Getnameif 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)
Getnameif and only if it is already a PyObject; does not perform coercion
-
-