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;
22  
23  import org.apache.expreval.expr.var.NamedParameter;
24  import org.apache.hadoop.hbase.hbql.client.HBqlException;
25  import org.apache.hadoop.hbase.hbql.util.AtomicReferences;
26  import org.apache.hadoop.hbase.hbql.util.Lists;
27  
28  import java.io.Serializable;
29  import java.util.Collection;
30  import java.util.List;
31  import java.util.SortedSet;
32  import java.util.TreeSet;
33  import java.util.concurrent.atomic.AtomicReference;
34  
35  public class NamedParameters implements Serializable {
36  
37      private static final long serialVersionUID = 1L;
38  
39      private final SortedSet<NamedParameter>             paramSet        = new TreeSet<NamedParameter>(NamedParameter.getComparator());
40      private final AtomicReference<List<NamedParameter>> atomicParamList = AtomicReferences.newAtomicReference();
41  
42      private SortedSet<NamedParameter> getParamSet() {
43          return this.paramSet;
44      }
45  
46      public void addParameters(final Collection<NamedParameter> params) {
47          if (params != null)
48              this.getParamSet().addAll(params);
49      }
50  
51      public AtomicReference<List<NamedParameter>> getAtomicParamList() {
52          return atomicParamList;
53      }
54  
55      public List<NamedParameter> getParameterList() {
56          if (this.getAtomicParamList().get() == null) {
57              synchronized (this) {
58                  if (this.getAtomicParamList().get() == null) {
59                      // This takes the ordered set and converts to a list
60                      // The order is determined by when the param was created.
61                      final int size = this.getParamSet().size();
62                      final List<NamedParameter> val = Lists.newArrayList(this.getParamSet()
63                                                                              .toArray(new NamedParameter[size]));
64                      this.getAtomicParamList().set(val);
65                  }
66              }
67          }
68          return this.getAtomicParamList().get();
69      }
70  
71      public NamedParameter getParameter(final int i) throws HBqlException {
72          try {
73              return this.getParameterList().get(i - 1);
74          }
75          catch (Exception e) {
76              throw new HBqlException("Invalid index: " + (i - 1));
77          }
78      }
79  
80      public void clearParameters() {
81          for (final NamedParameter param : this.getParameterList())
82              param.reset();
83      }
84  }