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.TypeSupport;
26 import org.apache.expreval.expr.node.BooleanValue;
27 import org.apache.expreval.expr.node.DateValue;
28 import org.apache.expreval.expr.node.GenericValue;
29 import org.apache.expreval.expr.node.NumberValue;
30 import org.apache.expreval.expr.node.StringValue;
31 import org.apache.hadoop.hbase.hbql.client.HBqlException;
32 import org.apache.hadoop.hbase.hbql.impl.HConnectionImpl;
33
34 import java.util.ArrayList;
35
36 public class DelegateCase extends GenericCase {
37
38 public DelegateCase() {
39 super(null, new ArrayList<GenericCaseWhen>(), null);
40 }
41
42 public Class<? extends GenericValue> validateTypes(final GenericValue parentExpr,
43 final boolean allowCollections) throws HBqlException {
44
45 final Class<? extends GenericValue> type = this.getWhenExprList().get(0).validateTypes(this, false);
46 final Class<? extends GenericValue> argType = this.getGenericValueClass(type);
47
48 for (final GenericCaseWhen val : this.getWhenExprList())
49 this.validateParentClass(argType, val.validateTypes(this, false));
50
51 if (this.getElseExpr() != null)
52 this.validateParentClass(argType, this.getElseExpr().validateTypes(parentExpr, false));
53
54 if (TypeSupport.isParentClass(StringValue.class, argType))
55 this.setTypedExpr(new StringCase(this.getWhenExprList(), this.getElseExpr()));
56 else if (TypeSupport.isParentClass(NumberValue.class, argType))
57 this.setTypedExpr(new NumberCase(this.getWhenExprList(), this.getElseExpr()));
58 else if (TypeSupport.isParentClass(DateValue.class, argType))
59 this.setTypedExpr(new DateCase(this.getWhenExprList(), this.getElseExpr()));
60 else if (TypeSupport.isParentClass(BooleanValue.class, argType))
61 this.setTypedExpr(new BooleanCase(this.getWhenExprList(), this.getElseExpr()));
62 else
63 this.throwInvalidTypeException(argType);
64
65 return this.getTypedExpr().validateTypes(parentExpr, false);
66 }
67
68 public GenericValue getOptimizedValue() throws HBqlException {
69 this.optimizeAllArgs();
70 return !this.isAConstant() ? this : this.getTypedExpr().getOptimizedValue();
71 }
72
73 public Object getValue(final HConnectionImpl conn, final Object object) throws HBqlException,
74 ResultMissingColumnException,
75 NullColumnValueException {
76 return this.getTypedExpr().getValue(conn, object);
77 }
78
79 public void addWhen(final GenericValue pred, final GenericValue value) {
80 this.getWhenExprList().add(new DelegateCaseWhen(pred, value));
81 }
82
83 public void addElse(final GenericValue value) {
84 if (value != null)
85 this.setElseExpr(new DelegateCaseElse(value));
86 }
87 }