cdugga
cdugga

Reputation: 3859

Java Enum and managing state

I want to use and Enumeration type to represent feature flags in my application. The enumeration type has a state and a description. I want to be able do something like the following in my code;

FeatureFlag.FANCYFEATURE.isActive()

The isActive() method would then call out to a service class connecting to a database to fetch the features state.

However in my spring application its not possible inject a bean into an Enum as the Enum type is static.

Can someone recommend a clean way to so this?

Upvotes: 0

Views: 270

Answers (2)

OldCurmudgeon
OldCurmudgeon

Reputation: 65851

By all means use an enum for a list of features but please don't use the enum for mutable state.

You would be better off using a EnumMap<Enum,State> structure to store mutable state against an enum.

Upvotes: 3

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280102

The following statement should tell you something is wrong

The isActive() method would then call out to a service class connecting to a database to fetch the features state

If you are fetching the state, we can assume it might be different at different times, ie. not a constant. Don't use enum for this.

And if you did, to answer

Can someone recommend a clean way to so this?

There is no way to inject beans into it, like you said.

Upvotes: 4

Related Questions