Class TypeUtilities
java.lang.Object
com.inductiveautomation.ignition.common.TypeUtilities
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
protected static class
Initializes most basic classes. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
static final Comparator<String>
static final String
A fully specified date & time, without timezone. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Object
Attempts to coerce the Objectvalue
into an instance of the Classtype
.static Date
coerceDateForLocale
(String value, Locale locale) static Object
coerceForLocale
(Object value, Class<?> target, Locale valueLocale) A version of coerce intended to go from String to Number, where the source string is formatted for a certain locale.static Object
coerceFromDataset
(Dataset dataset, Class<?> destType) static <T> T
coerceGeneric
(Object value, Class<? extends T> destType) Performs the same operation as coerce, but with generic typing.static Object
coerceLocaleSafe
(String str, Class<?> type) Special version of coerce that has explicit handling for String-to-Number coersions that don't depend on the current locale.static Object
coerceNullSafe
(Object value, Class<?> destType) Like coerce, except that if value is null, null will be returned instead of the "default" value for the desired type.static Object
coerceNumberForLocale
(Object value, Class<?> destType, Locale locale) Delegates tocoerce(Object, Class)
if the destType is a number and the value is null or a blank String, otherwise delegates tocoerceForLocale(Object, Class, Locale)
static Object
coerceNumberNullSafe
(Object value, Class<?> destType, Locale locale) Like coerce, except that if value is null, null will be returned instead of the "default" value for the desired type.static Object
coerceTimestamp
(Timestamp value, Class<?> destType) static String
colorToHex
(Color c) Returns the color as a web-compatible hex string.static <T extends Comparable<T>>
intcompareNullHigh
(T c1, T c2) Null-safe compare.static <T extends Comparable<T>>
intcompareNullLow
(T c1, T c2) Null-safe compare.static Dataset
datasetFromJSON
(JSONObject json) static Dataset
datasetFromJsonString
(String jsonStr) Use when you only have JSON in the form of a Stringstatic <T> T[]
datasetToArray
(Dataset dataset, Class<T> componentType) static com.inductiveautomation.ignition.common.gson.JsonElement
datasetToGson
(Dataset data) static com.inductiveautomation.ignition.common.gson.JsonElement
datasetToGson
(Dataset data, com.inductiveautomation.ignition.common.gson.Gson gson) static JSONObject
datasetToJSON
(Dataset data) static boolean
deepEquals
(Object o1, Object o2, boolean checkArrayTypes) Likeequals(Object, Object)
, but if the objects are arrays it delegates toObjects.deepEquals(Object, Object)
.static boolean
equals
(double val1, double val2, double tolerance) A special form of equals for doubles, that takes a provider tolerance into account.static boolean
This implementation of equals is useful in the following ways: Null-safe: null==null Number-safe: (new Long(0))==(new Integer(0)) and Boolean.TRUE == new Integer(1) Enum-safe: enums will be compared to their integer equivalent or their name Other than that, it delegates to the argument's .equals functions.static boolean
equalsIgnoreCase
(Object o1, Object o2) Equivalent semantics toequals(Object, Object)
, but if both arguments are Strings, case insensitive equality will be usedstatic Object
fromString
(String value, Class<?> dest, Locale locale) A special version of coerce, knowing that the source is a string in the given locale.static Color
getColorFromString
(String color) Tries to parse a Color from the given string.static <T> T
getFirstOrNull
(List<T> list) Takes a list and returns the first element.static Object
getInitValueForClass
(Class<?> c) static String
getLastNameComponent
(String name) Tags a string that represents a series of elements seperated by period (".") and returns the last portion.static Class<?>
getPrimitiveType
(Class<?> c) static Class<?>
getWrapperType
(Class<?> c) static org.python.core.PyObject
gsonToPy
(com.inductiveautomation.ignition.common.gson.JsonElement element) Attempt to encode a given JsonElement into base Python data structures - dictionaries for objects, lists for arrays, etc.static boolean
hasPrimitiveType
(Class<?> c) static boolean
hasValueChanged
(QualifiedValue currentValue, QualifiedValue previousValue, DataType expectedType, Double deadband) Checks whether the value and/or quality have change compared to the current value, taking into account the deadband.static boolean
isAssignable
(Class<?> dest, Class<?> source) This will return true if the setValue(Object o) knows how to coerce an object of type 'source' to an object of type 'dest'static boolean
True if cls == boolean.class || cls==Boolean.classstatic boolean
isDirectlyAssignable
(Class<?> dest, Class<?> source) Two classes are 'directly assignable' if reflection won't complain when you pass an object of type 'source' to a method which expects as an argument an object of type 'dest' For example, using reflection you can invoke the method setIntValue(int i) with an Integer object (instead of an 'int' primitive)static boolean
isFractional
(Class<?> cls) static boolean
Returns true if the String s is null or zero-lengthstatic boolean
Returns true if the class is boolean or is a proper number.static boolean
isPrimitive
(Class<?> c) Returns true if the class is one of the java primitive classesstatic boolean
isProperNumber
(Class<?> cls) Returns true if the given class represents a subclass of Number, or any of the numeric primitives.static boolean
Convenience for: !TypeUtilities.equals(o1,o2)static com.inductiveautomation.ignition.common.gson.JsonElement
pyToGson
(org.python.core.PyObject pyObject) Make a best effort to convert a PyObject into some Gson structure.static com.inductiveautomation.ignition.common.gson.JsonElement
pyToGson
(org.python.core.PyObject pyObject, com.inductiveautomation.ignition.common.gson.Gson customGson) Make a best effort to convert a PyObject into some Gson structure.static Object
pyToJava
(org.python.core.PyObject pyObject) Takes a PyObject and returns a java object, with a bit of additional logic.static QualityCode
setArrayValue
(Object arrayValue, Object newVal, int pos) Sets a value in an array, with support for QualifiedValues and conversion of the newVal to the target type.static Boolean
static Byte
static byte[]
toByteArray
(UUID uuid) static Color
static Dataset
static Date
static DocumentElement
toDocument
(Object value) static Double
static <T extends Enum>
Tstatic Float
static Integer
static Long
static Number
Coerces the value to a number.static Number
static Short
static String
Returns the string form of a value.static String
toStringLocalized
(Object value) static String
toStringLocalized
(Object value, Locale locale) Performs a localized stringifying/formatting of an object.static String
toStringOnlyNumberLocalized
(Object value, Locale locale) A version of toString that only localizes numbers.static UUID
toUUID
(byte[] barr) Uses a 16-byte long bytearray to create a new UUID.
-
Field Details
-
DATE_FORMAT_STRING
- See Also:
-
RFC_2339_LOCAL_DATETIME
A fully specified date & time, without timezone.
yyyy-MM-dd HH:mm:ss.SSS
- See Also:
-
NULL_SAFE_CASE_INSENSITIVE_ORDER
-
-
Constructor Details
-
TypeUtilities
public TypeUtilities()
-
-
Method Details
-
isProperNumber
Returns true if the given class represents a subclass of Number, or any of the numeric primitives. Booleans are not considered proper numbers. -
isPrimitive
Returns true if the class is one of the java primitive classes -
isFractional
-
isBoolean
True if cls == boolean.class || cls==Boolean.class -
isNumber
Returns true if the class is boolean or is a proper number. -
isAssignable
This will return true if the setValue(Object o) knows how to coerce an object of type 'source' to an object of type 'dest' -
isDirectlyAssignable
Two classes are 'directly assignable' if reflection won't complain when you pass an object of type 'source' to a method which expects as an argument an object of type 'dest' For example, using reflection you can invoke the method setIntValue(int i) with an Integer object (instead of an 'int' primitive) -
isNullOrEmpty
Returns true if the String s is null or zero-length -
compareNullHigh
Null-safe compare. Null is the largest value possible. -
compareNullLow
Null-safe compare. Null is the lowest value possible. -
hasValueChanged
public static boolean hasValueChanged(QualifiedValue currentValue, QualifiedValue previousValue, DataType expectedType, Double deadband) Checks whether the value and/or quality have change compared to the current value, taking into account the deadband. If expectedType is null, it will try to derive the type based on the value. Non numeric types will use straight equality. -
equals
This implementation of equals is useful in the following ways:- Null-safe: null==null
- Number-safe: (new Long(0))==(new Integer(0)) and Boolean.TRUE == new Integer(1)
- Enum-safe: enums will be compared to their integer equivalent or their name
-
deepEquals
Likeequals(Object, Object)
, but if the objects are arrays it delegates toObjects.deepEquals(Object, Object)
.If checkArrayTypes is
true
then o1 and o2 are not equal unless they are also the same type.Objects.deepEquals(Object, Object)
considers two empty arrays to be equal regardless of type.- Parameters:
o1
- an object.o2
- an object to be compared too2
for equality.checkArrayTypes
-true
if arrays must be of the same type to be considered equal.- Returns:
true
ifo1
equalso2
, with consideration for arrays and checkArrayTypes.
-
equals
public static boolean equals(double val1, double val2, double tolerance) A special form of equals for doubles, that takes a provider tolerance into account. -
neq
Convenience for: !TypeUtilities.equals(o1,o2) -
equalsIgnoreCase
Equivalent semantics toequals(Object, Object)
, but if both arguments are Strings, case insensitive equality will be used -
toStringLocalized
-
toStringLocalized
Performs a localized stringifying/formatting of an object.- null becomes empty string.
- Dates use the default DateFormat for the locale
- Numbers use NumberFormat.getInstance()
- Colors use their hex formatting
-
toString
Returns the string form of a value. Not adjusted for locale. Dates are returned in SQL format. Numbers are returned in java lang spec format. If the value is null, then null is returned. -
fromString
A special version of coerce, knowing that the source is a string in the given locale. -
toNumber
Coerces the value to a number. If the value is already a number, it is returned like it is. If it is null, null is returned. If the value is a string, it is attempted to be parsed into a Double. Otherwise, a ClassCastException is thrown. -
toNumber
@Nonnull public static Number toNumber(@Nullable String value, Locale locale) throws ClassCastException - Throws:
ClassCastException
-
coerceNumberNullSafe
@Nullable public static Object coerceNumberNullSafe(@Nullable Object value, Class<?> destType, Locale locale) throws ClassCastException Like coerce, except that if value is null, null will be returned instead of the "default" value for the desired type. Only numbers will be localized.- Throws:
ClassCastException
-
coerceNumberForLocale
public static Object coerceNumberForLocale(@Nullable Object value, Class<?> destType, Locale locale) Delegates tocoerce(Object, Class)
if the destType is a number and the value is null or a blank String, otherwise delegates tocoerceForLocale(Object, Class, Locale)
-
coerceNullSafe
@Nullable public static Object coerceNullSafe(@Nullable Object value, Class<?> destType) throws ClassCastException Like coerce, except that if value is null, null will be returned instead of the "default" value for the desired type.- Throws:
ClassCastException
-
coerceLocaleSafe
Special version of coerce that has explicit handling for String-to-Number coersions that don't depend on the current locale. If type isn't a float, double, byte, short, int, or long, it will fall back to normal coersion.- Throws:
ClassCastException
-
toStringOnlyNumberLocalized
A version of toString that only localizes numbers. If locale is null, the default locale will be used. -
coerceForLocale
public static Object coerceForLocale(Object value, Class<?> target, Locale valueLocale) throws ClassCastException A version of coerce intended to go from String to Number, where the source string is formatted for a certain locale. If locale is null, the default locale will be used.- Throws:
ClassCastException
-
coerceDateForLocale
- Throws:
ClassCastException
-
coerceGeneric
Performs the same operation as coerce, but with generic typing. -
coerceTimestamp
- Throws:
ClassCastException
-
coerceFromDataset
public static Object coerceFromDataset(Dataset dataset, Class<?> destType) throws ClassCastException - Throws:
ClassCastException
-
coerce
Attempts to coerce the Objectvalue
into an instance of the Classtype
. Ifvalue
is null, a default instance of the type is returned. If no coercion is known, a ClassCastException is thrown. If coercing to a String, it is better to call toStringLocalized(val,Locale), if the locale is known.- Throws:
ClassCastException
-
toEnum
-
toByteArray
-
toUUID
Uses a 16-byte long bytearray to create a new UUID.- Throws:
ClassCastException
- if the byte array isn't exactly 16 bytes longNullPointerException
- if barr is null
-
toInteger
- Throws:
ClassCastException
-
toLong
- Throws:
ClassCastException
-
toBool
- Throws:
ClassCastException
-
toDouble
- Throws:
ClassCastException
-
toFloat
- Throws:
ClassCastException
-
toByte
- Throws:
ClassCastException
-
toShort
- Throws:
ClassCastException
-
toDate
- Throws:
ClassCastException
-
toDataset
- Throws:
ClassCastException
-
toDocument
- Throws:
ClassCastException
-
getColorFromString
Tries to parse a Color from the given string. First it looks to see if the string is a named color in the NAMED_COLORS map. Then it tries to parse it as a web-style hex color, then it looks for RGB[A] values. If all of these attempts fail, a ClassCastException is thrown. -
toColor
-
colorToHex
Returns the color as a web-compatible hex string. Does not include the hash sign (#) in front. -
pyToJava
Takes a PyObject and returns a java object, with a bit of additional logic. For example, BigInt is converted to Long, and BigDecimal is converted to Double.- Parameters:
pyObject
- incoming value, if null, returns null.
-
gsonToPy
public static org.python.core.PyObject gsonToPy(@Nullable com.inductiveautomation.ignition.common.gson.JsonElement element) Attempt to encode a given JsonElement into base Python data structures - dictionaries for objects, lists for arrays, etc. UsesJsonUtilities.json2Java(JsonElement)
to unwrap primitive values before converting to PyObjects. -
pyToGson
public static com.inductiveautomation.ignition.common.gson.JsonElement pyToGson(@Nullable org.python.core.PyObject pyObject) Make a best effort to convert a PyObject into some Gson structure. Recurses, so make sure your data structure is sane.- Parameters:
pyObject
- object to convert
-
pyToGson
public static com.inductiveautomation.ignition.common.gson.JsonElement pyToGson(@Nullable org.python.core.PyObject pyObject, @Nullable com.inductiveautomation.ignition.common.gson.Gson customGson) Make a best effort to convert a PyObject into some Gson structure. Recurses, so make sure your data structure is sane.- Parameters:
pyObject
- object to convertcustomGson
- A specific Gson instance to use for conversion.
-
getWrapperType
- Throws:
ClassCastException
-
getPrimitiveType
- Throws:
ClassCastException
-
hasPrimitiveType
-
getLastNameComponent
Tags a string that represents a series of elements seperated by period (".") and returns the last portion. -
getFirstOrNull
Takes a list and returns the first element. If list is null or empty, returns null.- Returns:
- the first element of the list, or null if the list is null or contains no elements.
-
setArrayValue
Sets a value in an array, with support for QualifiedValues and conversion of the newVal to the target type. Returns a helpful error code if the assignment isn't possible.- Returns:
- the result of the assignment- good if successful, other quality if an error occured.
-
getInitValueForClass
- Throws:
ClassCastException
-
datasetToArray
-
datasetToJSON
-
datasetToGson
-
datasetToGson
public static com.inductiveautomation.ignition.common.gson.JsonElement datasetToGson(Dataset data, com.inductiveautomation.ignition.common.gson.Gson gson) -
datasetFromJsonString
public static Dataset datasetFromJsonString(String jsonStr) throws JSONException, ClassNotFoundException Use when you only have JSON in the form of a String- Throws:
JSONException
ClassNotFoundException
-
datasetFromJSON
- Throws:
JSONException
ClassNotFoundException
-