PageRenderTime 47ms CodeModel.GetById 16ms app.highlight 24ms RepoModel.GetById 2ms app.codeStats 0ms

/tags/release-0.1-rc2/hive/external/serde/src/java/org/apache/hadoop/hive/serde2/typeinfo/TypeInfoFactory.java

#
Java | 139 lines | 94 code | 20 blank | 25 comment | 14 complexity | f1e2e759fb17a0e4a6544112a6d4efe4 MD5 | raw file
  1/**
  2 * Licensed to the Apache Software Foundation (ASF) under one
  3 * or more contributor license agreements.  See the NOTICE file
  4 * distributed with this work for additional information
  5 * regarding copyright ownership.  The ASF licenses this file
  6 * to you under the Apache License, Version 2.0 (the
  7 * "License"); you may not use this file except in compliance
  8 * with the License.  You may obtain a copy of the License at
  9 *
 10 *     http://www.apache.org/licenses/LICENSE-2.0
 11 *
 12 * Unless required by applicable law or agreed to in writing, software
 13 * distributed under the License is distributed on an "AS IS" BASIS,
 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15 * See the License for the specific language governing permissions and
 16 * limitations under the License.
 17 */
 18
 19package org.apache.hadoop.hive.serde2.typeinfo;
 20
 21import java.util.ArrayList;
 22import java.util.HashMap;
 23import java.util.List;
 24
 25import org.apache.hadoop.hive.serde.Constants;
 26import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils;
 27
 28/**
 29 * TypeInfoFactory can be used to create the TypeInfo object for any types.
 30 * 
 31 * TypeInfo objects are all read-only so we can reuse them easily.
 32 * TypeInfoFactory has internal cache to make sure we don't create 2 TypeInfo
 33 * objects that represents the same type.
 34 */
 35public final class TypeInfoFactory {
 36
 37  static HashMap<String, TypeInfo> cachedPrimitiveTypeInfo = new HashMap<String, TypeInfo>();
 38
 39  private TypeInfoFactory() {
 40    // prevent instantiation
 41  }
 42
 43  public static TypeInfo getPrimitiveTypeInfo(String typeName) {
 44    if (null == PrimitiveObjectInspectorUtils
 45        .getTypeEntryFromTypeName(typeName)) {
 46      throw new RuntimeException("Cannot getPrimitiveTypeInfo for " + typeName);
 47    }
 48    TypeInfo result = cachedPrimitiveTypeInfo.get(typeName);
 49    if (result == null) {
 50      result = new PrimitiveTypeInfo(typeName);
 51      cachedPrimitiveTypeInfo.put(typeName, result);
 52    }
 53    return result;
 54  }
 55
 56  public static final TypeInfo voidTypeInfo = getPrimitiveTypeInfo(Constants.VOID_TYPE_NAME);
 57  public static final TypeInfo booleanTypeInfo = getPrimitiveTypeInfo(Constants.BOOLEAN_TYPE_NAME);
 58  public static final TypeInfo intTypeInfo = getPrimitiveTypeInfo(Constants.INT_TYPE_NAME);
 59  public static final TypeInfo longTypeInfo = getPrimitiveTypeInfo(Constants.BIGINT_TYPE_NAME);
 60  public static final TypeInfo stringTypeInfo = getPrimitiveTypeInfo(Constants.STRING_TYPE_NAME);
 61  public static final TypeInfo floatTypeInfo = getPrimitiveTypeInfo(Constants.FLOAT_TYPE_NAME);
 62  public static final TypeInfo doubleTypeInfo = getPrimitiveTypeInfo(Constants.DOUBLE_TYPE_NAME);
 63  public static final TypeInfo byteTypeInfo = getPrimitiveTypeInfo(Constants.TINYINT_TYPE_NAME);
 64  public static final TypeInfo shortTypeInfo = getPrimitiveTypeInfo(Constants.SMALLINT_TYPE_NAME);
 65
 66  public static final TypeInfo unknownTypeInfo = getPrimitiveTypeInfo("unknown");
 67
 68  public static TypeInfo getPrimitiveTypeInfoFromPrimitiveWritable(
 69      Class<?> clazz) {
 70    String typeName = PrimitiveObjectInspectorUtils
 71        .getTypeNameFromPrimitiveWritable(clazz);
 72    if (typeName == null) {
 73      throw new RuntimeException("Internal error: Cannot get typeName for "
 74          + clazz);
 75    }
 76    return getPrimitiveTypeInfo(typeName);
 77  }
 78
 79  public static TypeInfo getPrimitiveTypeInfoFromJavaPrimitive(Class<?> clazz) {
 80    return getPrimitiveTypeInfo(PrimitiveObjectInspectorUtils
 81        .getTypeNameFromPrimitiveJava(clazz));
 82  }
 83
 84  static HashMap<ArrayList<List<?>>, TypeInfo> cachedStructTypeInfo =
 85    new HashMap<ArrayList<List<?>>, TypeInfo>();
 86
 87  public static TypeInfo getStructTypeInfo(List<String> names,
 88      List<TypeInfo> typeInfos) {
 89    ArrayList<List<?>> signature = new ArrayList<List<?>>(2);
 90    signature.add(names);
 91    signature.add(typeInfos);
 92    TypeInfo result = cachedStructTypeInfo.get(signature);
 93    if (result == null) {
 94      result = new StructTypeInfo(names, typeInfos);
 95      cachedStructTypeInfo.put(signature, result);
 96    }
 97    return result;
 98  }
 99
100  static HashMap<List<?>, TypeInfo> cachedUnionTypeInfo =
101    new HashMap<List<?>, TypeInfo>();
102
103  public static TypeInfo getUnionTypeInfo(List<TypeInfo> typeInfos) {
104    TypeInfo result = cachedUnionTypeInfo.get(typeInfos);
105    if (result == null) {
106      result = new UnionTypeInfo(typeInfos);
107      cachedUnionTypeInfo.put(typeInfos, result);
108    }
109    return result;
110  }
111
112  static HashMap<TypeInfo, TypeInfo> cachedListTypeInfo = new HashMap<TypeInfo, TypeInfo>();
113
114  public static TypeInfo getListTypeInfo(TypeInfo elementTypeInfo) {
115    TypeInfo result = cachedListTypeInfo.get(elementTypeInfo);
116    if (result == null) {
117      result = new ListTypeInfo(elementTypeInfo);
118      cachedListTypeInfo.put(elementTypeInfo, result);
119    }
120    return result;
121  }
122
123  static HashMap<ArrayList<TypeInfo>, TypeInfo> cachedMapTypeInfo =
124    new HashMap<ArrayList<TypeInfo>, TypeInfo>();
125
126  public static TypeInfo getMapTypeInfo(TypeInfo keyTypeInfo,
127      TypeInfo valueTypeInfo) {
128    ArrayList<TypeInfo> signature = new ArrayList<TypeInfo>(2);
129    signature.add(keyTypeInfo);
130    signature.add(valueTypeInfo);
131    TypeInfo result = cachedMapTypeInfo.get(signature);
132    if (result == null) {
133      result = new MapTypeInfo(keyTypeInfo, valueTypeInfo);
134      cachedMapTypeInfo.put(signature, result);
135    }
136    return result;
137  };
138
139}