Package com.globalmentor.java
Class Objects
java.lang.Object
com.globalmentor.java.Objects
Various utilities to manipulate Java objects.
- Author:
- Garret Wilson
-
Field Summary
Modifier and TypeFieldDescriptionstatic final Object[]
A shared object array that contains no elements. -
Method Summary
Modifier and TypeMethodDescriptionasInstance
(Class<I> instanceClass) Convenience method that returns a function that casts some object to a given class if and only if it is an instance of that class.static <T,
I extends T>
Optional<I>asInstance
(T object, Class<I> instanceClass) Convenience method that returns the given object if and only if it is an instance of the given class.asInstances
(Class<I> instanceClass) Convenience method that returns a function that casts some object to a given class if and only if it is an instance of that class, returning a potentially empty stream.static <T> T
Checks to see if a given variable is of the correct type and if not, throws aClassCastException
.static <T> T
Checks to see if a given variable is of the correct type and if not, throws aClassCastException
.static <T extends CloneSupported>
Tclone
(T object) Clones an object that supports cloning.static <T extends Comparable<? super T>>
intcompare
(T object1, T object2, int nullBias) Compares two objects for order, taking into accountnull
.static final boolean
Deprecated.static int
getDoubleHashCode
(double... values) Deprecated.static int
getHashCode
(Object... objects) Deprecated.to be removed in favor ofObjects.hash(Object...)
from Java 7.static <T,
C extends T>
CgetInstance
(Class<C> objectClass, T... objects) Returns the first object that is an instance of the given object type.static <T> T
getInstance
(T... objects) Returns the first object that is an instance ofObject
(i.e.static Class<?>
getInstanceOf
(Object object, Class<?>... classes) Determines of which, if any, of the provided classes the given object is an instance.static int
getIntHashCode
(int... values) Deprecated.static int
getLongHashCode
(long... values) Deprecated.static Object
getProperty
(Object object, String propertyName) Returns the property of an object based upon a given property name.static int
getSeededHashCode
(int seed, Object... objects) Deprecated.static boolean
isInstanceOf
(Object object, Class<?>... classes) Determines if the object is an instance of any of the provided classes.static Object[]
requireNonNulls
(Object... objects) Checks to see if the elements are instances of any object, and throws aNullPointerException
if any element isnull
.static AutoCloseable
toAutoCloseable
(Object object) Converts an object to anAutoCloseable
instance so that it can be used with try-with-resources.static <T> T
toInstance
(T object, T defaultInstance) Returns either and object or a default instance if the object isnull
.static final String
Returns the string representation of the object or "null".
-
Field Details
-
NO_OBJECTS
A shared object array that contains no elements.
-
-
Method Details
-
checkType
Checks to see if a given variable is of the correct type and if not, throws aClassCastException
.- Type Parameters:
T
- The type of variable to check.- Parameters:
variable
- The variable to check, ornull
.type
- The type to verify.- Returns:
- The given variable.
- Throws:
ClassCastException
- if the given variable is notnull
and not an instance of type type.
-
checkType
Checks to see if a given variable is of the correct type and if not, throws aClassCastException
.- Type Parameters:
T
- The type of variable to check.- Parameters:
variable
- The variable to check, ornull
.type
- The type to verify.description
- A description of the variable to be used when generating an exception, ornull
for no description.- Returns:
- The given variable.
- Throws:
ClassCastException
- if the given variable is notnull
and not an instance of type type.
-
clone
Clones an object that supports cloning.- Type Parameters:
T
- The type of the object.- Parameters:
object
- The object that supports cloning through use of theCloneSupported
interface.- Returns:
- The cloned object.
- Throws:
IllegalStateException
- if the object'sCloneSupported.clone()
method throws aCloneNotSupportedException
.- See Also:
-
compare
Compares two objects for order, taking into accountnull
. If both objects arenull
they are considered equal. If only one object isnull
, comparison will be performed based upon whethernull
is considered higher or lower. Otherwise, the second object is compared to the first using the first object'sComparable.compareTo(Object)
method.- Type Parameters:
T
- The type of the object.- Parameters:
object1
- The first object to be compared.object2
- The second object to be compared.nullBias
- A negative or positive integer indicating ifnull
should be considered less than or greater than non-null
objects.- Returns:
- A negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
-
asInstance
Convenience method that returns the given object if and only if it is an instance of the given class. This method is equivalent toobject instanceof Type ? Optional.of((Type)object) : Optional.empty();
.- Type Parameters:
T
- The type of object given.I
- The type of object instance to check for.- Parameters:
object
- The object to examine.instanceClass
- The class of which the object may be an instance.- Returns:
- The object if it is an instance of the given class.
- See Also:
-
asInstance
Convenience method that returns a function that casts some object to a given class if and only if it is an instance of that class.- API Note:
- This method may be used to cast an
Optional
value usingoptional.flatMap(asInstance(instanceClass)
. In this way it is equivalent tooptional.filter(instanceClass::isInstance).map(instanceClass::cast)
. - Implementation Specification:
- This implementation returns a function that delegates to
asInstance(Object, Class)
. - Type Parameters:
T
- The type of object given.I
- The type of object instance to check for.- Parameters:
instanceClass
- The class of which the object may be an instance.- Returns:
- The a function that returns the object if it is an instance of the given class; otherwise an empty
Optional
. - See Also:
-
asInstances
Convenience method that returns a function that casts some object to a given class if and only if it is an instance of that class, returning a potentially empty stream.- API Note:
- This method may be used to cast
Stream
values usingstream.flatMap(asInstances(instanceClass)
. In this way it is equivalent tostream.filter(instanceClass::isInstance).map(instanceClass::cast)
. - Implementation Specification:
- This implementation returns a function that delegates to
asInstance(Object, Class)
. - Type Parameters:
T
- The type of object given.I
- The type of object instance to check for.- Parameters:
instanceClass
- The class of which the object may be an instance.- Returns:
- The a function that returns the object if it is an instance of the given class; otherwise an empty
Stream
. - See Also:
-
equals
Deprecated.to be removed in favor ofObjects.equals(Object, Object)
from Java 7.Compares two objects to make sure that the objects are equal, or the objects are both set tonull
. If the first object is notnull
, it is compared to the second using the first object'sObject.equals(Object)
method. This is a convenience method to compare two objects using theObject.equals(Object)
method when it's not known if one of the objects isnull
.- Parameters:
object1
- The first object to compare.object2
- The second object to compare.- Returns:
true
if the objects are equal according to the first object'sObject.equals(Object)
method or if both objects arenull
.- See Also:
-
getInstance
Returns the first object that is an instance ofObject
(i.e. that is notnull
).- Type Parameters:
T
- The type of the objects.- Parameters:
objects
- The objects to investigate.- Returns:
- The first object that is not
null
, ornull
if all objects arenull
.
-
getInstance
Returns the first object that is an instance of the given object type.- Type Parameters:
T
- The type of the objects.C
- The subtype of the given objects.- Parameters:
objectClass
- The class of the type of object to returnobjects
- The objects to investigate.- Returns:
- The first object that is the instance of the given type, or
null
if no object is an instance of the indicated type. - Throws:
NullPointerException
- if the given object class isnull
.
-
toInstance
public static <T> T toInstance(T object, T defaultInstance) Returns either and object or a default instance if the object isnull
.This is equivalent to the JavaScript statement
var x = object || defaultInstance;
- Type Parameters:
T
- The type of the object.- Parameters:
object
- The object to examine.defaultInstance
- The default instance to return if the object isnull
.- Returns:
- The object, or the default instance of the object is
null
. - Throws:
NullPointerException
- if the given default instance isnull
.
-
getProperty
public static Object getProperty(Object object, String propertyName) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException Returns the property of an object based upon a given property name. A property getter in the form "getPropertyName" takes precedence over a property getter in the form "isPropertyName" having aBoolean.TYPE
.- Parameters:
object
- The object the property of which to retrieve.propertyName
- The name of the property to retrieve.- Returns:
- The value of the given property.
- Throws:
NullPointerException
- if the given object isnull
.NoSuchMethodException
- if the given object has no method with the name "getPropertyName", or the name "isPropertyName" having aBoolean.TYPE
.IllegalAccessException
- if the getter method enforces Java language access control and the getter method is inaccessible.InvocationTargetException
- if the getter method throws an exception.ExceptionInInitializerError
- if the initialization provoked by the getter method fails.
-
getHashCode
Deprecated.to be removed in favor ofObjects.hash(Object...)
from Java 7.Generates a hash code based upon a series of objects. This method is based upon the algorithm explained in Effective Java (2001) by Joshua Bloch (pp. 38-39) as well as the hash code generation of the Java runtime library. Any or all objects can benull
. Arrays are deeply traversed. This methods delegates togetSeededHashCode(int, Object...)
with a seed value of 17.- Parameters:
objects
- The objects to be used in generating a hash code.- Returns:
- A hash code taking into account the given objects.
- Throws:
NullPointerException
- if the given array of objects isnull
.
-
getSeededHashCode
Deprecated.Generates a hash code based upon a seed and a series of objects. This method is based upon the algorithm explained in Effective Java (2001) by Joshua Bloch (pp. 38-39) as well as the hash code generation of the Java runtime library. Any or all objects can benull
. Arrays are deeply traversed.- Parameters:
seed
- The seed with which to start.objects
- The objects to be used in generating a hash code.- Returns:
- A hash code starting with the given seed and taking into account the given objects.
- Throws:
NullPointerException
- if the given array of objects isnull
.
-
getIntHashCode
Deprecated.Generates a hash code based upon a series of integer values. This is a convenience varargs method that delegates to theArrays.hashCode(int[])
version.- Parameters:
values
- The values to be used in generating a hash code.- Returns:
- A hash code taking into account the given values.
- Throws:
NullPointerException
- if the given array of values isnull
.- See Also:
-
getLongHashCode
Deprecated.Generates a hash code based upon a series of long values. This is a convenience varargs method that delegates to theArrays.hashCode(long[])
version.- Parameters:
values
- The values to be used in generating a hash code.- Returns:
- A hash code taking into account the given values.
- Throws:
NullPointerException
- if the given array of values isnull
.- See Also:
-
getDoubleHashCode
Deprecated.Generates a hash code based upon a series of integer values. This is a convenience varargs method that delegates to theArrays.hashCode(double[])
version.- Parameters:
values
- The values to be used in generating a hash code.- Returns:
- A hash code taking into account the given values.
- Throws:
NullPointerException
- if the given array of values isnull
.- See Also:
-
getInstanceOf
Determines of which, if any, of the provided classes the given object is an instance.- Parameters:
object
- The object to check as an instance; may benull
.classes
- The classes of which to check the given object being an instance.- Returns:
- The first of the listed classes of which the given object is an instance, or
null
if the object is not an instance of any of the listed classes.
-
isInstanceOf
Determines if the object is an instance of any of the provided classes.- Parameters:
object
- The object to check as an instance; may benull
.classes
- The classes of which to check the given object being an instance.- Returns:
true
if the object is an instance of one of the listed classes.
-
requireNonNulls
Checks to see if the elements are instances of any object, and throws aNullPointerException
if any element isnull
.- Parameters:
objects
- The objects of which to check.- Returns:
- The given objects.
- Throws:
NullPointerException
- if any of the given objects arenull
.
-
toAutoCloseable
Converts an object to anAutoCloseable
instance so that it can be used with try-with-resources.- API Note:
- The
Exception
thrown by theAutoCloseable
interface may be too broad; theIO.toCloseable(Object)
method converts to an interface with more limited exceptions allowed. - Implementation Specification:
- If the given object is an instance of
AutoCloseable
, the object itself is returned; otherwise, a no-operationAutoCloseable
instance is returned. - Parameters:
object
- The object to convert to anAutoCloseable
.- Returns:
- An
AutoCloseable
instance that will ensure the object is closed if it implementsAutoCloseable
. - See Also:
-
toString
Returns the string representation of the object or "null".- Parameters:
object
- An object to be represented by a string.- Returns:
- The string representation of the object or "null" if the object is
null
.
-
Objects.equals(Object, Object)
from Java 7.