]> WPIA git - gigi.git/blob - src/club/wpia/gigi/output/template/IfStatement.java
upd: rename package name and all references to it
[gigi.git] / src / club / wpia / gigi / output / template / IfStatement.java
1 package club.wpia.gigi.output.template;
2
3 import java.io.PrintWriter;
4 import java.util.Collection;
5 import java.util.Map;
6
7 import club.wpia.gigi.localisation.Language;
8
9 /**
10  * One ore two {@link Outputable}s that are emitted conditionally if a given
11  * variable is neither <code>null</code> nor {@link Boolean#FALSE}.
12  */
13 public final class IfStatement implements Translatable {
14
15     private final String variable;
16
17     private final TemplateBlock iftrue;
18
19     private final TemplateBlock iffalse;
20
21     /**
22      * Creates a new {@link IfStatement} with an empty else-part.
23      * 
24      * @param variable
25      *            the variable to check.
26      * @param body
27      *            the body to emit conditionally.
28      */
29     public IfStatement(String variable, TemplateBlock body) {
30         this.variable = variable;
31         this.iftrue = body;
32         this.iffalse = null;
33     }
34
35     /**
36      * Creates a new {@link IfStatement} with an else-block.
37      * 
38      * @param variable
39      *            the variable to check.
40      * @param iftrue
41      *            the block to emit if the check succeeds.
42      * @param iffalse
43      *            the block to emit if the check fails.
44      */
45     public IfStatement(String variable, TemplateBlock iftrue, TemplateBlock iffalse) {
46         this.variable = variable;
47         this.iftrue = iftrue;
48         this.iffalse = iffalse;
49     }
50
51     @Override
52     public void output(PrintWriter out, Language l, Map<String, Object> vars) {
53         Object o = vars.get(variable);
54
55         if ( !(o == null || Boolean.FALSE.equals(o))) {
56             iftrue.output(out, l, vars);
57         } else if (iffalse != null) {
58             iffalse.output(out, l, vars);
59         }
60     }
61
62     @Override
63     public void addTranslations(Collection<String> s) {
64         iftrue.addTranslations(s);
65         if (iffalse != null) {
66             iffalse.addTranslations(s);
67         }
68     }
69
70 }