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