From 61777591e0c6010067dd2355c92cde75b088bfd8 Mon Sep 17 00:00:00 2001 From: "pabs@pablotron.org" Date: Thu, 18 Dec 2014 16:33:54 -0500 Subject: add template cache --- java/pablotron/luigi/Cache.java | 51 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 java/pablotron/luigi/Cache.java (limited to 'java/pablotron') 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 strings; + private final Map filters; + private final Map templates = new HashMap(); + + public Cache( + final Map strings, + final Map filters + ) { + this.strings = strings; + this.filters = filters; + } + + public Cache(final Map strings) { + this(strings, Filter.FILTERS); + } + + public String run( + final String key, + final Map 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); + } +}; -- cgit v1.2.3