ObjectContracts
Provides fluent composition for Objects' equals, hashCode and toString.
API
ObjectContracts.java
class ObjectContracts {
ToString<T> toString(String name, Function<T, ?> getter)
Equality<T> checkEquals(Function<T, ?> getter)
Hashing<T> hashing(Function<T, ?> getter)
ObjectContract<T> contract(Class<T> objectClass)
ObjectContract<T> parse(Class<T> target, String propertyNames)
}
Example Usage
For example:
@RequiredArgsConstructor(staticName = "of")
public static class ComplexNumber implements Comparable<ComplexNumber> {
@Getter private final int real;
@Getter private final int imaginary;
private ObjectContracts.ObjectContract<ComplexNumber> contract
= ObjectContracts.contract(ComplexNumber.class)
.thenUse("real", ComplexNumber::getReal)
.thenUse("imaginary", ComplexNumber::getImaginary);
@Override
public boolean equals(Object o) {
return contract.equals(this, o);
}
@Override
public int hashCode() {
return contract.hashCode(this);
}
@Override
public int compareTo(final ComplexNumber other) {
return contract.compare(this, other);
}
@Override
public String toString() {
return contract.toString(this);
}
}
|
There are a number of deprecated methods that identify property names as strings. These should not be use, as they use are not type-safe and also use reflection heavily and so impose a performance hit. |