Skip to content

Instantly share code, notes, and snippets.

@fsarradin
Created August 5, 2014 04:31
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 fsarradin/0f4b16938e0c010ce40c to your computer and use it in GitHub Desktop.
Save fsarradin/0f4b16938e0c010ce40c to your computer and use it in GitHub Desktop.
IntelliJ IDEA live template for enum with by-name behavior
private static final Map<String, $CLASS_NAME$> $VAR_CLASS_NAME$sByName = create$CLASS_NAME$sByName();
private final String name;
$CLASS_NAME$() {
this(null);
}
$CLASS_NAME$(String name) {
this.name = name;
}
protected static Map<String, $CLASS_NAME$> create$CLASS_NAME$sByName() {
Map<String, $CLASS_NAME$> $VAR_CLASS_NAME$s = new HashMap<>();
for ($CLASS_NAME$ $VAR_CLASS_NAME$ : $CLASS_NAME$.values()) {
if ($VAR_CLASS_NAME$.name != null) {
$VAR_CLASS_NAME$s.put($VAR_CLASS_NAME$.name, $VAR_CLASS_NAME$);
}
}
return Collections.unmodifiableMap($VAR_CLASS_NAME$s);
}
public static $CLASS_NAME$ of(String name) {
if (!$VAR_CLASS_NAME$sByName.containsKey(name)) {
return $END$;
}
return $VAR_CLASS_NAME$sByName.get(name);
}
CLASS_NAME: className()
VAR_CLASS_NAME: decapitalize(CLASS_NAME) / item
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment