At Spring's @Cacheable
annotation I want to specify an unless
condition.
However my return value is neither a primitive type nor a Java bean, but an Enum.
How can I compare for equality with another Enum in SpEL (Spring Expression Language)?
We can compare enum variables using the following ways. Using Enum. compareTo() method. compareTo() method compares this enum with the specified object for order.
Because there is only one instance of each enum constant, it is permissible to use the == operator in place of the equals method when comparing two object references if it is known that at least one of them refers to an enum constant.
There are two ways for making comparison of enum members : equals method uses == operator internally to check if two enum are equal. This means, You can compare Enum using both == and equals method.
lang. Enum (see below code) uses == operator to check if two enum are equal. This means, You can compare Enum using both == and equals method. Also equals() method is declared final inside java.
#result == T(fully.qualified.path.to.AnEnum).A_VALUE
Reference here:
You can use the special T operator to specify an instance of java.lang.Class (the type).
#object.someEnumProperty.name() == 'CERTAIN_VALUE'
would work as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With