public class TypeUtilities
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static interface |
TypeUtilities.ClassInitializer |
protected static class |
TypeUtilities.DefaultClassInitializer
Initializes most basic classes.
|
Modifier and Type | Field and Description |
---|---|
static java.text.DateFormat |
_DATE_FORMATTER
yyyyMMdd.HHmmssSSSZ
|
static java.lang.String |
DATE_FORMAT_STRING |
static java.util.Comparator<java.lang.String> |
NULL_SAFE_CASE_INSENSITIVE_ORDER |
Constructor and Description |
---|
TypeUtilities() |
Modifier and Type | Method and Description |
---|---|
static <T> boolean |
anyEqual(T value,
T... others)
Returns whether the value is equal to any of the other values provided.
|
static java.lang.Object |
coerce(java.lang.Object value,
java.lang.Class destType)
Attempts to coerce the Object
value into an instance of the Class type . |
static java.lang.Object |
coerceForLocale(java.lang.Object value,
java.lang.Class target,
java.util.Locale valueLocale)
A version of coerce intended to go from String to Number, where the source string is formatted for a certain
locale.
|
static <T> T |
coerceGeneric(java.lang.Object value,
java.lang.Class<? extends T> destType)
Performs the same operation as coerce, but with generic typing.
|
static java.lang.Object |
coerceLocaleSafe(java.lang.String str,
java.lang.Class type)
Special version of coerce that has explicit handling for String-to-Number coersions that don't depend on the
current locale.
|
static java.lang.Object |
coerceNullSafe(java.lang.Object value,
java.lang.Class destType)
Like coerce, except that if value is null, null will be returned instead of the "default" value
for the desired type.
|
static java.lang.String |
colorToHex(java.awt.Color c)
Returns the color as a web-compatible hex string.
|
static int |
compareInts(int foo,
int bar) |
static <T extends java.lang.Comparable<T>> |
compareNullHigh(T c1,
T c2)
Null-safe compare.
|
static <T extends java.lang.Comparable<T>> |
compareNullLow(T c1,
T c2)
Null-safe compare.
|
static Dataset |
datasetFromJSON(JSONObject json) |
static JSONObject |
datasetToJSON(Dataset data) |
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 |
equals(java.lang.Object o1,
java.lang.Object o2)
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(java.lang.Object o1,
java.lang.Object o2)
Equivalent semantics to
equals(Object, Object) , but if both arguments are Strings, case insensitive
equality will be used |
static java.lang.Object |
fromString(java.lang.String value,
java.lang.Class<?> dest,
java.util.Locale locale)
A special version of coerce, knowing that the source is a string in the given locale.
|
static java.awt.Color |
getColorFromString(java.lang.String color)
Tries to parse a Color fram the given string.
|
static <T> T |
getFirstOrNull(java.util.List<T> list)
Takes a list and returns the first element.
|
static java.lang.Object |
getInitValueForClass(java.lang.Class<?> c) |
static java.lang.String |
getLastNameComponent(java.lang.String name)
Tags a string that represents a series of elements seperated by period (".") and returns the last portion.
|
static java.lang.Class<?> |
getPrimitiveType(java.lang.Class<?> c) |
static java.lang.Class<?> |
getWrapperType(java.lang.Class<?> c) |
static boolean |
hasPrimitiveType(java.lang.Class<?> c) |
static boolean |
hasValueChanged(QualifiedValue currentValue,
QualifiedValue previousValue,
DataType expectedType,
java.lang.Double deadband)
Checks whether the value and/or quality have change compared to the
current value, taking into account the deadband.
|
static boolean |
isAssignable(java.lang.Class<?> dest,
java.lang.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 |
isBoolean(java.lang.Class<?> cls)
True if cls == boolean.class || cls==Boolea.class
|
static boolean |
isDirectlyAssignable(java.lang.Class<?> dest,
java.lang.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(java.lang.Class<?> cls) |
static boolean |
isNullOrEmpty(java.lang.String s)
Returns true if the String s is null or zero-length
|
static boolean |
isNumber(java.lang.Class<?> cls)
Returns true if the class is boolean or is a proper number.
|
static boolean |
isPrimitive(java.lang.Class c)
Returns true if the class is one of the java primitive classes
|
static boolean |
isProperNumber(java.lang.Class<?> cls)
Returns true if the given class represents a subclass of Number, or any of the numeric primitives.
|
static boolean |
neq(java.lang.Object o1,
java.lang.Object o2)
Convenience for: !TypeUtilities.equals(o1,o2)
|
static java.lang.Object |
pyToJava(org.python.core.PyObject pyObject)
Takes a PyObject and returns a java object, with a bit of additional logic.
|
static void |
setClassInitializer(TypeUtilities.ClassInitializer init)
Overrides the default class initializer.
|
static java.lang.Boolean |
toBool(java.lang.Object value) |
static byte[] |
toByteArray(java.util.UUID uuid) |
static java.awt.Color |
toColor(java.lang.String color) |
static Dataset |
toDataset(java.lang.Object value) |
static java.util.Date |
toDate(java.lang.Object value) |
static java.lang.Double |
toDouble(java.lang.Object value) |
static java.lang.Float |
toFloat(java.lang.Object value) |
static java.lang.Integer |
toInteger(java.lang.Object value) |
static java.lang.Long |
toLong(java.lang.Object value) |
static java.lang.Number |
toNumber(java.lang.Object value)
Coerces the value to a number.
|
static java.lang.Number |
toNumber(java.lang.String value,
java.util.Locale locale) |
static java.lang.Short |
toShort(java.lang.Object value) |
static java.lang.String |
toStr(java.lang.Object value)
Deprecated.
use
Object.toString() instead |
static java.lang.String |
toString(java.lang.Object value)
Returns the string form of a value.
|
static java.lang.String |
toStringLocalized(java.lang.Object value) |
static java.lang.String |
toStringLocalized(java.lang.Object value,
java.util.Locale locale)
Performs a localized stringifying/formatting of an object.
|
static java.util.UUID |
toUUID(byte[] barr)
Uses a 16-byte long bytearray to create a new UUID.
|
public static final java.lang.String DATE_FORMAT_STRING
public static final java.text.DateFormat _DATE_FORMATTER
public static final java.util.Comparator<java.lang.String> NULL_SAFE_CASE_INSENSITIVE_ORDER
public static void setClassInitializer(TypeUtilities.ClassInitializer init)
init
- public static boolean isProperNumber(java.lang.Class<?> cls)
cls
- public static boolean isPrimitive(java.lang.Class c)
public static boolean isFractional(java.lang.Class<?> cls)
public static boolean isBoolean(java.lang.Class<?> cls)
public static boolean isNumber(java.lang.Class<?> cls)
cls
- public static boolean isAssignable(java.lang.Class<?> dest, java.lang.Class<?> source)
public static boolean isDirectlyAssignable(java.lang.Class<?> dest, java.lang.Class<?> source)
public static boolean isNullOrEmpty(java.lang.String s)
public static <T extends java.lang.Comparable<T>> int compareNullHigh(T c1, T c2)
public static <T extends java.lang.Comparable<T>> int compareNullLow(T c1, T c2)
public static boolean hasValueChanged(QualifiedValue currentValue, QualifiedValue previousValue, DataType expectedType, java.lang.Double deadband)
public static boolean equals(java.lang.Object o1, java.lang.Object o2)
o1
- o2
- public static boolean equals(double val1, double val2, double tolerance)
val1
- val2
- tolerance
- public static boolean neq(java.lang.Object o1, java.lang.Object o2)
public static <T> boolean anyEqual(T value, T... others)
value
- others
- public static boolean equalsIgnoreCase(java.lang.Object o1, java.lang.Object o2)
equals(Object, Object)
, but if both arguments are Strings, case insensitive
equality will be usedpublic static java.lang.String toStringLocalized(java.lang.Object value)
public static java.lang.String toStringLocalized(java.lang.Object value, java.util.Locale locale)
value
- locale
- public static java.lang.String toString(java.lang.Object value)
value
- public static java.lang.Object fromString(java.lang.String value, java.lang.Class<?> dest, java.util.Locale locale)
value
- dest
- locale
- public static java.lang.Number toNumber(java.lang.Object value)
value
- public static java.lang.Number toNumber(java.lang.String value, java.util.Locale locale) throws java.lang.ClassCastException
java.lang.ClassCastException
public static java.lang.Object coerceNullSafe(java.lang.Object value, java.lang.Class destType) throws java.lang.ClassCastException
java.lang.ClassCastException
public static java.lang.Object coerceLocaleSafe(java.lang.String str, java.lang.Class type) throws java.lang.ClassCastException
java.lang.ClassCastException
public static java.lang.Object coerceForLocale(java.lang.Object value, java.lang.Class target, java.util.Locale valueLocale) throws java.lang.ClassCastException
value
- target
- valueLocale
- java.lang.ClassCastException
public static <T> T coerceGeneric(java.lang.Object value, java.lang.Class<? extends T> destType)
T
- value
- destType
- public static java.lang.Object coerce(java.lang.Object value, java.lang.Class destType) throws java.lang.ClassCastException
value
into an instance of the Class type
. If
value
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.value
- destType
- java.lang.ClassCastException
public static int compareInts(int foo, int bar)
public static java.lang.Integer toInteger(java.lang.Object value) throws java.lang.ClassCastException
java.lang.ClassCastException
public static java.lang.Long toLong(java.lang.Object value) throws java.lang.ClassCastException
java.lang.ClassCastException
public static java.lang.Boolean toBool(java.lang.Object value) throws java.lang.ClassCastException
java.lang.ClassCastException
public static byte[] toByteArray(java.util.UUID uuid)
public static java.util.UUID toUUID(byte[] barr) throws java.lang.ClassCastException
barr
- java.lang.ClassCastException
- if the byte array isn't exactly 16 bytes longjava.lang.NullPointerException
- if barr is nullpublic static java.lang.Double toDouble(java.lang.Object value) throws java.lang.ClassCastException
java.lang.ClassCastException
public static java.lang.Float toFloat(java.lang.Object value) throws java.lang.ClassCastException
java.lang.ClassCastException
public static java.lang.Short toShort(java.lang.Object value) throws java.lang.ClassCastException
java.lang.ClassCastException
public static java.util.Date toDate(java.lang.Object value) throws java.lang.ClassCastException
java.lang.ClassCastException
public static Dataset toDataset(java.lang.Object value) throws java.lang.ClassCastException
java.lang.ClassCastException
@Deprecated public static java.lang.String toStr(java.lang.Object value) throws java.lang.ClassCastException
Object.toString()
insteadjava.lang.ClassCastException
public static java.awt.Color getColorFromString(java.lang.String color)
color
- public static java.awt.Color toColor(java.lang.String color)
public static java.lang.String colorToHex(java.awt.Color c)
public static java.lang.Object pyToJava(org.python.core.PyObject pyObject)
pyObject
- incoming value, if null, returns null.public static java.lang.Class<?> getWrapperType(java.lang.Class<?> c) throws java.lang.ClassCastException
java.lang.ClassCastException
public static java.lang.Class<?> getPrimitiveType(java.lang.Class<?> c) throws java.lang.ClassCastException
java.lang.ClassCastException
public static boolean hasPrimitiveType(java.lang.Class<?> c)
public static java.lang.String getLastNameComponent(java.lang.String name)
name
- public static <T> T getFirstOrNull(java.util.List<T> list)
T
- list
- public static java.lang.Object getInitValueForClass(java.lang.Class<?> c) throws java.lang.ClassCastException
java.lang.ClassCastException
public static JSONObject datasetToJSON(Dataset data)
public static Dataset datasetFromJSON(JSONObject json) throws java.lang.Exception
java.lang.Exception