Enum Warning
- java.lang.Object
-
- java.lang.Enum<Warning>
-
- nl.jqno.equalsverifier.Warning
-
- All Implemented Interfaces:
Serializable
,Comparable<Warning>
public enum Warning extends Enum<Warning>
Enum of warnings that can be suppressed inEqualsVerifier
.- See Also:
EqualsVerifierApi.suppress(Warning...)
-
-
Enum Constant Summary
Enum Constants Enum Constant Description ALL_FIELDS_SHOULD_BE_USED
Signals that not all fields are relevant in theequals
contract.ALL_NONFINAL_FIELDS_SHOULD_BE_USED
Signals that non-final fields are not relevant in theequals
contract.BIGDECIMAL_EQUALITY
Disables the check that equality ofBigDecimal
fields is implemented usingcompareTo
rather thanequals
.IDENTICAL_COPY
Disables the check, when theequals
method is overridden in the class under test, that an instance of this class should be equal to an identical copy of itself.IDENTICAL_COPY_FOR_VERSIONED_ENTITY
Disables the check, when theequals
method is overridden in the class under test, that an instance of this class should be equal to an identical copy of itself.INHERITED_DIRECTLY_FROM_OBJECT
Disables the check that verifiesequals
is actually overridden.NO_EXAMPLE_FOR_CACHED_HASHCODE
Disables the example check for cachedhashCode
.NONFINAL_FIELDS
Disables checks for non-final fields on whichequals
andhashCode
depend.NULL_FIELDS
REFERENCE_EQUALITY
Disables the check for reference equality on fields.STRICT_HASHCODE
Disables the check that all fields used inequals
must also be used inhashCode
.STRICT_INHERITANCE
Disables some of the stricter inheritance tests.SURROGATE_KEY
Disables the check that fields marked with the @Id or @EmbeddedId annotations in JPA entities may not be used in theequals
contract.TRANSIENT_FIELDS
Disables the check that transient fields not be part of theequals
contract.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Warning
valueOf(String name)
Returns the enum constant of this type with the specified name.static Warning[]
values()
Returns an array containing the constants of this enum type, in the order they are declared.
-
-
-
Enum Constant Detail
-
ALL_FIELDS_SHOULD_BE_USED
public static final Warning ALL_FIELDS_SHOULD_BE_USED
Signals that not all fields are relevant in theequals
contract.EqualsVerifier
will not fail if one or more fields do not affect the outcome ofequals
.Only applies to non-transient fields.
-
ALL_NONFINAL_FIELDS_SHOULD_BE_USED
public static final Warning ALL_NONFINAL_FIELDS_SHOULD_BE_USED
Signals that non-final fields are not relevant in theequals
contract.EqualsVerifier
will not fail if one or more non-final fields do not affect the outcome ofequals
.Only applies to non-transient fields.
-
REFERENCE_EQUALITY
public static final Warning REFERENCE_EQUALITY
Disables the check for reference equality on fields.EqualsVerifier will check if the
equals
method calls equals on the object fields of the class under test, instead of the==
operator, since normally this signifies a mistake (e.g. comparing string fields with==
).However, sometimes
==
is used intentionally, or the field in question doesn't implementequals
itself, so a call to theequals
method of that field is essentially a reference equality check instead of a value equality check. In these cases, this warning can be suppressed.
-
IDENTICAL_COPY
public static final Warning IDENTICAL_COPY
Disables the check, when theequals
method is overridden in the class under test, that an instance of this class should be equal to an identical copy of itself.Normally, it is important that an object be equal to an identical copy of itself: after all, this is the point of overriding
equals
in the first place.However, when the class is part of a hierarchy, but should be (pseudo-)singleton, it can be useful to suppress this warning. This can happen in certain implementations of the Null Object Pattern, for example.
If this warning is suppressed, and it turns out that an instance of the class under test is equal to an identical copy of itself after all,
EqualsVerifier
will fail.
-
IDENTICAL_COPY_FOR_VERSIONED_ENTITY
public static final Warning IDENTICAL_COPY_FOR_VERSIONED_ENTITY
Disables the check, when theequals
method is overridden in the class under test, that an instance of this class should be equal to an identical copy of itself.Normally, it is important that an object be equal to an identical copy of itself: after all, this is the point of overriding
equals
in the first place.However, when the class is a kind of versioned entity and there is an
id
field that is zero when the object is new, it is often the case that two new objects are never equal to each other. In these cases, it can be useful to suppress this warning.You cannot use
IDENTICAL_COPY
in these cases, because when theid
s are equal, the objects should be, too, andEqualsVerifier
would fail in this case.If this warning is suppressed, and it turns out that an instance of the class under test is equal to an identical copy of itself after all,
EqualsVerifier
will NOT fail.
-
INHERITED_DIRECTLY_FROM_OBJECT
public static final Warning INHERITED_DIRECTLY_FROM_OBJECT
Disables the check that verifiesequals
is actually overridden.Can be used when a whole package of classes is automatically scanned and presented to EqualsVerifier, and one or more of them don't need to override
equals
.
-
NO_EXAMPLE_FOR_CACHED_HASHCODE
public static final Warning NO_EXAMPLE_FOR_CACHED_HASHCODE
Disables the example check for cachedhashCode
.The example check verifies that the cached
hashCode
is properly initialized. You can use this, if creating an example object is too cumbersome. In this case, null can be passed as an example.Note that suppressing this warning can be dangerous and should only be done in unusual circumstances.
-
NONFINAL_FIELDS
public static final Warning NONFINAL_FIELDS
Disables checks for non-final fields on whichequals
andhashCode
depend.EqualsVerifier
's standard behaviour is to disallow non-final fields being used inequals
andhashCode
methods, since classes that depend on non-final fields in these methods cannot reliably be used in collections.However, sometimes an external library requires that fields be non-final. An example of this are Java Beans. In such a case, suppress this warning to prevent
EqualsVerifier
from checking for non-final fields.
-
NULL_FIELDS
public static final Warning NULL_FIELDS
Disables checks forNullPointerException
withinequals
,hashCode
andtoString
methods.Sometimes the constructor of a class makes sure no field can be null. If this is the case, and if the fields cannot be made null later in the lifecycle of the class by setters or other methods, suppress this warning to disable the check for
NullPointerException
.
-
STRICT_HASHCODE
public static final Warning STRICT_HASHCODE
Disables the check that all fields used inequals
must also be used inhashCode
.This is useful when bringing legacy systems under test, where you don't want to change the existing
hashCode
behaviour but you do want to use EqualsVerifier.Note that
hashCode
s with higher distributions give better performance when used in collections such asHashMap
. Therefore, if possible, you should use all fields that are used inequals
, inhashCode
as well.
-
STRICT_INHERITANCE
public static final Warning STRICT_INHERITANCE
Disables some of the stricter inheritance tests.EqualsVerifier
's standard behaviour, if T is not final and neither are itsequals
andhashCode
methods, is to require a reference to a subclass of T for which no instance can be equal to any instance of T, to make sure that subclasses that can redefineequals
orhashCode
don't break the contract; or it asks to call theusingGetClass
method if T usesgetClass()
instead ofinstanceof
in itsequals
method.Some may find that too strict for their liking; suppressing this warning disables that test.
-
SURROGATE_KEY
public static final Warning SURROGATE_KEY
Disables the check that fields marked with the @Id or @EmbeddedId annotations in JPA entities may not be used in theequals
contract.When this warning is suppressed, the fields marked with @Id or @EmbeddedId will become the entity's surrogate key. Only these fields can now be part of the
equals
contract; all other fields may no longer be used inequals
.
-
TRANSIENT_FIELDS
public static final Warning TRANSIENT_FIELDS
Disables the check that transient fields not be part of theequals
contract.EqualsVerifier
's standard behaviour is to disallow transient fields being used inequals
andhashCode
methods, since these fields may not be restored to their original state after deserialization, which would breakequals
.If measures are taken that this will never happen, this warning can be suppressed to disable
EqualsVerifier
's transience test.
-
BIGDECIMAL_EQUALITY
public static final Warning BIGDECIMAL_EQUALITY
Disables the check that equality ofBigDecimal
fields is implemented usingcompareTo
rather thanequals
.BigDecimal
objects that are equal usingcompareTo
are not necessarily equal usingequals
, for example the values of 1 and 1.0. For variants of the same value to be considered equal, classes withBigDecimal
fields should usecompareTo
to check equality of non-nullBigDecimal
references and produce the same hashcode for all equal variants.EqualsVerifier
checks for this by default but it can be disabled by suppressing this warning.
-
-
Method Detail
-
values
public static Warning[] values()
Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:for (Warning c : Warning.values()) System.out.println(c);
- Returns:
- an array containing the constants of this enum type, in the order they are declared
-
valueOf
public static Warning valueOf(String name)
Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)- Parameters:
name
- the name of the enum constant to be returned.- Returns:
- the enum constant with the specified name
- Throws:
IllegalArgumentException
- if this enum type has no constant with the specified nameNullPointerException
- if the argument is null
-
-