Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare Enums in SpEL

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)?

like image 728
Harold L. Brown Avatar asked Feb 16 '14 01:02

Harold L. Brown


People also ask

Can we compare two enums?

We can compare enum variables using the following ways. Using Enum. compareTo() method. compareTo() method compares this enum with the specified object for order.

Can you use == for enums?

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.

How do you compare enum values?

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.

Can you compare enums with == java?

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.


2 Answers

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

like image 146
Harold L. Brown Avatar answered Oct 05 '22 05:10

Harold L. Brown


#object.someEnumProperty.name() == 'CERTAIN_VALUE'

would work as well.

like image 29
sorrymissjackson Avatar answered Oct 05 '22 04:10

sorrymissjackson