Last active
January 30, 2019 20:42
-
-
Save alexradzin/22715f214bf4450072446fdfbaeec788 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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