Skip to content

Instantly share code, notes, and snippets.

@aploskov
Created July 26, 2018 15:58
Show Gist options
  • Save aploskov/61e5a147328990da2d3f988e9bf3936e to your computer and use it in GitHub Desktop.
Save aploskov/61e5a147328990da2d3f988e9bf3936e to your computer and use it in GitHub Desktop.
Some triks for Java enums.
public class Main {
public static void main(String[] args) {
for (final Mode mode : Mode.values()) {
System.out.printf("%-16s { new: %5b, edit: %5b, confirm: %5b, yet another: %5b }\n",
mode.name(),
mode.isNew(),
mode.isEdit(),
mode.isConfirm(),
mode.isYetAnother());
}
}
enum Mode {
NEW_ONE,
NEW_TWO,
NEW_THREE,
NEW_FOUR,
EDIT_ONE,
EDIT_TWO,
EDIT_THREE,
CONFIRM_ONE,
CONFIRM_TWO,
CONFIRM_THREE,
YET_ANOTHER_MODE;
public boolean isNew() {
return this.oneOf(NEW_ONE,
NEW_TWO,
NEW_THREE,
NEW_FOUR);
}
public boolean isEdit() {
return this.oneOf(EDIT_ONE,
EDIT_TWO,
EDIT_THREE);
}
public boolean isConfirm() {
return this.oneOf(CONFIRM_ONE,
CONFIRM_TWO,
CONFIRM_THREE);
}
public boolean isYetAnother() {
return this.equals(YET_ANOTHER_MODE);
}
private boolean oneOf(final Mode... modes) {
for (final Mode mode : modes) {
if (this.equals(mode)) {
return true;
}
}
return false;
}
}
}
@aploskov
Copy link
Author

OpenJDK 8:

NEW_ONE          { new:  true, edit: false, confirm: false, yet another: false }
NEW_TWO          { new:  true, edit: false, confirm: false, yet another: false }
NEW_THREE        { new:  true, edit: false, confirm: false, yet another: false }
NEW_FOUR         { new:  true, edit: false, confirm: false, yet another: false }
EDIT_ONE         { new: false, edit:  true, confirm: false, yet another: false }
EDIT_TWO         { new: false, edit:  true, confirm: false, yet another: false }
EDIT_THREE       { new: false, edit:  true, confirm: false, yet another: false }
CONFIRM_ONE      { new: false, edit: false, confirm:  true, yet another: false }
CONFIRM_TWO      { new: false, edit: false, confirm:  true, yet another: false }
CONFIRM_THREE    { new: false, edit: false, confirm:  true, yet another: false }
YET_ANOTHER_MODE { new: false, edit: false, confirm: false, yet another:  true }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment