java.lang.Object
edu.iu.IuObject
Simplifies building efficient
Object.equals(Object)
,
Object.hashCode()
, and Comparable.compareTo(Object)
methods
on plain Java objects.
The use of this utility is preferred, following the examples below, over other methods of generating these methods. When following these examples, the implements can be expected to follow the expected contracts in a null-safe and type-safe manner without undue object creation.
- Top level object:
-
@Override public int hashCode() { return ObjectUtil.hashCode(val1, val2); } @Override public boolean equals(Object obj) { if (!ObjectUtil.typeCheck(this, obj)) return false; MyClass other = (MyClass) obj; return ObjectUtil.equals(this.val1, other.val1) && ObjectUtil.equals(this.val2, other.val2); } @Override public int compareTo(T o) { Integer rv = ObjectUtil.compareNullCheck(this, o); if (rv != null) return rv; rv = ObjectUtil.compareTo(this.val1, o.val1); if (rv != 0) return rv; return ObjectUtil.compareTo(this.val2, o.val2); }
- Subclass object:
-
@Override public int hashCode() { return ObjectUtil.hashCodeSuper(super.hashCode(), val1, val2); } @Override public boolean equals(Object obj) { if (!ObjectUtil.typeCheck(this, obj)) return false; MyClass other = (MyClass) obj; return super.equals(obj) && ObjectUtil.equals(this.val1, other.val1) && ObjectUtil.equals(this.val2, other.val2); } @Override public int compareTo(T o) { Integer rv = ObjectUtil.compareNullCheck(this, o); if (rv != null) return rv; rv = ObjectUtil.compareTo(this.val1, o.val1); if (rv != 0) return rv; rv = ObjectUtil.compareTo(this.val2, o.val2); if (rv != 0) return rv; return super.compareTo(o); }
- Since:
- 4.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
assertNotOpen
(Class<?> classToCheck) Asserts that a class is in a module that is named and part of a package that is not open.static Integer
compareNullCheck
(Object o1, Object o2) Perform identity and and null check on two objects, returning a valid value forComparable.compareTo(Object)
if any of the checks result in a conclusive result.static int
Compares two objects with null checks (seecompareNullCheck(Object, Object)
) and also consistent sort order based for objects that don't implementComparable
.static <S,
T> T Passes a value through a conversion function if non-null.static boolean
Determine if two objects are equal, checking first for identity and null.static <T> T
first
(T... values) Gets the first non-null, after enforces that all remaining values are either null or equal to the first value.static <T> T
Enforces that a value is either not already set or is already set to the same value.static int
Generates a hash code for a top-level object based on related values (i.e.static int
hashCodeSuper
(int superHashCode, Object... oa) Generate a hash code for a subclass object based on its parent class' hash code and related values.static boolean
isPlatformName
(String name) Determines if a name is relative to a package provided by the JDK or JEE platform.static <T> T
once
(T current, T value) Enforces that either a current or new value is non-null, and that both non-null values are equal.static <T> T
Enforces that either a current or new value is non-null, and that both non-null values are equal.static <T> T
Enforces that either a current or new value is non-null, and that both non-null values are equal.static boolean
represents
(Object a, Object b) Determines if either or both objects are null, then if both non-null if both areequals(Object, Object)
.static <T> T
Require a condition to be true for a value.static <T> T
Require a condition to be true for a value if non-null.static <T> T
Require a condition to be true for a value if non-null.static Class
<?> requireFinalImpl
(Class<?> type) Determines if a class is a final implementation class.static <T> T
requireType
(Class<T> type, Object value) Require value to be an instance of a specific type or null.static <T> boolean
typeCheck
(T o1, T o2) Determine if two objects are both non-null instances of the same class.static <T> boolean
Determine if two objects are both instances of a specific class, or subclasses of that class.static void
waitFor
(Object lock, BooleanSupplier condition, Duration timeout) Waits until a condition is met or a timeout interval expires.static void
waitFor
(Object lock, BooleanSupplier condition, Duration timeout, Supplier<TimeoutException> timeoutFactory) Waits until a condition is met or a timeout interval expires.static void
waitFor
(Object lock, BooleanSupplier condition, Instant expires) Waits until a condition is met or a timeout interval expires.static void
waitFor
(Object lock, BooleanSupplier condition, Instant expires, Supplier<TimeoutException> timeoutFactory) Waits until a condition is met or a timeout interval expires.
-
Method Details
-
isPlatformName
Determines if a name is relative to a package provided by the JDK or JEE platform.- Parameters:
name
- type name- Returns:
true
if a platform type; else false
-
assertNotOpen
Asserts that a class is in a module that is named and part of a package that is not open.- Parameters:
classToCheck
-Class
- Throws:
IllegalStateException
- if the class is in an open module and/or package
-
requireFinalImpl
Determines if a class is a final implementation class.- Parameters:
type
- type- Returns:
- true if the class is not an interface and includes the final modifier
-
once
public static <T> T once(T current, T value) Enforces that either a current or new value is non-null, and that both non-null values are equal.- Type Parameters:
T
- value type- Parameters:
current
- current valuevalue
- value to set or enforce as already set- Returns:
- value
- Throws:
IllegalArgumentException
- if already set to the same value
-
once
Enforces that either a current or new value is non-null, and that both non-null values are equal.- Type Parameters:
T
- value type- Parameters:
current
- current valuevalue
- value to set or enforce as already setmessage
- message forIllegalArgumentException
if current was already set to a different value- Returns:
- value
- Throws:
IllegalArgumentException
- if already set to the same value
-
once
Enforces that either a current or new value is non-null, and that both non-null values are equal.- Type Parameters:
T
- value type- Parameters:
current
- current valuevalue
- value to set or enforce as already setmessageSupplier
- provides a message forIllegalArgumentException
if current was already set to a different value- Returns:
- value
- Throws:
IllegalArgumentException
- if already set to the same value
-
first
Gets the first non-null, after enforces that all remaining values are either null or equal to the first value.- Type Parameters:
T
- value type- Parameters:
values
- values to set or enforce as already set- Returns:
- first non-null value
- Throws:
IllegalArgumentException
- if any values are non-null and not equal to the returned value
-
first
Enforces that a value is either not already set or is already set to the same value.- Type Parameters:
T
- value type- Parameters:
current
- current valuevalue
- value to set or enforce as already setmessageSupplier
- provides a message forIllegalArgumentException
if current was already set to a different value- Returns:
- value
- Throws:
IllegalArgumentException
- if already set to the same value
-
represents
Determines if either or both objects are null, then if both non-null if both areequals(Object, Object)
.This method is the boolean equivalent of
first(Object...)
- Parameters:
a
- an objectb
- another object- Returns:
- true if either object is null or if both are equal; else false
-
requireType
Require value to be an instance of a specific type or null.- Type Parameters:
T
- required type- Parameters:
type
- required typevalue
- value- Returns:
- typed value
- Throws:
IllegalArgumentException
- if the types don't match
-
require
Require a condition to be true for a value.- Type Parameters:
T
- value type- Parameters:
value
- valuecondition
- condition to verify- Returns:
- value
- Throws:
IllegalArgumentException
- if the types don't match
-
require
Require a condition to be true for a value if non-null.- Type Parameters:
T
- value type- Parameters:
value
- valuecondition
- condition to verifymessage
- provides a message forIllegalArgumentException
- Returns:
- value
- Throws:
IllegalArgumentException
- if the types don't match
-
require
Require a condition to be true for a value if non-null.- Type Parameters:
T
- value type- Parameters:
value
- valuecondition
- condition to verifymessageSupplier
- provides a message forIllegalArgumentException
- Returns:
- value
- Throws:
IllegalArgumentException
- if the types don't match
-
convert
Passes a value through a conversion function if non-null.- Type Parameters:
S
- source typeT
- result type- Parameters:
value
- valueconversionFunction
- conversion function- Returns:
- converted value
-
compareNullCheck
Perform identity and and null check on two objects, returning a valid value forComparable.compareTo(Object)
if any of the checks result in a conclusive result.- Parameters:
o1
- any objecto2
- any object- Returns:
- 0 if o1 == o2, -1 if o1 is null, 1 if o2 is null; otherwise, return null indicating that compareTo should continue to inspect each object's specific data.
-
compareTo
Compares two objects with null checks (seecompareNullCheck(Object, Object)
) and also consistent sort order based for objects that don't implementComparable
.- Parameters:
o1
- any objecto2
- any object- Returns:
- Valid
Comparator
return value enforcing consistent sort order within the same JVM instance.
-
hashCode
Generates a hash code for a top-level object based on related values (i.e. field, bean property values, etc).- Parameters:
oa
- related values- Returns:
- hash code
-
hashCodeSuper
Generate a hash code for a subclass object based on its parent class' hash code and related values.- Parameters:
superHashCode
- parent class hash codeoa
- related values- Returns:
- hash code
-
typeCheck
public static <T> boolean typeCheck(T o1, T o2) Determine if two objects are both non-null instances of the same class. This method is useful as a null and type safety check when implementing equals. If this returns true, and the type of one of the objects is known, then it is safe to cast the other object to the same type.- Type Parameters:
T
- object type- Parameters:
o1
- any objecto2
- any object- Returns:
- True if both objects are not null and instances of the same class.
-
typeCheck
Determine if two objects are both instances of a specific class, or subclasses of that class. This method is useful as a null and type safety check when implementing equals. If this returns true, then it is safe to cast the both objects to the type provided.- Type Parameters:
T
- object type- Parameters:
o1
- any objecto2
- any objecttype
- the type to check, may be null for the behavior outlined intypeCheck(Object, Object)
.- Returns:
- True if both objects are not null and instances of the given type, or are the same class if type is null.
-
equals
Determine if two objects are equal, checking first for identity and null.- Parameters:
o1
- any objecto2
- any object- Returns:
- true if o1 and o2 refer to the same object, are both null, or if o1.equals(o2) returns true. Otherwise, return false.
-
waitFor
public static void waitFor(Object lock, BooleanSupplier condition, Duration timeout) throws InterruptedException, TimeoutException Waits until a condition is met or a timeout interval expires.- Parameters:
lock
- object to synchronize oncondition
- condition to wait fortimeout
- timeout interval- Throws:
InterruptedException
- if the current thread is interrupted while waiting for the condition to be metTimeoutException
- if the timeout interval expires before the condition is met
-
waitFor
public static void waitFor(Object lock, BooleanSupplier condition, Duration timeout, Supplier<TimeoutException> timeoutFactory) throws InterruptedException, TimeoutException Waits until a condition is met or a timeout interval expires.- Parameters:
lock
- object to synchronize oncondition
- condition to wait fortimeout
- timeout intervaltimeoutFactory
- creates a timeout exception to be thrown if the condition is not met before the expiration time- Throws:
InterruptedException
- if the current thread is interrupted while waiting for the condition to be metTimeoutException
- if the timeout interval expires before the condition is met
-
waitFor
public static void waitFor(Object lock, BooleanSupplier condition, Instant expires) throws InterruptedException, TimeoutException Waits until a condition is met or a timeout interval expires.- Parameters:
lock
- object to synchronize on to receive status change notificationscondition
- condition to wait forexpires
- timeout interval expiration time- Throws:
InterruptedException
- if the current thread is interrupted while waiting for the condition to be metTimeoutException
- if the timeout interval expires before the condition is met
-
waitFor
public static void waitFor(Object lock, BooleanSupplier condition, Instant expires, Supplier<TimeoutException> timeoutFactory) throws InterruptedException, TimeoutException Waits until a condition is met or a timeout interval expires.- Parameters:
lock
- object to synchronize on to receive status change notificationscondition
- condition to wait forexpires
- timeout interval expiration timetimeoutFactory
- creates a timeout exception to be thrown if the condition is not met before the expiration time- Throws:
InterruptedException
- if the current thread is interrupted while waiting for the condition to be metTimeoutException
- if the timeout interval expires before the condition is met
-