-
Notifications
You must be signed in to change notification settings - Fork 0
Java enum with some auxiliary data
Peter Nagy edited this page Oct 28, 2023
·
6 revisions
Very often you need an enum that also contains some extra data like e.g. an integer or string. For example, we load strings from a config file, but we want to use them as enum in our program. In this case, we can use the following pattern.
@Getter
public enum Action
{
CONVERT("convert"),
UPLOAD("upload"),
ALL("all");
private final String value;
Action(String value)
{
this.value = value;
}
public static Optional<Action> fromValue(String value)
{
return Arrays.stream(Action.values()).filter(i -> i.value.equals(value)).findFirst();
}
}