View Javadoc

1   /*
2    * Copyright (c) 2010.  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.impl;
22  
23  import org.apache.hadoop.hbase.hbql.client.HBqlException;
24  import org.apache.hadoop.hbase.hbql.mapping.ColumnAttrib;
25  
26  import java.io.Serializable;
27  import java.util.HashMap;
28  
29  public class ElementMap<T extends Value> extends HashMap<String, T> implements Serializable {
30  
31      private static final long serialVersionUID = 1L;
32  
33      private HRecordImpl record;
34  
35      public ElementMap() {
36      }
37  
38      public ElementMap(final HRecordImpl record) {
39          this.record = record;
40      }
41  
42      private HRecordImpl getRecord() {
43          return this.record;
44      }
45  
46      public void addElement(final T value) throws HBqlException {
47          final ColumnAttrib attrib = this.getRecord().getResultAccessor().getColumnAttribByName(value.getName());
48          final String name = (attrib == null) ? value.getName() : attrib.getFamilyQualifiedName();
49          this.put(name, value);
50      }
51  
52      public boolean containsName(final String name) {
53          return this.containsKey(name);
54      }
55  
56      private T getElement(final String name) {
57          return this.get(name);
58      }
59  
60      public T findElement(final String name) throws HBqlException {
61  
62          // First try the name given.
63          // If that doesn't work, then try qualified name
64          if (this.containsName(name))
65              return this.getElement(name);
66  
67          // Look up by  alias name
68          final ColumnAttrib attrib = this.getRecord().getResultAccessor().getColumnAttribByName(name);
69  
70          if (attrib != null) {
71              final String qualifiedName = attrib.getFamilyQualifiedName();
72              if (!qualifiedName.equals(name) && this.containsName(qualifiedName))
73                  return this.getElement(qualifiedName);
74          }
75  
76          return null;
77      }
78  }