Created
July 26, 2018 15:58
-
-
Save aploskov/61e5a147328990da2d3f988e9bf3936e to your computer and use it in GitHub Desktop.
Some triks for Java enums.
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
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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
OpenJDK 8: