aboutsummaryrefslogtreecommitdiff
path: root/java/pablotron/luigi
diff options
context:
space:
mode:
authorpabs@pablotron.org <pabs@pablotron.org>2014-12-18 16:33:54 -0500
committerpabs@pablotron.org <pabs@pablotron.org>2014-12-18 16:33:54 -0500
commit61777591e0c6010067dd2355c92cde75b088bfd8 (patch)
treeec6e0bc95827dd4922e117539f2e6e8cd2a3b848 /java/pablotron/luigi
parent67e810cce303cc7d3c10ca05b05812d2b04104bb (diff)
downloadluigi-template-61777591e0c6010067dd2355c92cde75b088bfd8.tar.bz2
luigi-template-61777591e0c6010067dd2355c92cde75b088bfd8.zip
add template cache
Diffstat (limited to 'java/pablotron/luigi')
-rw-r--r--java/pablotron/luigi/Cache.java51
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);
+ }
+};