From b496882c05c43f68f90b66d0c4dbb3a04e5a7ef2 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Mon, 27 Mar 2017 21:24:01 +0800 Subject: [PATCH] treat empty list as null --- .../filebot/format/ExpressionBindings.java | 17 +------- .../format/ExpressionFormatFunctions.java | 43 +++++++++++++------ 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/source/net/filebot/format/ExpressionBindings.java b/source/net/filebot/format/ExpressionBindings.java index d38ca71c..645bb266 100644 --- a/source/net/filebot/format/ExpressionBindings.java +++ b/source/net/filebot/format/ExpressionBindings.java @@ -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 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() { diff --git a/source/net/filebot/format/ExpressionFormatFunctions.java b/source/net/filebot/format/ExpressionFormatFunctions.java index fbd67f75..c6d53090 100644 --- a/source/net/filebot/format/ExpressionFormatFunctions.java +++ b/source/net/filebot/format/ExpressionFormatFunctions.java @@ -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 csv(Object path) throws IOException { Pattern[] delimiter = { TAB, SEMICOLON }; Map map = new LinkedHashMap();