Skip to content

Instantly share code, notes, and snippets.

@alexradzin
Last active January 30, 2019 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexradzin/22715f214bf4450072446fdfbaeec788 to your computer and use it in GitHub Desktop.
Save alexradzin/22715f214bf4450072446fdfbaeec788 to your computer and use it in GitHub Desktop.
enum Action {ONE, TWO, THREE}
// ..........
Action a = ...
switch (a) {
case ONE: one(); break;
case TWO: two(); break;
case THREE: three(); break;
default: throw new UnsupportedOperationException(String.format("Operation %s is not supported", a));
}
enum Action {ONE, TWO, THREE}
enum MyAction {
FIRST { @Override public void action() { /* one */} },
SECOND { @Override public void action() { /* two */} },
THIRD { @Override public void action() { /* three */} },
public abstract void action();
}
Map<Action, MyAction> actions = new EnumMap<>(Action.class);
actions.put(Action.ONE, MyAction.FIRST);
actions.put(Action.TWO, MyAction.SECOND);
actions.put(Action.THREE, MyAction.THIRD);
//Usage
Optional.ofNullable(actions.get(a))
.orElseThrow(() -> throw new UnsupportedOperationException(String.format("Operation %s is not supported", a)))
.action();
enum Action {
ONE { @Override public void action() { /* one */} },
TWO { @Override public void action() { /* two */} },
THREE { @Override public void action() { /* three */} },
public abstract void action();
}
Action a = ...
a.action();
private static int ONE = 1;
private static int TWO = 2;
private static int THREE = 3;
// ..........
switch (c) {
case ONE: one(); break;
case TWO: two(); break;
case THREE: three(); break;
default: throw new UnsupportedOperationException(String.format("Operation %d is not supported", c));
}
switch (c) {
case 1: one(); break;
case 2: two(); break;
case 3: three(); break;
default: throw new UnsupportedOperationException(String.format("Operation %d is not supported", c));
}
enum Action {ONE, TWO, THREE}
enum MyAction {
ONE { @Override public void action() { /* one */} },
TWO { @Override public void action() { /* two */} },
THREE { @Override public void action() { /* three */} },
public abstract void action();
}
Action a = ...
MyAction.valueOf(a.name()).action();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment