Created
February 18, 2021 01:51
-
-
Save jami-i/9fe96bbbb1b42e99d39d06b4485b789a 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
class Scratch { | |
public static void main(String[] args) { | |
Bar defined = Bar.of("1"); | |
Bar unknown = Bar.of("unknown"); | |
assert defined.getValue().equals("1"); | |
assert unknown.getValue().equals("unknown"); | |
} | |
} | |
interface Bar { | |
String getValue(); | |
static Bar of(String value) { | |
try { | |
return DefinedBar.valueOf(value); | |
} catch (Exception e) { | |
return new UnknownBar(value); | |
} | |
} | |
} | |
enum DefinedBar implements Bar { | |
OPTION1("1"), OPTION2("2"), OPTION3("3"); | |
private final String value; | |
DefinedBar(String value) { | |
this.value = value; | |
} | |
public String getValue() { | |
return value; | |
} | |
} | |
final class UnknownBar implements Bar { | |
private final String value; | |
UnknownBar(String value) { | |
this.value = value; | |
} | |
public String getValue() { | |
return value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment