diff --git a/source/net/filebot/format/ExpressionFormatFunctions.java b/source/net/filebot/format/ExpressionFormatFunctions.java index 85eb192f..e99fba84 100644 --- a/source/net/filebot/format/ExpressionFormatFunctions.java +++ b/source/net/filebot/format/ExpressionFormatFunctions.java @@ -1,13 +1,16 @@ package net.filebot.format; +import static java.util.stream.Collectors.*; + import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; -import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Objects; +import java.util.stream.Stream; import groovy.lang.Closure; @@ -19,73 +22,31 @@ public class ExpressionFormatFunctions { /** * General helpers and utilities */ - public static Object c(Closure c) { - try { - return c.call(); - } catch (Exception e) { - return null; + public static Object call(Object object) { + if (object instanceof Closure) { + try { + return ((Closure) object).call(); + } catch (Exception e) { + return null; + } } + return object; } - public static Object any(Object a0, Object a1, Object... args) { - for (Object it : new Object[] { a0, a1 }) { - try { - Object result = callIfCallable(it); - if (result != null) { - return result; - } - } catch (Exception e) { - // ignore - } - } - - for (Object it : args) { - try { - Object result = callIfCallable(it); - if (result != null) { - return result; - } - } catch (Exception e) { - // ignore - } - } - - return null; + public static Object any(Object c1, Object c2, Object... cN) { + return stream(c1, c2, cN).findFirst().get(); } - public static List allOf(Object a0, Object a1, Object... args) { - List values = new ArrayList(); - - for (Object it : new Object[] { a0, a1 }) { - try { - Object result = callIfCallable(it); - if (result != null) { - values.add(result); - } - } catch (Exception e) { - // ignore - } - } - - for (Object it : args) { - try { - Object result = callIfCallable(it); - if (result != null) { - values.add(result); - } - } catch (Exception e) { - // ignore - } - } - - return values; + public static List allOf(Object c1, Object c2, Object... cN) { + return stream(c1, c2, cN).collect(toList()); } - private static Object callIfCallable(Object obj) throws Exception { - if (obj instanceof Closure) { - return ((Closure) obj).call(); - } - return obj; + public static String concat(Object c1, Object c2, Object... cN) { + return stream(c1, c2, cN).map(Objects::toString).collect(joining()); + } + + protected static Stream stream(Object c1, Object c2, Object... cN) { + return Stream.concat(Stream.of(c1, c2), Stream.of(cN)).map(ExpressionFormatFunctions::call).filter(Objects::nonNull); } public static Map csv(String path) throws IOException { diff --git a/source/net/filebot/format/ExpressionFormatMethods.java b/source/net/filebot/format/ExpressionFormatMethods.java index 776b7a3e..c724bf50 100644 --- a/source/net/filebot/format/ExpressionFormatMethods.java +++ b/source/net/filebot/format/ExpressionFormatMethods.java @@ -1,6 +1,7 @@ package net.filebot.format; import static java.util.regex.Pattern.*; +import static net.filebot.format.ExpressionFormatFunctions.*; import java.io.File; import java.io.IOException; @@ -22,9 +23,11 @@ import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation; import com.ibm.icu.text.Transliterator; import groovy.lang.Closure; +import net.filebot.Language; import net.filebot.similarity.Normalization; import net.filebot.util.FileUtilities; import net.filebot.util.StringUtilities; +import net.filebot.web.SimpleDate; public class ExpressionFormatMethods { @@ -422,12 +425,20 @@ public class ExpressionFormatMethods { return Locale.forLanguageTag(self); } - public static String plus(String self, Closure closure) { - try { - return self + closure.call(); - } catch (Exception e) { - return self; - } + public static String plus(String self, Closure other) { + return concat(self, other); + } + + public static String plus(Closure self, Object other) { + return concat(self, other); + } + + public static String plus(Language self, Object other) { + return concat(self, other); + } + + public static String plus(SimpleDate self, Object other) { + return concat(self, other); } } diff --git a/website/naming.html b/website/naming.html index cdb86dac..1226e5b8 100644 --- a/website/naming.html +++ b/website/naming.html @@ -190,7 +190,7 @@ plex - Plex Naming Standard + Plex Naming Standard <path>