1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.expreval.expr.casestmt;
22
23 import org.apache.expreval.client.NullColumnValueException;
24 import org.apache.expreval.client.ResultMissingColumnException;
25 import org.apache.expreval.expr.DelegateStmt;
26 import org.apache.expreval.expr.ExpressionType;
27 import org.apache.hadoop.hbase.hbql.client.HBqlException;
28 import org.apache.hadoop.hbase.hbql.impl.HConnectionImpl;
29
30 import java.util.List;
31
32 public abstract class GenericCase extends DelegateStmt<GenericCase> {
33
34 private final List<GenericCaseWhen> whenExprList;
35 private GenericCaseElse elseExpr;
36
37 protected GenericCase(final ExpressionType type, final List<GenericCaseWhen> whenExprList, final GenericCaseElse elseExpr) {
38 super(type);
39 this.whenExprList = whenExprList;
40 this.elseExpr = elseExpr;
41 }
42
43 public Object getValue(final HConnectionImpl conn, final Object object) throws HBqlException,
44 ResultMissingColumnException,
45 NullColumnValueException {
46 for (final GenericCaseWhen when : this.getWhenExprList()) {
47 final boolean predicate = when.getPredicateValue(conn, object);
48 if (predicate)
49 return when.getValue(conn, object);
50 }
51
52 if (this.getElseExpr() != null)
53 return this.getElseExpr().getValue(conn, object);
54
55 return null;
56 }
57
58 public void reset() {
59 for (final GenericCaseWhen when : this.getWhenExprList())
60 when.reset();
61
62 if (this.getElseExpr() != null)
63 this.getElseExpr().reset();
64 }
65
66 public String asString() {
67
68 final StringBuilder sbuf = new StringBuilder();
69
70 sbuf.append("CASE ");
71
72 for (final GenericCaseWhen when : this.getWhenExprList())
73 sbuf.append(when.asString());
74
75 if (this.getElseExpr() != null)
76 sbuf.append(this.getElseExpr().asString());
77
78 sbuf.append("END");
79
80 return sbuf.toString();
81 }
82
83
84 protected List<GenericCaseWhen> getWhenExprList() {
85 return this.whenExprList;
86 }
87
88 protected GenericCaseElse getElseExpr() {
89 return this.elseExpr;
90 }
91
92 protected void setElseExpr(final GenericCaseElse elseExpr) {
93 this.elseExpr = elseExpr;
94 }
95 }