/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. package org.apache.hadoop.hive.serde2.typeinfo;
  19. import java.util.ArrayList;
  20. import java.util.HashMap;
  21. import java.util.List;
  22. import org.apache.hadoop.hive.serde.Constants;
  23. import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils;
  24. /**
  25. * TypeInfoFactory can be used to create the TypeInfo object for any types.
  26. *
  27. * TypeInfo objects are all read-only so we can reuse them easily.
  28. * TypeInfoFactory has internal cache to make sure we don't create 2 TypeInfo
  29. * objects that represents the same type.
  30. */
  31. public final class TypeInfoFactory {
  32. static HashMap<String, TypeInfo> cachedPrimitiveTypeInfo = new HashMap<String, TypeInfo>();
  33. private TypeInfoFactory() {
  34. // prevent instantiation
  35. }
  36. public static TypeInfo getPrimitiveTypeInfo(String typeName) {
  37. if (null == PrimitiveObjectInspectorUtils
  38. .getTypeEntryFromTypeName(typeName)) {
  39. throw new RuntimeException("Cannot getPrimitiveTypeInfo for " + typeName);
  40. }
  41. TypeInfo result = cachedPrimitiveTypeInfo.get(typeName);
  42. if (result == null) {
  43. result = new PrimitiveTypeInfo(typeName);
  44. cachedPrimitiveTypeInfo.put(typeName, result);
  45. }
  46. return result;
  47. }
  48. public static final TypeInfo voidTypeInfo = getPrimitiveTypeInfo(Constants.VOID_TYPE_NAME);
  49. public static final TypeInfo booleanTypeInfo = getPrimitiveTypeInfo(Constants.BOOLEAN_TYPE_NAME);
  50. public static final TypeInfo intTypeInfo = getPrimitiveTypeInfo(Constants.INT_TYPE_NAME);
  51. public static final TypeInfo longTypeInfo = getPrimitiveTypeInfo(Constants.BIGINT_TYPE_NAME);
  52. public static final TypeInfo stringTypeInfo = getPrimitiveTypeInfo(Constants.STRING_TYPE_NAME);
  53. public static final TypeInfo floatTypeInfo = getPrimitiveTypeInfo(Constants.FLOAT_TYPE_NAME);
  54. public static final TypeInfo doubleTypeInfo = getPrimitiveTypeInfo(Constants.DOUBLE_TYPE_NAME);
  55. public static final TypeInfo byteTypeInfo = getPrimitiveTypeInfo(Constants.TINYINT_TYPE_NAME);
  56. public static final TypeInfo shortTypeInfo = getPrimitiveTypeInfo(Constants.SMALLINT_TYPE_NAME);
  57. public static final TypeInfo unknownTypeInfo = getPrimitiveTypeInfo("unknown");
  58. public static TypeInfo getPrimitiveTypeInfoFromPrimitiveWritable(
  59. Class<?> clazz) {
  60. String typeName = PrimitiveObjectInspectorUtils
  61. .getTypeNameFromPrimitiveWritable(clazz);
  62. if (typeName == null) {
  63. throw new RuntimeException("Internal error: Cannot get typeName for "
  64. + clazz);
  65. }
  66. return getPrimitiveTypeInfo(typeName);
  67. }
  68. public static TypeInfo getPrimitiveTypeInfoFromJavaPrimitive(Class<?> clazz) {
  69. return getPrimitiveTypeInfo(PrimitiveObjectInspectorUtils
  70. .getTypeNameFromPrimitiveJava(clazz));
  71. }
  72. static HashMap<ArrayList<List<?>>, TypeInfo> cachedStructTypeInfo =
  73. new HashMap<ArrayList<List<?>>, TypeInfo>();
  74. public static TypeInfo getStructTypeInfo(List<String> names,
  75. List<TypeInfo> typeInfos) {
  76. ArrayList<List<?>> signature = new ArrayList<List<?>>(2);
  77. signature.add(names);
  78. signature.add(typeInfos);
  79. TypeInfo result = cachedStructTypeInfo.get(signature);
  80. if (result == null) {
  81. result = new StructTypeInfo(names, typeInfos);
  82. cachedStructTypeInfo.put(signature, result);
  83. }
  84. return result;
  85. }
  86. static HashMap<List<?>, TypeInfo> cachedUnionTypeInfo =
  87. new HashMap<List<?>, TypeInfo>();
  88. public static TypeInfo getUnionTypeInfo(List<TypeInfo> typeInfos) {
  89. TypeInfo result = cachedUnionTypeInfo.get(typeInfos);
  90. if (result == null) {
  91. result = new UnionTypeInfo(typeInfos);
  92. cachedUnionTypeInfo.put(typeInfos, result);
  93. }
  94. return result;
  95. }
  96. static HashMap<TypeInfo, TypeInfo> cachedListTypeInfo = new HashMap<TypeInfo, TypeInfo>();
  97. public static TypeInfo getListTypeInfo(TypeInfo elementTypeInfo) {
  98. TypeInfo result = cachedListTypeInfo.get(elementTypeInfo);
  99. if (result == null) {
  100. result = new ListTypeInfo(elementTypeInfo);
  101. cachedListTypeInfo.put(elementTypeInfo, result);
  102. }
  103. return result;
  104. }
  105. static HashMap<ArrayList<TypeInfo>, TypeInfo> cachedMapTypeInfo =
  106. new HashMap<ArrayList<TypeInfo>, TypeInfo>();
  107. public static TypeInfo getMapTypeInfo(TypeInfo keyTypeInfo,
  108. TypeInfo valueTypeInfo) {
  109. ArrayList<TypeInfo> signature = new ArrayList<TypeInfo>(2);
  110. signature.add(keyTypeInfo);
  111. signature.add(valueTypeInfo);
  112. TypeInfo result = cachedMapTypeInfo.get(signature);
  113. if (result == null) {
  114. result = new MapTypeInfo(keyTypeInfo, valueTypeInfo);
  115. cachedMapTypeInfo.put(signature, result);
  116. }
  117. return result;
  118. };
  119. }