When defining Predicate's or Function's, I used to create them as static final
private static final Predicate<SomeDto> SOME_PREDICATE =
new Predicate<SomeDto>() {
@Override
public boolean apply(SomeDto input) {
return input.isValid();
}
}
However I have noticed that there is a lot of use also with enum
version, for example
private enum SomePredicate implements Predicate<SomeDto> {
INSTANCE;
@Override
public boolean apply(SomeDto input) {
return input.isValid();
}
}
I am aware about the enum vs static final topics, but is there any real advantage of using enum's over static final with predicates or functions?
The main advantage of the enum
singleton approach in this case is that the Predicate
/Function
is automatically Serializable
.
Guava itself uses enum
s for some of its Predicate
and Function
implementations for this reason, among others. Josh Bloch recommends the use of enum
s when you want a singleton in Effective Java 2nd Ed., Item 3. To quote:
This approach is functionally equivalent to the public field approach, except that it is more concise, provides the serialization machinery for free, and provides an ironclad guarantee against multiple instantiation, even in the face of sophisticated serialization or reflection attacks. While this approach has yet to be widely adopted, a single-element enum type is the best way to implement a singleton.
Guava exposes those singletons through static methods in its API though, avoiding the ugliness of SomePredicate.INSTANCE
in user code.
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