Class Utils
java.lang.Object
com.blackrook.archetext.struct.Utils
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <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> TCreates 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 booleanTests if a class is actually an array type.static booleanTests if an object is actually an array type.static booleanChecks 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 booleanstatic 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.
-
Constructor Details
-
Utils
public Utils()
-
-
Method Details
-
isWindows
public static boolean isWindows()- Returns:
- true if we using Windows.
-
createForType
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
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:
-
isArray
Tests if a class is actually an array type.- Parameters:
clazz- the class to test.- Returns:
- true if so, false if not.
-
isArray
Tests if an object is actually an array type.- Parameters:
object- the object to test.- Returns:
- true if so, false if not.
-
getArrayDimensions
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
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
-
getArrayType
-
isEmpty
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
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
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
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
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
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:
-
getFieldValue
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:
-
invokeBlind
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:
-
create
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
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
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
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
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.
-