org.mvel2.integration.impl.CachedMapVariableResolverFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tbel Show documentation
Show all versions of tbel Show documentation
TBEL is a powerful expression language for ThingsBoard platform user-defined functions.
Original implementation is based on MVEL.
package org.mvel2.integration.impl;
import org.mvel2.UnresolveablePropertyException;
import org.mvel2.integration.VariableResolver;
import org.mvel2.integration.VariableResolverFactory;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class CachedMapVariableResolverFactory extends BaseVariableResolverFactory {
/**
* Holds the instance of the variables.
*/
protected Map variables;
public CachedMapVariableResolverFactory() {
}
public CachedMapVariableResolverFactory(Map variables) {
this.variables = variables;
variableResolvers = new HashMap(variables.size() * 2);
for (Map.Entry entry : variables.entrySet()) {
variableResolvers.put(entry.getKey(), new PrecachedMapVariableResolver(entry, entry.getKey()));
}
}
public CachedMapVariableResolverFactory(Map variables, VariableResolverFactory nextFactory) {
this.variables = variables;
variableResolvers = new HashMap(variables.size() * 2);
for (Map.Entry entry : variables.entrySet()) {
variableResolvers.put(entry.getKey(), new PrecachedMapVariableResolver(entry, entry.getKey()));
}
this.nextFactory = nextFactory;
}
public VariableResolver createVariable(String name, Object value) {
VariableResolver vr;
try {
(vr = getVariableResolver(name)).setValue(value);
return vr;
}
catch (UnresolveablePropertyException e) {
addResolver(name, vr = new MapVariableResolver(variables, name)).setValue(value);
return vr;
}
}
public VariableResolver createVariable(String name, Object value, Class> type) {
VariableResolver vr;
try {
vr = getVariableResolver(name);
}
catch (UnresolveablePropertyException e) {
vr = null;
}
if (vr != null && vr.getType() != null) {
throw new RuntimeException("variable already defined within scope: " + vr.getType() + " " + name);
}
else {
addResolver(name, vr = new MapVariableResolver(variables, name, type)).setValue(value);
return vr;
}
}
public VariableResolver getVariableResolver(String name) {
VariableResolver vr = variableResolvers.get(name);
if (vr != null) {
return vr;
}
else if (variables.containsKey(name)) {
variableResolvers.put(name, vr = new MapVariableResolver(variables, name));
return vr;
}
else if (nextFactory != null) {
return nextFactory.getVariableResolver(name);
}
throw new UnresolveablePropertyException("unable to resolve variable '" + name + "'");
}
public boolean isResolveable(String name) {
return (variableResolvers != null && variableResolvers.containsKey(name))
|| (variables != null && variables.containsKey(name))
|| (nextFactory != null && nextFactory.isResolveable(name));
}
protected VariableResolver addResolver(String name, VariableResolver vr) {
if (variableResolvers == null) variableResolvers = new HashMap();
variableResolvers.put(name, vr);
return vr;
}
public boolean isTarget(String name) {
return variableResolvers != null && variableResolvers.containsKey(name);
}
public Set getKnownVariables() {
if (nextFactory == null) {
if (variables != null) return new HashSet(variables.keySet());
return new HashSet(0);
}
else {
if (variables != null) return new HashSet(variables.keySet());
return new HashSet(0);
}
}
}