Package com.blackrook.archetext.struct
Class Utils
- java.lang.Object
-
- com.blackrook.archetext.struct.Utils
-
public final class Utils extends Object
-
-
Constructor Summary
Constructors Constructor Description Utils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> TapplyToObject(ArcheTextObject atObject, T object)Applies this object to an object bean / plain ol' Java object.static <T> Tconstruct(Constructor<T> constructor, Object... params)Creates a new instance of a class from a class type.static <T> Tcreate(Class<T> clazz)Creates a new instance of a class from a class type.static <T> TcreateForType(Object object, Class<T> targetType)Reflect.creates a new instance of an object for placement in a POJO or elsewhere.static <T,S extends Set<T>>
Sdifference(S set1, S set2)Returns a new Set that is the difference of the objects in two sets, i.e.static <T> voidexportTo(T object, ArcheTextObject atext)Exports the values of an object to an ArcheTextObject.static intgetArrayDimensions(Class<?> arrayType)Gets how many dimensions that this array, represented by the provided type, has.static intgetArrayDimensions(Object array)Gets how many array dimensions that an object (presumably an array) has.static Class<?>getArrayType(Class<?> arrayType)Gets the class type of this array type, if this is an array type.static Class<?>getArrayType(Object object)Gets the class type of this array, if this is an array.static ObjectgetFieldValue(Object instance, Field field)Gets the value of a field on an object.static <T> StringgetIdentityFromObject(T object)Get identity name for object.static <T,S extends Set<T>>
Sintersection(S set1, S set2)Returns a new Set that is the intersection of the objects in two sets, i.e.static ObjectinvokeBlind(Method method, Object instance, Object... params)Blindly invokes a method, only throwing aRuntimeExceptionif something goes wrong.static booleanisArray(Class<?> clazz)Tests if a class is actually an array type.static booleanisArray(Object object)Tests if an object is actually an array type.static booleanisEmpty(Object obj)Checks if a value is "empty." The following is considered "empty": Null references.static <T> TisNull(T testObject, T nullReturn)Returns the first object if it is not null, otherwise returns the second.static booleanisWindows()static InputStreamopenResource(String pathString)Opens anInputStreamto a resource using the current thread'sClassLoader.static voidsetFieldValue(Object instance, Field field, Object value)Sets the value of a field on an object.static <T,S extends Set<T>>
Sunion(S set1, S set2)Returns a new Set that is the union of the objects in two sets, i.e.static <T,S extends Set<T>>
Sxor(S set1, S set2)Returns a new Set that is the union minus the intersection of the objects in two sets.
-
-
-
Method Detail
-
isWindows
public static boolean isWindows()
- Returns:
- true if we using Windows.
-
createForType
public static <T> T createForType(Object object, Class<T> targetType)
Reflect.creates a new instance of an object for placement in a POJO or elsewhere.- Type Parameters:
T- the return object type.- Parameters:
object- the object to convert to another objecttargetType- the target class type to convert to, if the types differ.- Returns:
- a suitable object of type
targetType. - Throws:
ClassCastException- if the incoming type cannot be converted.
-
openResource
public static InputStream openResource(String pathString)
Opens anInputStreamto a resource using the current thread'sClassLoader.- Parameters:
pathString- the resource pathname.- Returns:
- an open
InputStreamfor reading the resource or null if not found. - See Also:
ClassLoader.getResourceAsStream(String)
-
isArray
public static boolean isArray(Class<?> clazz)
Tests if a class is actually an array type.- Parameters:
clazz- the class to test.- Returns:
- true if so, false if not.
-
isArray
public static boolean isArray(Object object)
Tests if an object is actually an array type.- Parameters:
object- the object to test.- Returns:
- true if so, false if not.
-
getArrayDimensions
public static int getArrayDimensions(Class<?> arrayType)
Gets how many dimensions that this array, represented by the provided type, has.- Parameters:
arrayType- the type to inspect.- Returns:
- the number of array dimensions, or 0 if not an array.
-
getArrayDimensions
public static int getArrayDimensions(Object array)
Gets how many array dimensions that an object (presumably an array) has.- Parameters:
array- the object to inspect.- Returns:
- the number of array dimensions, or 0 if not an array.
-
getArrayType
public static Class<?> getArrayType(Class<?> arrayType)
Gets the class type of this array type, if this is an array type.- Parameters:
arrayType- the type to inspect.- Returns:
- this array's type, or null if the provided type is not an array, or if the found class is not on the classpath.
-
getArrayType
public static Class<?> getArrayType(Object object)
Gets the class type of this array, if this is an array.- Parameters:
object- the object to inspect.- Returns:
- this array's type, or null if the provided object is not an array, or if the found class is not on the classpath.
-
isEmpty
public static boolean isEmpty(Object obj)
Checks if a value is "empty." The following is considered "empty":- Null references.
Arrayobjects that have a length of 0.Booleanobjects that are false.Characterobjects that are the null character ('\0', ' ').Numberobjects that are zero.Stringobjects that are the empty string, or areString.trim()'ed down to the empty string.Collectionobjects whereCollection.isEmpty()returns true.
- Parameters:
obj- the object to check.- Returns:
- true if the provided object is considered "empty", false otherwise.
-
isNull
public static <T> T isNull(T testObject, T nullReturn)Returns the first object if it is not null, otherwise returns the second.- Type Parameters:
T- class that extends Object.- Parameters:
testObject- the first ("tested") object.nullReturn- the object to return if testObject is null.- Returns:
- testObject if not null, nullReturn otherwise.
-
union
public static <T,S extends Set<T>> S union(S set1, S set2)
Returns a new Set that is the union of the objects in two sets, i.e. a set with all objects from both sets.- Type Parameters:
T- the object type in the provided set.S- the set that contains type T.- Parameters:
set1- the first set.set2- the second set.- Returns:
- a new set.
-
intersection
public static <T,S extends Set<T>> S intersection(S set1, S set2)
Returns a new Set that is the intersection of the objects in two sets, i.e. the objects that are present in both sets.- Type Parameters:
T- the object type in the provided set.S- the set table that contains type T.- Parameters:
set1- the first set.set2- the second set.- Returns:
- a new set.
-
difference
public static <T,S extends Set<T>> S difference(S set1, S set2)
Returns a new Set that is the difference of the objects in two sets, i.e. the objects in the first set minus the objects in the second.- Type Parameters:
T- the object type in the provided set.S- the set table that contains type T.- Parameters:
set1- the first set.set2- the second set.- Returns:
- a new set.
-
xor
public static <T,S extends Set<T>> S xor(S set1, S set2)
Returns a new Set that is the union minus the intersection of the objects in two sets.- Type Parameters:
T- the object type in the provided set.S- the set table that contains type T.- Parameters:
set1- the first set.set2- the second set.- Returns:
- a new set.
-
setFieldValue
public static void setFieldValue(Object instance, Field field, Object value)
Sets the value of a field on an object.- Parameters:
instance- the object instance to set the field on.field- the field to set.value- the value to set.- Throws:
NullPointerException- if the field or object provided is null.ClassCastException- if the value could not be cast to the proper type.RuntimeException- if anything goes wrong (bad field name, bad target, bad argument, or can't access the field).- See Also:
Field.set(Object, Object)
-
getFieldValue
public static Object getFieldValue(Object instance, Field field)
Gets the value of a field on an object.- Parameters:
instance- the object instance to get the field value of.field- the field to get the value of.- Returns:
- the current value of the field.
- Throws:
NullPointerException- if the field or object provided is null.RuntimeException- if anything goes wrong (bad target, bad argument, or can't access the field).- See Also:
Field.set(Object, Object)
-
invokeBlind
public static Object invokeBlind(Method method, Object instance, Object... params)
Blindly invokes a method, only throwing aRuntimeExceptionif something goes wrong. Here for the convenience of not making a billion try/catch clauses for a method invocation.- Parameters:
method- the method to invoke.instance- the object instance that is the method target.params- the parameters to pass to the method.- Returns:
- the return value from the method invocation. If void, this is null.
- Throws:
ClassCastException- if one of the parameters could not be cast to the proper type.RuntimeException- if anything goes wrong (bad target, bad argument, or can't access the method).- See Also:
Method.invoke(Object, Object...)
-
create
public static <T> T create(Class<T> clazz)
Creates a new instance of a class from a class type. This essentially callsClass.newInstance(), but wraps the call in a try/catch block that only throws an exception if something goes wrong.- Type Parameters:
T- the return object type.- Parameters:
clazz- the class type to instantiate.- Returns:
- a new instance of an object.
- Throws:
RuntimeException- if instantiation cannot happen, either due to a non-existent constructor or a non-visible constructor.
-
construct
public static <T> T construct(Constructor<T> constructor, Object... params)
Creates a new instance of a class from a class type. This essentially callsClass.newInstance(), but wraps the call in a try/catch block that only throws an exception if something goes wrong.- Type Parameters:
T- the return object type.- Parameters:
constructor- the constructor to call.params- the constructor parameters.- Returns:
- a new instance of an object created via the provided constructor.
- Throws:
RuntimeException- if instantiation cannot happen, either due to a non-existent constructor or a non-visible constructor.
-
exportTo
public static <T> void exportTo(T object, ArcheTextObject atext)Exports the values of an object to an ArcheTextObject.- Type Parameters:
T- the object's type.- Parameters:
object- the object to export.atext- the destination structure.- Throws:
ArcheTextExportException- if a problem happens during export.ClassCastException- if a value cannot be converted.
-
applyToObject
public static <T> T applyToObject(ArcheTextObject atObject, T object)
Applies this object to an object bean / plain ol' Java object.This object is applied via the target object's public fields and setter methods.
For instance, if there is a member on this object called "color", its value will be applied via the public field "color" or the setter "setColor()". Public fields take precedence over setters.
- Type Parameters:
T- the object's type.- Parameters:
atObject- the source object.object- the target object.- Returns:
- the applied object itself.
-
getIdentityFromObject
public static <T> String getIdentityFromObject(T object)
Get identity name for object.- Type Parameters:
T- the object type.- Parameters:
object- the object to inspect.- Returns:
- the identity of the object to use for the ArcheText object.
-
-