Object#equals(Object) methods.
This class provides methods to build a good equals method for any
class. It follows rules laid out in
Effective Java
, by Joshua Bloch. In particular the rule for comparing doubles,
floats, and arrays can be tricky. Also, making sure that
equals() and hashCode() are consistent can be
difficult.
Two Objects that compare as equals must generate the same hash code, but two Objects with the same hash code do not have to be equal.
All relevant fields should be included in the calculation of equals. Derived fields may be ignored. In particular, any field used in generating a hash code must be used in the equals method, and vice versa.
Typical use for the code is as follows:
public boolean equals(Object obj) {
if (obj == null) { return false; }
if (obj == this) { return true; }
if (obj.getClass() != getClass()) {
return false;
}
MyClass rhs = (MyClass) obj;
return new EqualsBuilder()
.appendSuper(super.equals(obj))
.append(field1, rhs.field1)
.append(field2, rhs.field2)
.append(field3, rhs.field3)
.isEquals();
}
Alternatively, there is a method that uses reflection to determine
the fields to test. Because these fields are usually private, the method,
reflectionEquals, uses AccessibleObject.setAccessible to
change the visibility of the fields. This will fail under a security
manager, unless the appropriate permissions are set up correctly. It is
also slower than testing explicitly. Non-primitive fields are compared using
equals().
A typical invocation for this method would look like:
public boolean equals(Object obj) {
return EqualsBuilder.reflectionEquals(this, obj);
}
The EqualsExclude annotation can be used to exclude fields from being
used by the reflectionEquals methods.
| Modifier and Type | Field and Description |
|---|---|
| private List | |
| private String[] | |
| private boolean | isEquals
If the fields tested are equals. |
| private Class | |
| private static final ThreadLocal | REGISTRY
A registry of objects used by reflection methods to detect cyclical object references and avoid infinite loops. |
| private boolean | |
| private boolean |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public EqualsBuilder | Returns: this instance.the left-hand side lhs, final boolean booleanthe right-hand side rhs)booleanTest if two |
| public EqualsBuilder | Returns: this instance.the left-hand side lhs, final boolean[] boolean[]the right-hand side rhs)boolean[]Deep comparison of array of |
| public EqualsBuilder | Returns: this instance.the left-hand side lhs, final byte bytethe right-hand side rhs)byteTest if two |
| public EqualsBuilder | Returns: this instance.the left-hand side lhs, final byte[] byte[]the right-hand side rhs)byte[]Deep comparison of array of |
| public EqualsBuilder | Returns: this instance.the left-hand side lhs, final char charthe right-hand side rhs)charTest if two |
| public EqualsBuilder | Returns: this instance.the left-hand side lhs, final char[] char[]the right-hand side rhs)char[]Deep comparison of array of |
| public EqualsBuilder | Returns: this instance.the left-hand side lhs, final double doublethe right-hand side rhs)doubleTest if two |
| public EqualsBuilder | Returns: this instance.the left-hand side lhs, final double[] double[]the right-hand side rhs)double[]Deep comparison of array of |
| public EqualsBuilder | Returns: this instance.the left-hand side lhs, final float floatthe right-hand side rhs)floatTest if two |
| public EqualsBuilder | Returns: this instance.the left-hand side lhs, final float[] float[]the right-hand side rhs)float[]Deep comparison of array of |
| public EqualsBuilder | Returns: this instance.the left-hand side lhs, final int intthe right-hand side rhs)intTest if two |
| public EqualsBuilder | Returns: this instance.the left-hand side lhs, final int[] int[]the right-hand side rhs)int[]Deep comparison of array of |
| public EqualsBuilder | Returns: this instance.the left-hand side lhs, final long longthe right-hand side rhs)longTest if two |
| public EqualsBuilder | Returns: this instance.the left-hand side lhs, final long[] long[]the right-hand side rhs)long[]Deep comparison of array of |
| public EqualsBuilder | Returns: this instance.the left-hand side object lhs, final Object the right-hand side object rhs)Test if two |
| public EqualsBuilder | |
| public EqualsBuilder | Returns: this instance.the left-hand side lhs, final short shortthe right-hand side rhs)shortTest if two |
| public EqualsBuilder | Returns: this instance.the left-hand side lhs, final short[] short[]the right-hand side rhs)short[]Deep comparison of array of |
| private void | appendArray(final Object
the left-hand side object, an array lhs, final Object the right-hand side object rhs)Test if an |
| public EqualsBuilder | Returns: this instance.the result of calling superEquals)super.equals()Adds the result of |
| public Boolean | Returns: true if all of the fields that have been checked
are equal, false otherwise.Implements org. Returns |
| pack-priv static Pair | Returns: the pairthis objectthe other object rhs)Converters value pair into a register pair. |
| pack-priv static Set | Returns: Set the registry of objects being traversedGets the registry of object pairs being traversed by the reflection methods in the current thread. |
| public boolean | |
| pack-priv static boolean | Returns: booleantrue if the registry contains the given object.this object to lookup in registrythe other object to lookup on registry rhs)Tests whether the registry contains the given object pair. |
| public EqualsBuilder | Returns: this instance.the left-hand side object lhs, final Object the right-hand side object rhs)Tests if two |
| private void | reflectionAppend(final Object
the left-hand side object lhs, final Object the right-hand side object rhs, final Class<?> the class to append details of clazz)Appends the fields and values defined by the given object of the given Class. |
| public static boolean | Returns: true if the two Objects have tested equals.this objectthe other object rhs, final boolean whether to include transient fields testTransients)This method uses reflection to determine if the two |
| public static boolean | Returns: true if the two Objects have tested equals.this objectthe other object rhs, final boolean whether to include transient fields testTransients, final Class<?> the superclass to reflect up to (inclusive),
may be reflectUpToClass, final boolean nullwhether to call reflection equals on non-primitive
fields recursively. testRecursive, final String... array of field names to exclude from testing excludeFields)This method uses reflection to determine if the two |
| public static boolean | Returns: true if the two Objects have tested equals.this objectthe other object rhs, final boolean whether to include transient fields testTransients, final Class<?> the superclass to reflect up to (inclusive),
may be reflectUpToClass, final String... nullarray of field names to exclude from testing excludeFields)This method uses reflection to determine if the two |
| public static boolean | Returns: true if the two Objects have tested equals.this objectthe other object rhs, final Collection<String> Collection of String field names to exclude from testing excludeFields)This method uses reflection to determine if the two |
| public static boolean | Returns: true if the two Objects have tested equals.this objectthe other object rhs, final String... array of field names to exclude from testing excludeFields)This method uses reflection to determine if the two |
| private static void | |
| public void | |
| public EqualsBuilder | Returns: this instance.classes to bypass reflection test bypassReflectionClasses)Sets |
| protected void | |
| public EqualsBuilder | Returns: this instance.the fields to exclude excludeFields)Sets field names to be excluded by reflection tests. |
| public EqualsBuilder | Returns: this instance.the super class to reflect up to reflectUpToClass)Sets the superclass to reflect up to at reflective tests. |
| public EqualsBuilder | Returns: this instance.whether to do a recursive test testRecursive)Sets whether to test fields recursively, instead of using their equals method, when reflectively comparing objects. |
| public EqualsBuilder | Returns: this instance.whether to test transient fields testTransients)Sets whether to include transient fields when reflectively comparing objects. |
| private static void | unregister(final Object
this object to unregisterthe other object to unregister rhs)Unregisters the given object pair. |
| bypassReflectionClasses | back to summary |
|---|---|
| private List<Class<?>> bypassReflectionClasses | |
| excludeFields | back to summary |
|---|---|
| private String[] excludeFields | |
| isEquals | back to summary |
|---|---|
| private boolean isEquals If the fields tested are equals.
The default value is | |
| reflectUpToClass | back to summary |
|---|---|
| private Class<?> reflectUpToClass | |
| REGISTRY | back to summary |
|---|---|
| private static final ThreadLocal<Set<Pair<IDKey, IDKey>>> REGISTRY A registry of objects used by reflection methods to detect cyclical object references and avoid infinite loops.
| |
| testRecursive | back to summary |
|---|---|
| private boolean testRecursive | |
| testTransients | back to summary |
|---|---|
| private boolean testTransients | |
| EqualsBuilder | back to summary |
|---|---|
| public EqualsBuilder() Constructor for EqualsBuilder. Starts off assuming that equals is
| |
| append | back to summary |
|---|---|
| public EqualsBuilder append(final boolean lhs, final boolean rhs) Test if two
| |
| append | back to summary |
|---|---|
| public EqualsBuilder append(final boolean[] lhs, final boolean[] rhs) Deep comparison of array of The method
| |
| append | back to summary |
|---|---|
| public EqualsBuilder append(final byte lhs, final byte rhs) Test if two
| |
| append | back to summary |
|---|---|
| public EqualsBuilder append(final byte[] lhs, final byte[] rhs) Deep comparison of array of The method
| |
| append | back to summary |
|---|---|
| public EqualsBuilder append(final char lhs, final char rhs) Test if two
| |
| append | back to summary |
|---|---|
| public EqualsBuilder append(final char[] lhs, final char[] rhs) Deep comparison of array of The method
| |
| append | back to summary |
|---|---|
| public EqualsBuilder append(final double lhs, final double rhs) Test if two This handles NaNs, Infinities, and It is compatible with the hash code generated by
| |
| append | back to summary |
|---|---|
| public EqualsBuilder append(final double[] lhs, final double[] rhs) Deep comparison of array of The method
| |
| append | back to summary |
|---|---|
| public EqualsBuilder append(final float lhs, final float rhs) Test if two This handles NaNs, Infinities, and It is compatible with the hash code generated by
| |
| append | back to summary |
|---|---|
| public EqualsBuilder append(final float[] lhs, final float[] rhs) Deep comparison of array of The method
| |
| append | back to summary |
|---|---|
| public EqualsBuilder append(final int lhs, final int rhs) Test if two
| |
| append | back to summary |
|---|---|
| public EqualsBuilder append(final int[] lhs, final int[] rhs) Deep comparison of array of The method
| |
| append | back to summary |
|---|---|
| public EqualsBuilder append(final long lhs, final long rhs) Test if two
| |
| append | back to summary |
|---|---|
| public EqualsBuilder append(final long[] lhs, final long[] rhs) Deep comparison of array of The method
| |
| append | back to summary |
|---|---|
| public EqualsBuilder append(final Object lhs, final Object rhs) Test if two
| |
| append | back to summary |
|---|---|
| public EqualsBuilder append(final Object[] lhs, final Object[] rhs) Performs a deep comparison of two This also will be called for the top level of multi-dimensional, ragged, and multi-typed arrays. Note that this method does not compare the type of the arrays; it only compares the contents.
| |
| append | back to summary |
|---|---|
| public EqualsBuilder append(final short lhs, final short rhs) Test if two
| |
| append | back to summary |
|---|---|
| public EqualsBuilder append(final short[] lhs, final short[] rhs) Deep comparison of array of The method
| |
| appendArray | back to summary |
|---|---|
| private void appendArray(final Object lhs, final Object rhs) Test if an | |
| appendSuper | back to summary |
|---|---|
| public EqualsBuilder appendSuper(final boolean superEquals) Adds the result of
| |
| build | back to summary |
|---|---|
| public Boolean build() Implements org. Returns | |
| getRegisterPair | back to summary |
|---|---|
| pack-priv static Pair Converters value pair into a register pair. | |
| getRegistry | back to summary |
|---|---|
| pack-priv static Set Gets the registry of object pairs being traversed by the reflection methods in the current thread. | |
| isEquals | back to summary |
|---|---|
| public boolean isEquals() Returns
| |
| isRegistered | back to summary |
|---|---|
| pack-priv static boolean isRegistered(final Object lhs, final Object rhs) Tests whether the registry contains the given object pair. Used by the reflection methods to avoid infinite loops. Objects might be swapped therefore a check is needed if the object pair is registered in given or swapped order. | |
| reflectionAppend | back to summary |
|---|---|
| public EqualsBuilder reflectionAppend(final Object lhs, final Object rhs) Tests if two It uses If the testTransients field is set to Static fields will not be included. Superclass fields will be appended
up to and including the specified superclass in field Field names listed in field If either class of the compared objects is contained in
| |
| reflectionAppend | back to summary |
|---|---|
| private void reflectionAppend(final Object lhs, final Object rhs, final Class<?> clazz) Appends the fields and values defined by the given object of the given Class. | |
| reflectionEquals | back to summary |
|---|---|
| public static boolean reflectionEquals(final Object lhs, final Object rhs, final boolean testTransients) This method uses reflection to determine if the two It uses If the TestTransients parameter is set to Static fields will not be tested. Superclass fields will be included.
| |
| reflectionEquals | back to summary |
|---|---|
| public static boolean reflectionEquals(final Object lhs, final Object rhs, final boolean testTransients, final Class<?> reflectUpToClass, final boolean testRecursive, final String... excludeFields) This method uses reflection to determine if the two It uses If the testTransients parameter is set to Static fields will not be included. Superclass fields will be appended up to and including the specified superclass. A null superclass is treated as java.lang.Object. If the testRecursive parameter is set to
| |
| reflectionEquals | back to summary |
|---|---|
| public static boolean reflectionEquals(final Object lhs, final Object rhs, final boolean testTransients, final Class<?> reflectUpToClass, final String... excludeFields) This method uses reflection to determine if the two It uses If the testTransients parameter is set to Static fields will not be included. Superclass fields will be appended up to and including the specified superclass. A null superclass is treated as java.lang.Object.
| |
| reflectionEquals | back to summary |
|---|---|
| public static boolean reflectionEquals(final Object lhs, final Object rhs, final Collection<String> excludeFields) This method uses reflection to determine if the two It uses Transient members will be not be tested, as they are likely derived fields, and not part of the value of the Object. Static fields will not be tested. Superclass fields will be included.
| |
| reflectionEquals | back to summary |
|---|---|
| public static boolean reflectionEquals(final Object lhs, final Object rhs, final String... excludeFields) This method uses reflection to determine if the two It uses Transient members will be not be tested, as they are likely derived fields, and not part of the value of the Object. Static fields will not be tested. Superclass fields will be included.
| |
| register | back to summary |
|---|---|
| private static void register(final Object lhs, final Object rhs) Registers the given object pair. Used by the reflection methods to avoid infinite loops. | |
| reset | back to summary |
|---|---|
| public void reset() Reset the EqualsBuilder so you can use the same object again.
| |
| setBypassReflectionClasses | back to summary |
|---|---|
| public EqualsBuilder setBypassReflectionClasses(final List<Class<?>> bypassReflectionClasses) Sets Here you should name classes having non-transient fields which are cache fields being set lazily.
| |
| setEquals | back to summary |
|---|---|
| protected void setEquals(final boolean isEquals) Sets the
| |
| setExcludeFields | back to summary |
|---|---|
| public EqualsBuilder setExcludeFields(final String... excludeFields) Sets field names to be excluded by reflection tests.
| |
| setReflectUpToClass | back to summary |
|---|---|
| public EqualsBuilder setReflectUpToClass(final Class<?> reflectUpToClass) Sets the superclass to reflect up to at reflective tests.
| |
| setTestRecursive | back to summary |
|---|---|
| public EqualsBuilder setTestRecursive(final boolean testRecursive) Sets whether to test fields recursively, instead of using their equals method, when reflectively comparing objects.
String objects, which cache a hash value, are automatically excluded from recursive testing.
You may specify other exceptions by calling
| |
| setTestTransients | back to summary |
|---|---|
| public EqualsBuilder setTestTransients(final boolean testTransients) Sets whether to include transient fields when reflectively comparing objects.
| |
| unregister | back to summary |
|---|---|
| private static void unregister(final Object lhs, final Object rhs) Unregisters the given object pair. Used by the reflection methods to avoid infinite loops. | |