mirror of
https://github.com/mitb-archive/filebot
synced 2024-11-02 08:25:02 -04:00
Refactor
This commit is contained in:
parent
535a16621c
commit
7d5c9545b8
@ -520,7 +520,7 @@ public class ReleaseInfo {
|
||||
}
|
||||
|
||||
private String or(Object[] terms) {
|
||||
return joinSorted(terms, "|", reverseOrder(), "(", ")"); // non-capturing group that matches the longest occurrence
|
||||
return join(stream(terms).sorted(reverseOrder()), "|", "(", ")"); // non-capturing group that matches the longest occurrence
|
||||
}
|
||||
|
||||
private String[] quoteAll(Collection<String> values) {
|
||||
|
@ -73,7 +73,7 @@ public class JsonUtilities {
|
||||
}
|
||||
|
||||
public static String getString(Object node, String key) {
|
||||
return nonEmptyOrNull(asMap(node).get(key));
|
||||
return StringUtilities.asNonEmptyString(asMap(node).get(key));
|
||||
}
|
||||
|
||||
public static Integer getInteger(Object node, String key) {
|
||||
@ -97,7 +97,7 @@ public class JsonUtilities {
|
||||
}
|
||||
|
||||
public static <K extends Enum<K>> EnumMap<K, String> mapStringValues(Object node, Class<K> cls) {
|
||||
return mapValues(node, cls, JsonUtilities::nonEmptyOrNull);
|
||||
return mapValues(node, cls, StringUtilities::asNonEmptyString);
|
||||
}
|
||||
|
||||
public static <K extends Enum<K>, V> EnumMap<K, V> mapValues(Object node, Class<K> cls, Function<Object, V> converter) {
|
||||
@ -115,14 +115,4 @@ public class JsonUtilities {
|
||||
return map;
|
||||
}
|
||||
|
||||
public static String nonEmptyOrNull(Object object) {
|
||||
if (object != null) {
|
||||
String string = object.toString();
|
||||
if (string.length() > 0) {
|
||||
return string;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -2,12 +2,15 @@ package net.filebot.util;
|
||||
|
||||
import static java.util.Arrays.*;
|
||||
import static java.util.Collections.*;
|
||||
import static java.util.stream.Collectors.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public final class StringUtilities {
|
||||
|
||||
@ -53,39 +56,38 @@ public final class StringUtilities {
|
||||
return object == null ? null : object.toString();
|
||||
}
|
||||
|
||||
public static String asNonEmptyString(Object object) {
|
||||
if (object != null) {
|
||||
String string = object.toString();
|
||||
if (string.length() > 0) {
|
||||
return string;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static boolean isEmpty(Object object) {
|
||||
return object == null || object.toString().length() == 0;
|
||||
}
|
||||
|
||||
public static String join(Iterable<?> values, CharSequence delimiter) {
|
||||
return join(values, delimiter, "", "");
|
||||
public static boolean nonEmpty(Object object) {
|
||||
return object != null && object.toString().length() > 0;
|
||||
}
|
||||
|
||||
public static String join(Collection<?> values, CharSequence delimiter) {
|
||||
return join(values.stream(), delimiter, "", "");
|
||||
}
|
||||
|
||||
public static String join(CharSequence delimiter, Object... values) {
|
||||
return join(asList(values), delimiter, "", "");
|
||||
return join(stream(values), delimiter, "", "");
|
||||
}
|
||||
|
||||
public static String join(Object[] values, CharSequence delimiter) {
|
||||
return join(asList(values), delimiter, "", "");
|
||||
return join(stream(values), delimiter, "", "");
|
||||
}
|
||||
|
||||
public static String joinSorted(Object[] values, CharSequence delimiter, Comparator<Object> sort, CharSequence start, CharSequence end) {
|
||||
return join(stream(values).sorted(sort)::iterator, delimiter, start, end);
|
||||
}
|
||||
|
||||
public static String join(Iterable<?> values, CharSequence delimiter, CharSequence start, CharSequence end) {
|
||||
StringBuilder sb = new StringBuilder().append(start);
|
||||
|
||||
for (Object value : values) {
|
||||
if (!isEmpty(value)) {
|
||||
if (sb.length() > start.length()) {
|
||||
sb.append(delimiter);
|
||||
}
|
||||
sb.append(value);
|
||||
}
|
||||
}
|
||||
|
||||
return sb.append(end).toString();
|
||||
public static String join(Stream<?> values, CharSequence delimiter, CharSequence prefix, CharSequence suffix) {
|
||||
return values.map(StringUtilities::asNonEmptyString).filter(Objects::nonNull).collect(joining(delimiter, prefix, suffix));
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user