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.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  }