PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Java | 149 lines | 87 code | 21 blank | 41 comment | 12 complexity | 8e1586e5faf0829d039d314048da7e75 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, JSON, CPL-1.0
  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.io.Serializable;
  20. import java.util.ArrayList;
  21. import java.util.Iterator;
  22. import java.util.List;
  23. import org.apache.hadoop.hive.serde.Constants;
  24. import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector.Category;
  25. /**
  26. * StructTypeInfo represents the TypeInfo of a struct. A struct contains one or
  27. * more fields each of which has a unique name and its own TypeInfo. Different
  28. * fields can have the same or different TypeInfo.
  29. *
  30. * Always use the TypeInfoFactory to create new TypeInfo objects, instead of
  31. * directly creating an instance of this class.
  32. */
  33. public final class StructTypeInfo extends TypeInfo implements Serializable {
  34. private static final long serialVersionUID = 1L;
  35. private ArrayList<String> allStructFieldNames;
  36. private ArrayList<TypeInfo> allStructFieldTypeInfos;
  37. /**
  38. * For java serialization use only.
  39. */
  40. public StructTypeInfo() {
  41. }
  42. @Override
  43. public String getTypeName() {
  44. StringBuilder sb = new StringBuilder();
  45. sb.append(Constants.STRUCT_TYPE_NAME + "<");
  46. for (int i = 0; i < allStructFieldNames.size(); i++) {
  47. if (i > 0) {
  48. sb.append(",");
  49. }
  50. sb.append(allStructFieldNames.get(i));
  51. sb.append(":");
  52. sb.append(allStructFieldTypeInfos.get(i).getTypeName());
  53. }
  54. sb.append(">");
  55. return sb.toString();
  56. }
  57. /**
  58. * For java serialization use only.
  59. */
  60. public void setAllStructFieldNames(ArrayList<String> allStructFieldNames) {
  61. this.allStructFieldNames = allStructFieldNames;
  62. }
  63. /**
  64. * For java serialization use only.
  65. */
  66. public void setAllStructFieldTypeInfos(
  67. ArrayList<TypeInfo> allStructFieldTypeInfos) {
  68. this.allStructFieldTypeInfos = allStructFieldTypeInfos;
  69. }
  70. /**
  71. * For TypeInfoFactory use only.
  72. */
  73. StructTypeInfo(List<String> names, List<TypeInfo> typeInfos) {
  74. allStructFieldNames = new ArrayList<String>();
  75. allStructFieldNames.addAll(names);
  76. allStructFieldTypeInfos = new ArrayList<TypeInfo>();
  77. allStructFieldTypeInfos.addAll(typeInfos);
  78. }
  79. @Override
  80. public Category getCategory() {
  81. return Category.STRUCT;
  82. }
  83. public ArrayList<String> getAllStructFieldNames() {
  84. return allStructFieldNames;
  85. }
  86. public ArrayList<TypeInfo> getAllStructFieldTypeInfos() {
  87. return allStructFieldTypeInfos;
  88. }
  89. public TypeInfo getStructFieldTypeInfo(String field) {
  90. String fieldLowerCase = field.toLowerCase();
  91. for (int i = 0; i < allStructFieldNames.size(); i++) {
  92. if (fieldLowerCase.equals(allStructFieldNames.get(i))) {
  93. return allStructFieldTypeInfos.get(i);
  94. }
  95. }
  96. throw new RuntimeException("cannot find field " + field
  97. + "(lowercase form: " + fieldLowerCase + ") in " + allStructFieldNames);
  98. // return null;
  99. }
  100. @Override
  101. public boolean equals(Object other) {
  102. if (this == other) {
  103. return true;
  104. }
  105. if (!(other instanceof StructTypeInfo)) {
  106. return false;
  107. }
  108. StructTypeInfo o = (StructTypeInfo) other;
  109. Iterator<String> namesIterator = getAllStructFieldNames().iterator();
  110. Iterator<String> otherNamesIterator = o.getAllStructFieldNames().iterator();
  111. // Compare the field names using ignore-case semantics
  112. while (namesIterator.hasNext() && otherNamesIterator.hasNext()) {
  113. if (!namesIterator.next().equalsIgnoreCase(otherNamesIterator.next())) {
  114. return false;
  115. }
  116. }
  117. // Different number of field names
  118. if (namesIterator.hasNext() || otherNamesIterator.hasNext()) {
  119. return false;
  120. }
  121. // Compare the field types
  122. return o.getAllStructFieldTypeInfos().equals(getAllStructFieldTypeInfos());
  123. }
  124. @Override
  125. public int hashCode() {
  126. return allStructFieldNames.hashCode() ^ allStructFieldTypeInfos.hashCode();
  127. }
  128. }