* 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,59 +11,61 @@ import javax.script.Bindings;
import net.sourceforge.tuned.ExceptionUtilities;
public class ExpressionBindings extends AbstractMap<String, Object> implements Bindings {
protected final Object bindingBean;
protected final Map<String, Method> bindings = new TreeMap<String, Method>(String.CASE_INSENSITIVE_ORDER);
protected final Method undefined;
public ExpressionBindings(Object bindingBean) {
this.bindingBean = bindingBean;
// get method bindings
for (Method method : bindingBean.getClass().getMethods()) {
Define define = method.getAnnotation(Define.class);
if (define != null) {
for (String name : define.value()) {
Method existingBinding = bindings.put(name, method);
if (existingBinding != null)
throw new IllegalArgumentException(String.format("Illegal binding {%s} on %s", name, method.getName()));
}
}
}
// extract mapping that handles undefined bindings
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;
}
// invoke fallback method
return undefined.invoke(bindingBean);
}
@Override
public Object get(Object key) {
Method method = bindings.get(key);
if (method != null) {
try {
return evaluate(method);
@ -73,61 +73,53 @@ public class ExpressionBindings extends AbstractMap<String, Object> implements B
throw new BindingException(key.toString(), ExceptionUtilities.getRootCauseMessage(e), e);
}
}
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>>();
for (final String key : keySet()) {
entrySet.add(new Entry<String, Object>() {
@Override
public String getKey() {
return key;
}
@Override
public Object getValue() {
return get(key);
}
@Override
public Object setValue(Object value) {
@ -135,8 +127,8 @@ public class ExpressionBindings extends AbstractMap<String, Object> implements B
}
});
}
return entrySet;
}
}

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 = [:]