2014-04-15 08:23:58 -04:00
|
|
|
package net.sourceforge.filebot.format;
|
|
|
|
|
|
|
|
import groovy.lang.Closure;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Global functions available in the {@link ExpressionFormat}
|
|
|
|
*/
|
|
|
|
public class ExpressionFormatFunctions {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* General helpers and utilities
|
|
|
|
*/
|
|
|
|
public static Object c(Closure<?> c) {
|
|
|
|
try {
|
|
|
|
return c.call();
|
|
|
|
} catch (Exception e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-15 10:29:13 -04:00
|
|
|
public static Object any(Object a0, Object a1, Object... args) {
|
|
|
|
for (Object it : new Object[] { a0, a1 }) {
|
2014-04-15 08:23:58 -04:00
|
|
|
try {
|
2014-04-15 10:29:13 -04:00
|
|
|
Object result = callIfCallable(it);
|
2014-04-15 08:23:58 -04:00
|
|
|
if (result != null) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
}
|
2014-04-15 10:29:13 -04:00
|
|
|
|
|
|
|
for (Object it : args) {
|
|
|
|
try {
|
|
|
|
Object result = callIfCallable(it);
|
|
|
|
if (result != null) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-15 08:23:58 -04:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-04-15 10:29:13 -04:00
|
|
|
public static List<Object> allOf(Object a0, Object a1, Object... args) {
|
2014-04-15 08:23:58 -04:00
|
|
|
List<Object> values = new ArrayList<Object>();
|
|
|
|
|
2014-04-15 10:29:13 -04:00
|
|
|
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) {
|
2014-04-15 08:23:58 -04:00
|
|
|
try {
|
2014-04-15 10:29:13 -04:00
|
|
|
Object result = callIfCallable(it);
|
2014-04-15 08:23:58 -04:00
|
|
|
if (result != null) {
|
|
|
|
values.add(result);
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return values;
|
|
|
|
}
|
|
|
|
|
2014-04-15 10:29:13 -04:00
|
|
|
private static Object callIfCallable(Object obj) throws Exception {
|
|
|
|
if (obj instanceof Closure<?>) {
|
|
|
|
return ((Closure<?>) obj).call();
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2014-04-15 08:23:58 -04:00
|
|
|
}
|