Make non-String objects behave more like String objects in format expressions

This commit is contained in:
Reinhard Pointner 2016-03-28 10:13:55 +00:00
parent 36a02ff457
commit c9bbdf5e65
3 changed files with 40 additions and 68 deletions

View File

@ -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<Object> allOf(Object a0, Object a1, Object... args) {
List<Object> values = new ArrayList<Object>();
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<Object> 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<Object> 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<String, String> csv(String path) throws IOException {

View File

@ -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);
}
}

View File

@ -190,7 +190,7 @@
</tr>
<tr>
<td>plex</td>
<td><a href="https://support.plex.tv/hc/en-us/sections/200059498-Naming-and-Organizing-TV-Shows" target="_blank">Plex Naming Standard</a></td>
<td><a href="https://support.plex.tv/hc/en-us/categories/200028098-Media-Preparation" target="_blank">Plex Naming Standard</a></td>
<td>&lt;path&gt;</td>
</tr>
<tr>