* empty string binding values will be considered undefined

* convenience global functions any and allOf to we can easily evaluate many closures in one go
This commit is contained in:
Reinhard Pointner 2013-12-20 13:27:22 +00:00
parent dffcf5aa2b
commit a7b0920d33
2 changed files with 44 additions and 32 deletions

View File

@ -1,7 +1,5 @@
package net.sourceforge.filebot.format;
import java.lang.reflect.Method;
import java.util.AbstractMap;
import java.util.HashSet;
@ -13,7 +11,6 @@ import javax.script.Bindings;
import net.sourceforge.tuned.ExceptionUtilities;
public class ExpressionBindings extends AbstractMap<String, Object> implements Bindings {
protected final Object bindingBean;
@ -22,7 +19,6 @@ public class ExpressionBindings extends AbstractMap<String, Object> implements B
protected final Method undefined;
public ExpressionBindings(Object bindingBean) {
this.bindingBean = bindingBean;
@ -44,16 +40,21 @@ public class ExpressionBindings extends AbstractMap<String, Object> implements B
undefined = bindings.remove(Define.undefined);
}
protected boolean isUndefined(Object value) {
if (value instanceof CharSequence) {
return ((CharSequence) value).length() <= 0;
}
return value == null;
}
public Object getBindingBean() {
return bindingBean;
}
protected Object evaluate(final Method method) throws Exception {
Object value = method.invoke(bindingBean);
if (value != null) {
if (!isUndefined(value)) {
return value;
}
@ -61,7 +62,6 @@ public class ExpressionBindings extends AbstractMap<String, Object> implements B
return undefined.invoke(bindingBean);
}
@Override
public Object get(Object key) {
Method method = bindings.get(key);
@ -77,39 +77,33 @@ public class ExpressionBindings extends AbstractMap<String, Object> implements B
return null;
}
@Override
public Object put(String key, Object value) {
// bindings are immutable
return null;
}
@Override
public Object remove(Object key) {
// bindings are immutable
return null;
}
@Override
public boolean containsKey(Object key) {
return bindings.containsKey(key);
}
@Override
public Set<String> keySet() {
return bindings.keySet();
}
@Override
public boolean isEmpty() {
return bindings.isEmpty();
}
@Override
public Set<Entry<String, Object>> entrySet() {
Set<Entry<String, Object>> entrySet = new HashSet<Entry<String, Object>>();
@ -122,13 +116,11 @@ public class ExpressionBindings extends AbstractMap<String, Object> implements B
return key;
}
@Override
public Object getValue() {
return get(key);
}
@Override
public Object setValue(Object value) {
return put(key, value);

View File

@ -172,7 +172,7 @@ String.metaClass.ascii = { fallback = ' ' -> delegate.transliterate("Any-Latin;L
/**
* General helpers and utilities
*/
def c(c) {
def c(Closure c) {
try {
return c.call()
} catch (Throwable e) {
@ -180,6 +180,26 @@ def c(c) {
}
}
def any(Closure... closures) {
return closures.findResult{ c ->
try {
return c.call()
} catch (Throwable e) {
return null
}
}
}
def allOf(Closure... closures) {
return closures.toList().findResults{ c ->
try {
return c.call()
} catch (Throwable e) {
return null
}
}
}
def csv(path, delim = ';', keyIndex = 0, valueIndex = 1) {
def f = path as File
def values = [:]