Class TypeUtilities
java.lang.Object
com.inductiveautomation.ignition.common.TypeUtilities
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceprotected static classInitializes most basic classes. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final DateFormatyyyyMMdd.HHmmssSSSZstatic final Stringstatic final Comparator<String>static final StringA fully specified date & time, without timezone. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> booleananyEqual(T value, T... others) Determines if the value is equal to any of the other valuesstatic ObjectAttempts to coerce the Objectvalueinto an instance of the Classtype.static DatecoerceDateForLocale(String value, Locale locale) static ObjectcoerceForLocale(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 ObjectcoerceFromDataset(Dataset dataset, Class<?> destType) static <T> TcoerceGeneric(Object value, Class<? extends T> destType) Performs the same operation as coerce, but with generic typing.static ObjectcoerceLocaleSafe(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 ObjectcoerceNullSafe(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 ObjectcoerceNumberForLocale(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 ObjectcoerceNumberNullSafe(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 ObjectcoerceTimestamp(Timestamp value, Class<?> destType) static StringcolorToHex(Color c) Returns the color as a web-compatible hex string.static intcompareInts(int foo, int bar) 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 DatasetdatasetFromJSON(JSONObject json) static DatasetdatasetFromJsonString(String jsonStr) Use when you only have JSON in the form of a Stringstatic com.inductiveautomation.ignition.common.gson.JsonElementdatasetToGson(Dataset data) static com.inductiveautomation.ignition.common.gson.JsonElementdatasetToGson(Dataset data, com.inductiveautomation.ignition.common.gson.Gson gson) static JSONObjectdatasetToJSON(Dataset data) static booleandeepEquals(Object o1, Object o2, boolean checkArrayTypes) Likeequals(Object, Object), but if the objects are arrays it delegates toObjects.deepEquals(Object, Object).static booleanequals(double val1, double val2, double tolerance) A special form of equals for doubles, that takes a provider tolerance into account.static booleanThis 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 booleanequalsIgnoreCase(Object o1, Object o2) Equivalent semantics toequals(Object, Object), but if both arguments are Strings, case insensitive equality will be usedstatic ObjectfromString(String value, Class<?> dest, Locale locale) A special version of coerce, knowing that the source is a string in the given locale.static ColorgetColorFromString(String color) Tries to parse a Color from the given string.static <T> TgetFirstOrNull(List<T> list) Takes a list and returns the first element.static ObjectgetInitValueForClass(Class<?> c) static StringgetLastNameComponent(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.PyObjectgsonToPy(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 booleanhasPrimitiveType(Class<?> c) static booleanhasValueChanged(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 booleanisAssignable(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 booleanTrue if cls == boolean.class || cls==Boolean.classstatic booleanisDirectlyAssignable(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 booleanisFractional(Class<?> cls) static booleanReturns true if the String s is null or zero-lengthstatic booleanReturns true if the class is boolean or is a proper number.static booleanisPrimitive(Class c) Returns true if the class is one of the java primitive classesstatic booleanisProperNumber(Class<?> cls) Returns true if the given class represents a subclass of Number, or any of the numeric primitives.static booleanConvenience for: !TypeUtilities.equals(o1,o2)static com.inductiveautomation.ignition.common.gson.JsonElementpyToGson(org.python.core.PyObject pyObject) Make a best effort to convert a PyObject into some Gson structure.static com.inductiveautomation.ignition.common.gson.JsonElementpyToGson(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 ObjectpyToJava(org.python.core.PyObject pyObject) Takes a PyObject and returns a java object, with a bit of additional logic.static QualityCodesetArrayValue(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 voidOverrides the default class initializer.static Booleanstatic byte[]toByteArray(UUID uuid) static Colorstatic Datasetstatic Datestatic DocumentElementtoDocument(Object value) static Doublestatic <T extends Enum>
Tstatic Floatstatic Integerstatic Longstatic NumberCoerces the value to a number.static Numberstatic Shortstatic StringDeprecated.static StringReturns the string form of a value.static StringtoStringLocalized(Object value) static StringtoStringLocalized(Object value, Locale locale) Performs a localized stringifying/formatting of an object.static StringtoStringOnlyNumberLocalized(Object value, Locale locale) A version of toString that only localizes numbers.static UUIDtoUUID(byte[] barr) Uses a 16-byte long bytearray to create a new UUID.
-
Field Details
-
DATE_FORMAT_STRING
- See Also:
-
_DATE_FORMATTER
yyyyMMdd.HHmmssSSSZ -
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
-
setClassInitializer
Overrides the default class initializer. A Class initializer knows how to create default, empty versions of various classes. -
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
truethen 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 too2for equality.checkArrayTypes-trueif arrays must be of the same type to be considered equal.- Returns:
trueifo1equalso2, 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) -
anyEqual
public static <T> boolean anyEqual(T value, T... others) Determines if the value is equal to any of the other values- Parameters:
others- vararg values to compare the the value- Returns:
- true if the value is equal to any of the other values provided.
-
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
- Throws:
ClassCastException
-
coerceNumberNullSafe
public static Object coerceNumberNullSafe(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
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(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 Objectvalueinto an instance of the Classtype. Ifvalueis 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
-
compareInts
public static int compareInts(int foo, int bar) -
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
-
toShort
- Throws:
ClassCastException
-
toDate
- Throws:
ClassCastException
-
toDataset
- Throws:
ClassCastException
-
toDocument
- Throws:
ClassCastException
-
toStr
Deprecated.useObject.toString()instead- 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
-
datasetToJSON
-
datasetToGson
-
datasetToGson
public static com.inductiveautomation.ignition.common.gson.JsonElement datasetToGson(Dataset data, com.inductiveautomation.ignition.common.gson.Gson gson) -
datasetFromJsonString
Use when you only have JSON in the form of a String- Throws:
Exception
-
datasetFromJSON
- Throws:
Exception
-
Object.toString()instead