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.impl;
22  
23  import org.apache.hadoop.hbase.hbql.client.HBqlException;
24  import org.apache.hadoop.hbase.hbql.mapping.Mapping;
25  import org.apache.hadoop.hbase.hbql.util.Lists;
26  import org.apache.hadoop.hbase.hbql.util.Maps;
27  
28  import java.lang.reflect.Field;
29  import java.util.Collection;
30  import java.util.Date;
31  import java.util.Map;
32  
33  public class ReflectionMapping extends Mapping {
34  
35      private static final Map<Class<?>, ReflectionMapping> reflectionMappingMap = Maps.newHashMap();
36  
37      private ReflectionMapping(final Class clazz) {
38          super(clazz.getName(), null);
39  
40          for (final Field field : clazz.getDeclaredFields()) {
41  
42              if (field.getType().isArray())
43                  continue;
44  
45              if (field.getType().isPrimitive()
46                  || field.getType().equals(String.class)
47                  || field.getType().equals(Date.class)) {
48                  final ReflectionAttrib attrib = new ReflectionAttrib(field);
49                  try {
50                      addAttribToVariableNameMap(attrib, attrib.getVariableName());
51                  }
52                  catch (HBqlException e) {
53                      // Not going to be hit with ReflectionMapping
54                      e.printStackTrace();
55                  }
56              }
57          }
58      }
59  
60      public static ReflectionMapping getReflectionMapping(final Object obj) {
61          return getReflectionMapping(obj.getClass());
62      }
63  
64      public synchronized static ReflectionMapping getReflectionMapping(final Class clazz) {
65  
66          ReflectionMapping mapping = getReflectionMappingMap().get(clazz);
67          if (mapping != null)
68              return mapping;
69  
70          mapping = new ReflectionMapping(clazz);
71          getReflectionMappingMap().put(clazz, mapping);
72          return mapping;
73      }
74  
75      private static Map<Class<?>, ReflectionMapping> getReflectionMappingMap() {
76          return reflectionMappingMap;
77      }
78  
79      public Collection<String> getMappingFamilyNames() throws HBqlException {
80          return Lists.newArrayList();
81      }
82  }