View Javadoc

1   /*
2    * Copyright (c) 2011.  The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
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  }