mirror of
https://github.com/mitb-archive/filebot
synced 2025-01-10 21:38:04 -05:00
treat empty list as null
This commit is contained in:
parent
3511f08344
commit
b496882c05
@ -4,7 +4,6 @@ import static net.filebot.util.ExceptionUtilities.*;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.AbstractMap;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
@ -43,21 +42,7 @@ public class ExpressionBindings extends AbstractMap<String, Object> implements B
|
||||
}
|
||||
|
||||
protected boolean isUndefined(Object value) {
|
||||
if (value == null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// treat empty string as null
|
||||
if (value instanceof CharSequence && value.toString().isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// treat empty list as null
|
||||
if (value instanceof Collection && ((Collection) value).isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
return value == null || ExpressionFormatFunctions.isEmptyValue(value);
|
||||
}
|
||||
|
||||
public Object getBindingBean() {
|
||||
|
@ -28,31 +28,40 @@ import net.filebot.util.FileUtilities;
|
||||
*/
|
||||
public class ExpressionFormatFunctions {
|
||||
|
||||
/**
|
||||
/*
|
||||
* General helpers and utilities
|
||||
*/
|
||||
|
||||
public static Object call(Object object) {
|
||||
if (object instanceof Closure<?>) {
|
||||
if (object instanceof Closure) {
|
||||
try {
|
||||
return ((Closure<?>) object).call();
|
||||
return call(((Closure) object).call());
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// treat empty string as null
|
||||
if (object instanceof CharSequence && object.toString().isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// treat empty list as null
|
||||
if (object instanceof Collection && ((Collection) object).isEmpty()) {
|
||||
if (isEmptyValue(object)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return object;
|
||||
}
|
||||
|
||||
public static boolean isEmptyValue(Object object) {
|
||||
// treat empty string as null
|
||||
if (object instanceof CharSequence && object.toString().isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// treat empty list as null
|
||||
if (object instanceof Collection && ((Collection) object).isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static Object any(Object c1, Object c2, Object... cN) {
|
||||
return stream(c1, c2, cN).findFirst().orElse(null);
|
||||
}
|
||||
@ -69,18 +78,26 @@ public class ExpressionFormatFunctions {
|
||||
return Stream.concat(Stream.of(c1, c2), Stream.of(cN)).map(ExpressionFormatFunctions::call).filter(Objects::nonNull);
|
||||
}
|
||||
|
||||
/*
|
||||
* Unix Shell / Windows PowerShell utilities
|
||||
*/
|
||||
|
||||
public static String quote(Object c1, Object... cN) {
|
||||
return Platform.isWindows() ? quote_ps(c1, cN) : quote_sh(c1, cN);
|
||||
return Platform.isWindows() ? quotePowerShell(c1, cN) : quoteBash(c1, cN);
|
||||
}
|
||||
|
||||
public static String quote_sh(Object c1, Object... cN) {
|
||||
public static String quoteBash(Object c1, Object... cN) {
|
||||
return stream(c1, null, cN).map(Objects::toString).map(s -> "'" + s.replace("'", "'\"'\"'") + "'").collect(joining(" "));
|
||||
}
|
||||
|
||||
public static String quote_ps(Object c1, Object... cN) {
|
||||
public static String quotePowerShell(Object c1, Object... cN) {
|
||||
return stream(c1, null, cN).map(Objects::toString).map(s -> "@'\n" + s + "\n'@").collect(joining(" "));
|
||||
}
|
||||
|
||||
/*
|
||||
* I/O utilities
|
||||
*/
|
||||
|
||||
public static Map<String, String> csv(Object path) throws IOException {
|
||||
Pattern[] delimiter = { TAB, SEMICOLON };
|
||||
Map<String, String> map = new LinkedHashMap<String, String>();
|
||||
|
Loading…
Reference in New Issue
Block a user