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.hadoop.hbase.hbql.statement.args;
22  
23  import org.apache.expreval.expr.ArgumentListTypeSignature;
24  import org.apache.expreval.expr.ExpressionProperty;
25  import org.apache.expreval.expr.node.GenericValue;
26  import org.apache.hadoop.hbase.hbql.client.HBqlException;
27  
28  import java.io.Serializable;
29  import java.util.concurrent.atomic.AtomicBoolean;
30  
31  public class DefaultArg extends ExpressionProperty implements Serializable {
32  
33      private static final long serialVersionUID = 1L;
34  
35      // We have to make value transient because Object is not serializable for RecordFilter
36      // We will compute it again on the server after reset is called
37      private Serializable  value          = null;
38      private AtomicBoolean atomicComputed = new AtomicBoolean(false);
39  
40      public DefaultArg(final Class<? extends GenericValue> exprType, final GenericValue expr) throws HBqlException {
41          super(new ArgumentListTypeSignature(exprType), expr);
42  
43          this.validate();
44  
45          // This will force the type checking to happen
46          this.getDefaultValue();
47      }
48  
49      private AtomicBoolean getAtomicComputed() {
50          return this.atomicComputed;
51      }
52  
53      public void reset() {
54          this.getAtomicComputed().set(false);
55          this.value = null;
56      }
57  
58      public Object getDefaultValue() throws HBqlException {
59          if (!this.getAtomicComputed().get()) {
60              synchronized (this) {
61                  if (!this.getAtomicComputed().get()) {
62                      // Type checking happens in this call, so we force it above in the constructor
63                      this.value = (Serializable)this.evaluateConstant(0, false);
64  
65                      this.getAtomicComputed().set(true);
66                  }
67              }
68          }
69          return this.value;
70      }
71  
72      public String asString() {
73          return this.getGenericValue(0).asString();
74      }
75  }