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.yaoql.client;
22  
23  import com.google.common.base.Predicate;
24  import org.apache.expreval.client.NullColumnValueException;
25  import org.apache.expreval.client.ResultMissingColumnException;
26  import org.apache.expreval.expr.ExpressionTree;
27  import org.apache.hadoop.hbase.hbql.client.HBqlException;
28  import org.apache.hadoop.hbase.hbql.mapping.MappingContext;
29  import org.apache.hadoop.hbase.hbql.parser.ParserUtil;
30  import org.apache.yaoql.impl.ParameterBinding;
31  import org.apache.yaoql.impl.ReflectionMapping;
32  
33  import java.util.concurrent.atomic.AtomicBoolean;
34  
35  public class ObjectQueryPredicate<T> extends ParameterBinding implements Predicate<T> {
36  
37      private final String query;
38      private ExpressionTree expressionTree = null;
39      private AtomicBoolean  initialized    = new AtomicBoolean(false);
40  
41      public ObjectQueryPredicate(final String query) {
42          this.query = query;
43      }
44  
45      public void reset() {
46          this.initialized.set(false);
47      }
48  
49      public String getQuery() {
50          return this.query;
51      }
52  
53      private ExpressionTree getExpressionTree() {
54          return this.expressionTree;
55      }
56  
57      private boolean isInitialized() {
58          return this.initialized.get();
59      }
60  
61      public boolean apply(final T obj) {
62  
63          try {
64              if (!this.isInitialized()) {
65                  final ReflectionMapping mapping = ReflectionMapping.getReflectionMapping(obj);
66                  final MappingContext mappingContext = new MappingContext(mapping);
67                  this.expressionTree = ParserUtil.parseWhereExpression(this.query, mappingContext);
68                  this.applyParameters(this.getExpressionTree());
69                  this.initialized.set(true);
70              }
71  
72              return this.getExpressionTree().evaluate(null, obj);
73          }
74          catch (ResultMissingColumnException e) {
75              // Not possible
76              return false;
77          }
78          catch (NullColumnValueException e) {
79              // Not possible
80              return false;
81          }
82          catch (HBqlException e) {
83              e.printStackTrace();
84              return false;
85          }
86      }
87  }