diff options
Diffstat (limited to 'java/pablotron/luigi/Cache.java')
-rw-r--r-- | java/pablotron/luigi/Cache.java | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/java/pablotron/luigi/Cache.java b/java/pablotron/luigi/Cache.java new file mode 100644 index 0000000..b550b46 --- /dev/null +++ b/java/pablotron/luigi/Cache.java @@ -0,0 +1,51 @@ +package pablotron.luigi; + +import java.util.Map; +import java.util.HashMap; +import pablotron.luigi.Filter; +import pablotron.luigi.Template; +import pablotron.luigi.LuigiError; +import pablotron.luigi.actions.Action; + +public final class Cache { + private final Map<String, String> strings; + private final Map<String, Filter.Handler> filters; + private final Map<String, Template> templates = new HashMap<String, Template>(); + + public Cache( + final Map<String, String> strings, + final Map<String, Filter.Handler> filters + ) { + this.strings = strings; + this.filters = filters; + } + + public Cache(final Map<String, String> strings) { + this(strings, Filter.FILTERS); + } + + public String run( + final String key, + final Map<String, String> args + ) throws LuigiError { + Template t; + + if (templates.containsKey(key)) { + // get template + t = templates.get(key); + } else { + // make sure template exists + if (!strings.containsKey(key)) + throw new LuigiError("unknown template: " + key); + + // create template + t = new Template(strings.get(key), filters); + + // cache template + templates.put(key, t); + } + + // run template with args + return t.run(args); + } +}; |