EqualsBuilder¶
- public class org.apache.commons.lang3.builder.EqualsBuilder extends AbstractReflection implements Builder<Boolean>[source]¶
Assists in implementing
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 thatequals()andhashCode()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, usesAccessibleObject.setAccessibleto 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 usingequals().See also
AbstractBuilder#setForceAccessible(boolean)A typical invocation for this method would look like:
public boolean equals(Object obj) { return EqualsBuilder.reflectionEquals(this, obj); }
The
EqualsExcludeannotation can be used to exclude fields from being used by thereflectionEqualsmethods.- Since:
1.0
- See also:
Constructors¶
Constructor for EqualsBuilder. |
Methods¶
Test if two |
|
Deep comparison of array of |
|
Test if two |
|
Deep comparison of array of |
|
Test if two |
|
Deep comparison of array of |
|
Test if two |
|
Deep comparison of array of |
|
Test if two |
|
Deep comparison of array of |
|
Test if two |
|
Deep comparison of array of |
|
Test if two |
|
Performs a deep comparison of two |
|
Test if two |
|
Deep comparison of array of |
|
Test if two |
|
Deep comparison of array of |
|
Adds the result of |
|
Returns |
|
Constructs a new Builder. |
|
Tests whether all fields checked so far are equal. |
|
Tests if two |
|
Uses reflection to determine if the two |
|
|
Uses reflection to determine if the two |
Uses reflection to determine if the two |
|
Uses reflection to determine if the two |
|
Uses reflection to determine if the two |
|
Reset the EqualsBuilder so you can use the same object again. |
|
Sets |
|
Sets the |
|
Sets field names to be excluded by reflection tests. |
|
Sets the superclass to reflect up to at reflective tests. |
|
Sets whether to test fields recursively, instead of using their equals method, when reflectively comparing objects. |
|
Sets whether to include transient fields when reflectively comparing objects. |
- public EqualsBuilder append(boolean lhs, boolean rhs)[source]¶
Test if two
booleanss are equal.- Parameters:
lhs – The left-hand side
booleanrhs – The right-hand side
boolean
- Returns:
thisinstance.
- public EqualsBuilder append(boolean[] lhs, boolean[] rhs)[source]¶
Deep comparison of array of
boolean. Length and all values are compared.The method
#append(boolean, boolean)is used.- Parameters:
lhs – The left-hand side
boolean[]rhs – The right-hand side
boolean[]
- Returns:
thisinstance.
- public EqualsBuilder append(byte lhs, byte rhs)[source]¶
Test if two
bytes are equal.- Parameters:
lhs – The left-hand side
byterhs – The right-hand side
byte
- Returns:
thisinstance.
- public EqualsBuilder append(byte[] lhs, byte[] rhs)[source]¶
Deep comparison of array of
byte. Length and all values are compared.The method
#append(byte, byte)is used.- Parameters:
lhs – The left-hand side
byte[]rhs – The right-hand side
byte[]
- Returns:
thisinstance.
- public EqualsBuilder append(char lhs, char rhs)[source]¶
Test if two
chars are equal.- Parameters:
lhs – The left-hand side
charrhs – The right-hand side
char
- Returns:
thisinstance.
- public EqualsBuilder append(char[] lhs, char[] rhs)[source]¶
Deep comparison of array of
char. Length and all values are compared.The method
#append(char, char)is used.- Parameters:
lhs – The left-hand side
char[]rhs – The right-hand side
char[]
- Returns:
thisinstance.
- public EqualsBuilder append(double lhs, double rhs)[source]¶
Test if two
doubles are equal by testing that the pattern of bits returned bydoubleToLongare equal.This handles NaNs, Infinities, and
-0.0.It is compatible with the hash code generated by
HashCodeBuilder.- Parameters:
lhs – The left-hand side
doublerhs – The right-hand side
double
- Returns:
thisinstance.
- public EqualsBuilder append(double[] lhs, double[] rhs)[source]¶
Deep comparison of array of
double. Length and all values are compared.The method
#append(double, double)is used.- Parameters:
lhs – The left-hand side
double[]rhs – The right-hand side
double[]
- Returns:
thisinstance.
- public EqualsBuilder append(float lhs, float rhs)[source]¶
Test if two
floats are equal by testing that the pattern of bits returned by doubleToLong are equal.This handles NaNs, Infinities, and
-0.0.It is compatible with the hash code generated by
HashCodeBuilder.- Parameters:
lhs – The left-hand side
floatrhs – The right-hand side
float
- Returns:
thisinstance.
- public EqualsBuilder append(float[] lhs, float[] rhs)[source]¶
Deep comparison of array of
float. Length and all values are compared.The method
#append(float, float)is used.- Parameters:
lhs – The left-hand side
float[]rhs – The right-hand side
float[]
- Returns:
thisinstance.
- public EqualsBuilder append(int lhs, int rhs)[source]¶
Test if two
ints are equal.- Parameters:
lhs – The left-hand side
intrhs – The right-hand side
int
- Returns:
thisinstance.
- public EqualsBuilder append(int[] lhs, int[] rhs)[source]¶
Deep comparison of array of
int. Length and all values are compared.The method
#append(int, int)is used.- Parameters:
lhs – The left-hand side
int[]rhs – The right-hand side
int[]
- Returns:
thisinstance.
- public EqualsBuilder append(Object lhs, Object rhs)[source]¶
Test if two
Objects are equal using either ##reflectionAppend(Object, Object), if object are non primitives (or wrapper of primitives) or if fieldtestRecursiveis set tofalse. Otherwise, using theirequalsmethod.- Parameters:
lhs – The left-hand side object
rhs – The right-hand side object
- Returns:
thisinstance.
- public EqualsBuilder append(Object[] lhs, Object[] rhs)[source]¶
Performs a deep comparison of two
Objectarrays.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.
- Parameters:
lhs – The left-hand side
Object[]rhs – The right-hand side
Object[]
- Returns:
thisinstance.
- public EqualsBuilder append(long lhs, long rhs)[source]¶
Test if two
longs are equal.- Parameters:
lhs – the left-hand side
longrhs – the right-hand side
long
- Returns:
thisinstance.
- public EqualsBuilder append(long[] lhs, long[] rhs)[source]¶
Deep comparison of array of
long. Length and all values are compared.The method
#append(long, long)is used.- Parameters:
lhs – The left-hand side
long[]rhs – The right-hand side
long[]
- Returns:
thisinstance.
- public EqualsBuilder append(short lhs, short rhs)[source]¶
Test if two
shorts are equal.- Parameters:
lhs – The left-hand side
shortrhs – The right-hand side
short
- Returns:
thisinstance.
- public EqualsBuilder append(short[] lhs, short[] rhs)[source]¶
Deep comparison of array of
short. Length and all values are compared.The method
#append(short, short)is used.- Parameters:
lhs – The left-hand side
short[]rhs – The right-hand side
short[]
- Returns:
thisinstance.
- public EqualsBuilder appendSuper(boolean superEquals)[source]¶
Adds the result of
super.equals()to this builder.- Parameters:
superEquals – The result of calling
super.equals()- Returns:
thisinstance.- Since:
2.0
- @Override public Boolean build()[source]¶
Returns
trueif the fields that have been checked are all equal.- Returns:
trueif all of the fields that have been checked are equal,falseotherwise.- Since:
3.0
- public static EqualsBuilder.Builder builder()[source]¶
Constructs a new Builder.
- Returns:
A new Builder.
- public boolean isEquals()[source]¶
Tests whether all fields checked so far are equal.
- Returns:
boolean
- public EqualsBuilder reflectionAppend(Object lhs, Object rhs)[source]¶
Tests if two
objectsby using reflection.It uses
AccessibleObject.setAccessibleto gain access to private fields. This means that it will throw a security exception if run under a security manager, if the permissions are not set up correctly. It is also not as efficient as testing explicitly. Non-primitive fields are compared usingequals().If the testTransients field is set to
true, transient members will be tested, otherwise they are ignored, as they are likely derived fields, and not part of the value of theObject.Static fields will not be included. Superclass fields will be appended up to and including the specified superclass in field
reflectUpToClass. A null superclass is treated as java.lang.Object.Field names listed in field
excludeFieldswill be ignored.If either class of the compared objects is contained in
bypassReflectionClasses, both objects are compared by calling the equals method of the left-hand side object with the right-hand side object as an argument.- Parameters:
lhs – The left-hand side object
rhs – The right-hand side object
- Returns:
thisinstance.
- public static boolean reflectionEquals(Object lhs, Object rhs, boolean testTransients)[source]¶
Uses reflection to determine if the two
Objects are equal.It uses
AccessibleObject.setAccessibleto gain access to private fields. This means that it will throw a security exception if run under a security manager, if the permissions are not set up correctly. It is also not as efficient as testing explicitly. Non-primitive fields are compared usingequals().If the TestTransients parameter is set to
true, transient members will be tested, otherwise they are ignored, as they are likely derived fields, and not part of the value of theObject.Static fields will not be tested. Superclass fields will be included.
- Parameters:
lhs –
thisobjectrhs – The other object
testTransients – whether to include transient fields
- Returns:
trueif the two Objects have tested equals.- See also:
- public static boolean reflectionEquals(Object lhs, Object rhs, boolean testTransients, Class<?> reflectUpToClass, boolean testRecursive, String... excludeFields)[source]¶
Uses reflection to determine if the two
Objects are equal.It uses
AccessibleObject.setAccessibleto gain access to private fields. This means that it will throw a security exception if run under a security manager, if the permissions are not set up correctly. It is also not as efficient as testing explicitly. Non-primitive fields are compared usingequals().If the testTransients parameter is set to
true, transient members will be tested, otherwise they are ignored, as they are likely derived fields, and not part of the value of theObject.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
true, non primitive (and non primitive wrapper) field types will be compared byEqualsBuilderrecursively instead of invoking theirequals()method. Leading to a deep reflection equals test.Note on graph shape: the internal registry that prevents infinite recursion on cyclic object graphs is a visit stack, not a visited set - object pairs reachable more than once through shared (acyclic) references are re-compared on every path. On deeply nested graphs with many shared references (reference “diamonds”), the comparison cost can grow exponentially with nesting depth. Do not use recursive reflection equality on object graphs built from untrusted input (for example, graphs materialized by an identity-preserving deserializer).
- Parameters:
lhs –
thisobjectrhs – The other object
testTransients – whether to include transient fields
reflectUpToClass – The superclass to reflect up to (inclusive), may be
nulltestRecursive – whether to call reflection equals on non-primitive fields recursively.
excludeFields – array of field names to exclude from testing
- Returns:
trueif the two Objects have tested equals.- See also:
- Since:
3.6
- public static boolean reflectionEquals(Object lhs, Object rhs, boolean testTransients, Class<?> reflectUpToClass, String... excludeFields)[source]¶
Uses reflection to determine if the two
Objects are equal.It uses
AccessibleObject.setAccessibleto gain access to private fields. This means that it will throw a security exception if run under a security manager, if the permissions are not set up correctly. It is also not as efficient as testing explicitly. Non-primitive fields are compared usingequals().If the testTransients parameter is set to
true, transient members will be tested, otherwise they are ignored, as they are likely derived fields, and not part of the value of theObject.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.
- Parameters:
lhs –
thisobjectrhs – The other object
testTransients – whether to include transient fields
reflectUpToClass – The superclass to reflect up to (inclusive), may be
nullexcludeFields – array of field names to exclude from testing
- Returns:
trueif the two Objects have tested equals.- See also:
- Since:
2.0
- public static boolean reflectionEquals(Object lhs, Object rhs, String... excludeFields)[source]¶
Uses reflection to determine if the two
Objects are equal.It uses
AccessibleObject.setAccessibleto gain access to private fields. This means that it will throw a security exception if run under a security manager, if the permissions are not set up correctly. It is also not as efficient as testing explicitly. Non-primitive fields are compared usingequals().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.
- Parameters:
lhs –
thisobjectrhs – The other object
excludeFields – array of field names to exclude from testing
- Returns:
trueif the two Objects have tested equals.- See also:
- public static boolean reflectionEquals(Object lhs, Object rhs, Collection<String> excludeFields)[source]¶
Uses reflection to determine if the two
Objects are equal.It uses
AccessibleObject.setAccessibleto gain access to private fields. This means that it will throw a security exception if run under a security manager, if the permissions are not set up correctly. It is also not as efficient as testing explicitly. Non-primitive fields are compared usingequals().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.
- Parameters:
lhs –
thisobjectrhs – The other object
excludeFields – Collection of String field names to exclude from testing
- Returns:
trueif the two Objects have tested equals.- See also:
- public void reset()[source]¶
Reset the EqualsBuilder so you can use the same object again.
- Since:
2.5
- public EqualsBuilder setBypassReflectionClasses(List<Class<?>> bypassReflectionClasses)[source]¶
Sets
Classes whose instances should be compared by calling theirequalsalthough being in recursive mode. So the fields of these classes will not be compared recursively by reflection.Here you should name classes having non-transient fields which are cache fields being set lazily.- Parameters:
bypassReflectionClasses – classes to bypass reflection test
- Returns:
thisinstance.- See also:
- Since:
3.8
- protected void setEquals(boolean isEquals)[source]¶
Sets the
isEqualsvalue.- Parameters:
isEquals – The value to set.
- Since:
2.1
- public EqualsBuilder setExcludeFields(String... excludeFields)[source]¶
Sets field names to be excluded by reflection tests.
- Parameters:
excludeFields – The fields to exclude
- Returns:
thisinstance.- Since:
3.6
- public EqualsBuilder setReflectUpToClass(Class<?> reflectUpToClass)[source]¶
Sets the superclass to reflect up to at reflective tests.
- Parameters:
reflectUpToClass – The super class to reflect up to
- Returns:
thisinstance.- Since:
3.6
- public EqualsBuilder setTestRecursive(boolean testRecursive)[source]¶
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
#setBypassReflectionClasses(List).Cycle protection is a visit stack, not a visited set: shared (acyclic) references are re-compared on every path, so deeply nested graphs with many shared references can be exponentially expensive to compare. Avoid on object graphs built from untrusted input.
- Parameters:
testRecursive – whether to do a recursive test
- Returns:
thisinstance.- See also:
- Since:
3.6
- public EqualsBuilder setTestTransients(boolean testTransients)[source]¶
Sets whether to include transient fields when reflectively comparing objects.
- Parameters:
testTransients – whether to test transient fields
- Returns:
thisinstance.- Since:
3.6