aboutsummaryrefslogtreecommitdiff
path: root/java/pablotron/luigi/Cache.java
blob: b550b46e0282fe80d927ec6924cf2f6c0e88e00f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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);
  }
};