public class GroovyScriptCachingBuilder {
private GroovyShell shell;
public GroovyScriptCachingBuilder() {
this(null);
}
public GroovyScriptCachingBuilder(GroovyShell shell) {
this.shell = shell;
}
private final Map<String, Script> scripts = new HashMap<>();
public Script getScript(final String expression) {
Script script;
if (scripts.containsKey(expression)) {
script = scripts.get(expression);
} else {
synchronized (scripts) {
if (scripts.containsKey(expression)) {
script = scripts.get(expression);
} else {
script = shell.parse(expression);
scripts.put(expression, script);
}
}
}
return script;
}
}