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.instmt;
22  
23  import org.apache.expreval.client.NullColumnValueException;
24  import org.apache.expreval.client.ResultMissingColumnException;
25  import org.apache.expreval.expr.ExpressionType;
26  import org.apache.expreval.expr.NotValue;
27  import org.apache.expreval.expr.node.BooleanValue;
28  import org.apache.expreval.expr.node.GenericValue;
29  import org.apache.hadoop.hbase.filter.WritableByteArrayComparable;
30  import org.apache.hadoop.hbase.hbql.client.HBqlException;
31  import org.apache.hadoop.hbase.hbql.impl.HConnectionImpl;
32  import org.apache.hadoop.hbase.hbql.impl.InvalidServerFilterException;
33  
34  import java.util.List;
35  
36  public abstract class GenericInStmt extends NotValue<GenericInStmt> implements BooleanValue {
37  
38      protected GenericInStmt(final GenericValue arg0, final boolean not, final List<GenericValue> inList) {
39          super(ExpressionType.INSTMT, not, arg0, inList);
40      }
41  
42      protected abstract boolean evaluateInList(final Object object) throws HBqlException,
43                                                                            ResultMissingColumnException,
44                                                                            NullColumnValueException;
45  
46      protected List<GenericValue> getInList() {
47          return this.getSubArgs(1);
48      }
49  
50      public Boolean getValue(final HConnectionImpl conn, final Object object) throws HBqlException,
51                                                                                      ResultMissingColumnException,
52                                                                                      NullColumnValueException {
53          final boolean retval = this.evaluateInList(object);
54          return (this.isNot()) ? !retval : retval;
55      }
56  
57      public Class<? extends GenericValue> validateTypes(final GenericValue parentExpr,
58                                                         final boolean allowCollections) throws HBqlException {
59          return BooleanValue.class;
60      }
61  
62      public String asString() {
63          final StringBuilder sbuf = new StringBuilder(this.getExprArg(0).asString() + notAsString() + " IN (");
64  
65          boolean first = true;
66          for (final GenericValue valueExpr : this.getInList()) {
67              if (!first)
68                  sbuf.append(", ");
69              sbuf.append(valueExpr.asString());
70              first = false;
71          }
72          sbuf.append(")");
73          return sbuf.toString();
74      }
75  
76      private boolean inListIsConstant() {
77          for (final GenericValue valueExpr : this.getInList()) {
78              if (!valueExpr.isAConstant())
79                  return false;
80          }
81          return true;
82      }
83  
84      protected abstract static class GenericInComparable<T> extends WritableByteArrayComparable {
85  
86          private List<T> inValues;
87  
88          protected List<T> getInValues() {
89              return this.inValues;
90          }
91  
92          protected void setInValue(final List<T> inValues) {
93              this.inValues = inValues;
94          }
95      }
96  
97      protected void validateArgsForInFilter() throws InvalidServerFilterException {
98          if (this.getExprArg(0).isAColumnReference() && this.inListIsConstant())
99              return;
100 
101         throw new InvalidServerFilterException("Filter requires a column reference and list of constants");
102     }
103 }