This commit is contained in:
Reinhard Pointner 2016-03-09 05:58:44 +00:00
parent 535a16621c
commit 7d5c9545b8
3 changed files with 27 additions and 35 deletions

View File

@ -520,7 +520,7 @@ public class ReleaseInfo {
} }
private String or(Object[] terms) { 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) { private String[] quoteAll(Collection<String> values) {

View File

@ -73,7 +73,7 @@ public class JsonUtilities {
} }
public static String getString(Object node, String key) { 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) { 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) { 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) { 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; return map;
} }
public static String nonEmptyOrNull(Object object) {
if (object != null) {
String string = object.toString();
if (string.length() > 0) {
return string;
}
}
return null;
}
} }

View File

@ -2,12 +2,15 @@ package net.filebot.util;
import static java.util.Arrays.*; import static java.util.Arrays.*;
import static java.util.Collections.*; import static java.util.Collections.*;
import static java.util.stream.Collectors.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Stream;
public final class StringUtilities { public final class StringUtilities {
@ -53,39 +56,38 @@ public final class StringUtilities {
return object == null ? null : object.toString(); 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) { public static boolean isEmpty(Object object) {
return object == null || object.toString().length() == 0; return object == null || object.toString().length() == 0;
} }
public static String join(Iterable<?> values, CharSequence delimiter) { public static boolean nonEmpty(Object object) {
return join(values, delimiter, "", ""); 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) { 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) { 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) { public static String join(Stream<?> values, CharSequence delimiter, CharSequence prefix, CharSequence suffix) {
return join(stream(values).sorted(sort)::iterator, delimiter, start, end); return values.map(StringUtilities::asNonEmptyString).filter(Objects::nonNull).collect(joining(delimiter, prefix, suffix));
}
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();
} }
/** /**