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