Refactor Enum.forName error messages

This commit is contained in:
Reinhard Pointner 2017-02-17 22:02:20 +08:00
parent 21d562eadd
commit 4897adf913
6 changed files with 53 additions and 22 deletions

View File

@ -1,5 +1,7 @@
package net.filebot; package net.filebot;
import static java.util.Arrays.*;
import static java.util.stream.Collectors.*;
import static net.filebot.UserFiles.*; import static net.filebot.UserFiles.*;
import java.io.File; import java.io.File;
@ -7,6 +9,7 @@ import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.LinkOption; import java.nio.file.LinkOption;
import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.BasicFileAttributes;
import java.util.List;
import net.filebot.util.FileUtilities; import net.filebot.util.FileUtilities;
@ -163,12 +166,18 @@ public enum StandardRenameAction implements RenameAction {
} }
} }
public static StandardRenameAction forName(String action) { public static List<String> names() {
for (StandardRenameAction it : values()) { return stream(values()).map(Enum::name).collect(toList());
if (it.name().equalsIgnoreCase(action))
return it;
} }
throw new IllegalArgumentException("Illegal rename action: " + action);
public static StandardRenameAction forName(String name) {
for (StandardRenameAction action : values()) {
if (action.name().equalsIgnoreCase(name)) {
return action;
}
}
throw new IllegalArgumentException(String.format("%s not in %s", name, names()));
} }
public static File revert(File current, File original) throws IOException { public static File revert(File current, File original) throws IOException {

View File

@ -1,16 +1,26 @@
package net.filebot.cli; package net.filebot.cli;
import static java.util.Arrays.*;
import static java.util.stream.Collectors.*;
import java.util.List;
public enum ConflictAction { public enum ConflictAction {
SKIP, OVERRIDE, FAIL, AUTO, INDEX; SKIP, OVERRIDE, FAIL, AUTO, INDEX;
public static ConflictAction forName(String action) { public static List<String> names() {
for (ConflictAction it : values()) { return stream(values()).map(Enum::name).collect(toList());
if (it.name().equalsIgnoreCase(action))
return it;
} }
throw new IllegalArgumentException("Illegal conflict action: " + action); public static ConflictAction forName(String name) {
for (ConflictAction action : values()) {
if (action.name().equalsIgnoreCase(name)) {
return action;
}
}
throw new IllegalArgumentException(String.format("%s not in %s", name, names()));
} }
} }

View File

@ -1,8 +1,11 @@
package net.filebot.subtitle; package net.filebot.subtitle;
import static java.util.Arrays.*;
import static java.util.stream.Collectors.*;
import static net.filebot.util.FileUtilities.*; import static net.filebot.util.FileUtilities.*;
import java.io.File; import java.io.File;
import java.util.List;
import net.filebot.web.SubtitleDescriptor; import net.filebot.web.SubtitleDescriptor;
@ -49,13 +52,18 @@ public enum SubtitleNaming {
public abstract String format(File video, SubtitleDescriptor subtitle, String ext); public abstract String format(File video, SubtitleDescriptor subtitle, String ext);
public static SubtitleNaming forName(String s) { public static List<String> names() {
for (SubtitleNaming it : values()) { return stream(values()).map(Enum::name).collect(toList());
if (it.name().equalsIgnoreCase(s) || it.toString().equalsIgnoreCase(s)) { }
return it;
public static SubtitleNaming forName(String name) {
for (SubtitleNaming naming : values()) {
if (naming.name().equalsIgnoreCase(name)) {
return naming;
} }
} }
return null;
throw new IllegalArgumentException(String.format("%s not in %s", name, names()));
} }
} }

View File

@ -1,8 +1,11 @@
package net.filebot.vfs; package net.filebot.vfs;
import static java.util.Collections.*;
import static net.filebot.Logging.*;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.logging.Level;
public enum ArchiveType { public enum ArchiveType {
@ -25,12 +28,12 @@ public enum ArchiveType {
return files; return files;
} }
} catch (Exception e) { } catch (Exception e) {
// ignore debug.log(Level.WARNING, e, e::toString);
} }
} }
// cannot extract data, return empty archive // cannot extract data, return empty archive
return Collections.emptySet(); return emptySet();
} }
}, },
@ -39,10 +42,12 @@ public enum ArchiveType {
@Override @Override
public Iterable<MemoryFile> fromData(ByteBuffer data) { public Iterable<MemoryFile> fromData(ByteBuffer data) {
// cannot extract data, return empty archive // cannot extract data, return empty archive
return Collections.emptySet(); return emptySet();
} }
}; };
public abstract Iterable<MemoryFile> fromData(ByteBuffer data);
public static ArchiveType forName(String name) { public static ArchiveType forName(String name) {
if (name == null) if (name == null)
return UNDEFINED; return UNDEFINED;
@ -53,6 +58,4 @@ public enum ArchiveType {
return UNKOWN; return UNKOWN;
} }
public abstract Iterable<MemoryFile> fromData(ByteBuffer data);
} }

View File

@ -24,7 +24,7 @@ public enum SortOrder {
} }
public static List<String> names() { public static List<String> names() {
return stream(values()).map(SortOrder::name).collect(toList()); return stream(values()).map(Enum::name).collect(toList());
} }
public static SortOrder forName(String name) { public static SortOrder forName(String name) {

View File

@ -6,6 +6,7 @@ import java.util.Locale;
public class SubtitleSearchResult extends Movie { public class SubtitleSearchResult extends Movie {
public enum Kind { public enum Kind {
Movie, Series, Other, Unkown; Movie, Series, Other, Unkown;
public static Kind forName(String s) { public static Kind forName(String s) {