Skip to content

Instantly share code, notes, and snippets.

@benjishults
Created December 31, 2018 18:02
Show Gist options
  • Save benjishults/2b6969fb38583c01b85f465a3841f7df to your computer and use it in GitHub Desktop.
Save benjishults/2b6969fb38583c01b85f465a3841f7df to your computer and use it in GitHub Desktop.
DocumentType first pass in enum refactoring demo
public enum DocumentType {
TXT("txt", "text"),
MD("md", "markdown"),
HTML("html", "htm");
String[] extensions;
DocumentType(String... extensions) {
this.extensions = Arrays.copyOf(extensions, extensions.length);
}
public static DocumentType forPath(Path file) {
String ext = FileNameHelper.getLowercaseExtension(file);
for (DocumentType type : DocumentType.values()) {
if (Arrays.stream(type.extensions).anyMatch(ext::equals)) {
return type;
}
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment