On this page

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 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().

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 EqualsExclude annotation can be used to exclude fields from being used by the reflectionEquals methods.

Since:

1.0

See also:

AbstractBuilder#setForceAccessible(boolean)

Constructors

EqualsBuilder()

Constructor for EqualsBuilder.

public EqualsBuilder()[source]

Constructor for EqualsBuilder.

Starts off assuming that equals is true.

See also:

Object#equals(Object)

Methods

append(boolean, boolean)

Test if two booleanss are equal.

append(boolean[], boolean[])

Deep comparison of array of boolean.

append(byte, byte)

Test if two bytes are equal.

append(byte[], byte[])

Deep comparison of array of byte.

append(char, char)

Test if two chars are equal.

append(char[], char[])

Deep comparison of array of char.

append(double, double)

Test if two doubles are equal by testing that the pattern of bits returned by doubleToLong are equal.

append(double[], double[])

Deep comparison of array of double.

append(float, float)

Test if two floats are equal by testing that the pattern of bits returned by doubleToLong are equal.

append(float[], float[])

Deep comparison of array of float.

append(int, int)

Test if two ints are equal.

append(int[], int[])

Deep comparison of array of int.

append(Object, Object)

Test if two Objects are equal using either ##reflectionAppend(Object, Object), if object are non primitives (or wrapper of primitives) or if field testRecursive is set to false.

append(Object[], Object[])

Performs a deep comparison of two Object arrays.

append(long, long)

Test if two longs are equal.

append(long[], long[])

Deep comparison of array of long.

append(short, short)

Test if two shorts are equal.

append(short[], short[])

Deep comparison of array of short.

appendSuper(boolean)

Adds the result of super.equals() to this builder.

build()

Returns true if the fields that have been checked are all equal.

builder()

Constructs a new Builder.

isEquals()

Tests whether all fields checked so far are equal.

reflectionAppend(Object, Object)

Tests if two objects by using reflection.

reflectionEquals(Object, Object, boolean)

Uses reflection to determine if the two Objects are equal.

reflectionEquals(Object, Object, boolean, Class, boolean, String[])

Uses reflection to determine if the two Objects are equal.

reflectionEquals(Object, Object, boolean, Class, String[])

Uses reflection to determine if the two Objects are equal.

reflectionEquals(Object, Object, String[])

Uses reflection to determine if the two Objects are equal.

reflectionEquals(Object, Object, Collection)

Uses reflection to determine if the two Objects are equal.

reset()

Reset the EqualsBuilder so you can use the same object again.

setBypassReflectionClasses(List)

Sets Classes whose instances should be compared by calling their equals although being in recursive mode.

setEquals(boolean)

Sets the isEquals value.

setExcludeFields(String[])

Sets field names to be excluded by reflection tests.

setReflectUpToClass(Class)

Sets the superclass to reflect up to at reflective tests.

setTestRecursive(boolean)

Sets whether to test fields recursively, instead of using their equals method, when reflectively comparing objects.

setTestTransients(boolean)

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 boolean

  • rhs – The right-hand side boolean

Returns:

this instance.

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:

this instance.

public EqualsBuilder append(byte lhs, byte rhs)[source]

Test if two bytes are equal.

Parameters:
  • lhs – The left-hand side byte

  • rhs – The right-hand side byte

Returns:

this instance.

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:

this instance.

public EqualsBuilder append(char lhs, char rhs)[source]

Test if two chars are equal.

Parameters:
  • lhs – The left-hand side char

  • rhs – The right-hand side char

Returns:

this instance.

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:

this instance.

public EqualsBuilder append(double lhs, double rhs)[source]

Test if two doubles 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 double

  • rhs – The right-hand side double

Returns:

this instance.

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:

this instance.

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 float

  • rhs – The right-hand side float

Returns:

this instance.

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:

this instance.

public EqualsBuilder append(int lhs, int rhs)[source]

Test if two ints are equal.

Parameters:
  • lhs – The left-hand side int

  • rhs – The right-hand side int

Returns:

this instance.

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:

this instance.

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 field testRecursive is set to false. Otherwise, using their equals method.

Parameters:
  • lhs – The left-hand side object

  • rhs – The right-hand side object

Returns:

this instance.

public EqualsBuilder append(Object[] lhs, Object[] rhs)[source]

Performs a deep comparison of two Object arrays.

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:

this instance.

public EqualsBuilder append(long lhs, long rhs)[source]

Test if two longs are equal.

Parameters:
  • lhs – the left-hand side long

  • rhs – the right-hand side long

Returns:

this instance.

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:

this instance.

public EqualsBuilder append(short lhs, short rhs)[source]

Test if two shorts are equal.

Parameters:
  • lhs – The left-hand side short

  • rhs – The right-hand side short

Returns:

this instance.

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:

this instance.

public EqualsBuilder appendSuper(boolean superEquals)[source]

Adds the result of super.equals() to this builder.

Parameters:

superEquals – The result of calling super.equals()

Returns:

this instance.

Since:

2.0

@Override public Boolean build()[source]

Returns true if the fields that have been checked are all equal.

Returns:

true if all of the fields that have been checked are equal, false otherwise.

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 objects by using reflection.

It uses AccessibleObject.setAccessible to 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 using equals().

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 the Object.

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 excludeFields will 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:

this instance.

public static boolean reflectionEquals(Object lhs, Object rhs, boolean testTransients)[source]

Uses reflection to determine if the two Objects are equal.

It uses AccessibleObject.setAccessible to 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 using equals().

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 the Object.

Static fields will not be tested. Superclass fields will be included.

Parameters:
  • lhs – this object

  • rhs – The other object

  • testTransients – whether to include transient fields

Returns:

true if the two Objects have tested equals.

See also:

EqualsExclude

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.setAccessible to 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 using equals().

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 the Object.

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 by EqualsBuilder recursively instead of invoking their equals() 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 – this object

  • rhs – The other object

  • testTransients – whether to include transient fields

  • reflectUpToClass – The superclass to reflect up to (inclusive), may be null

  • testRecursive – whether to call reflection equals on non-primitive fields recursively.

  • excludeFields – array of field names to exclude from testing

Returns:

true if the two Objects have tested equals.

See also:

EqualsExclude

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.setAccessible to 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 using equals().

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 the Object.

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 – this object

  • rhs – The other object

  • testTransients – whether to include transient fields

  • reflectUpToClass – The superclass to reflect up to (inclusive), may be null

  • excludeFields – array of field names to exclude from testing

Returns:

true if the two Objects have tested equals.

See also:

EqualsExclude

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.setAccessible to 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 using equals().

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 – this object

  • rhs – The other object

  • excludeFields – array of field names to exclude from testing

Returns:

true if the two Objects have tested equals.

See also:

EqualsExclude

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.setAccessible to 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 using equals().

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 – this object

  • rhs – The other object

  • excludeFields – Collection of String field names to exclude from testing

Returns:

true if the two Objects have tested equals.

See also:

EqualsExclude

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 their equals although 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.
Prominent example being String class with its hash code cache field. Due to the importance of the String class, it is included in the default bypasses classes. Usually, if you use your own set of classes here, remember to include String class, too.
Parameters:

bypassReflectionClasses – classes to bypass reflection test

Returns:

this instance.

See also:

#setTestRecursive(boolean)

Since:

3.8

protected void setEquals(boolean isEquals)[source]

Sets the isEquals value.

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:

this instance.

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:

this instance.

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:

this instance.

See also:

#setBypassReflectionClasses(List)

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:

this instance.

Since:

3.6