PageRenderTime 22ms CodeModel.GetById 12ms app.highlight 7ms RepoModel.GetById 2ms app.codeStats 0ms

/tags/release-0.0.0-rc0/hive/external/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/StandardMapObjectInspector.java

#
Java | 125 lines | 70 code | 17 blank | 38 comment | 8 complexity | 2b7a59cece1df82d5877abf046b4142e 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.objectinspector;
 20
 21import java.util.HashMap;
 22import java.util.Map;
 23
 24/**
 25 * StandardMapObjectInspector works on map data that is stored as a Java Map
 26 * object. Note: the key object of the map must support equals and hashCode by
 27 * itself.
 28 * 
 29 * We also plan to have a GeneralMapObjectInspector which can work on map with
 30 * key objects that does not support equals and hashCode. That will require us
 31 * to store InspectableObject as the key, which will have overridden equals and
 32 * hashCode methods.
 33 * 
 34 * Always use the ObjectInspectorFactory to create new ObjectInspector objects,
 35 * instead of directly creating an instance of this class.
 36 */
 37public class StandardMapObjectInspector implements SettableMapObjectInspector {
 38
 39  ObjectInspector mapKeyObjectInspector;
 40  ObjectInspector mapValueObjectInspector;
 41
 42  /**
 43   * Call ObjectInspectorFactory.getStandardMapObjectInspector instead.
 44   */
 45  protected StandardMapObjectInspector(ObjectInspector mapKeyObjectInspector,
 46      ObjectInspector mapValueObjectInspector) {
 47    this.mapKeyObjectInspector = mapKeyObjectInspector;
 48    this.mapValueObjectInspector = mapValueObjectInspector;
 49  }
 50
 51  // without data
 52  public ObjectInspector getMapKeyObjectInspector() {
 53    return mapKeyObjectInspector;
 54  }
 55
 56  public ObjectInspector getMapValueObjectInspector() {
 57    return mapValueObjectInspector;
 58  }
 59
 60  // with data
 61  // TODO: Now we assume the key Object supports hashCode and equals functions.
 62  public Object getMapValueElement(Object data, Object key) {
 63    if (data == null || key == null) {
 64      return null;
 65    }
 66    Map<?, ?> map = (Map<?, ?>) data;
 67    return map.get(key);
 68  }
 69
 70  public int getMapSize(Object data) {
 71    if (data == null) {
 72      return -1;
 73    }
 74    Map<?, ?> map = (Map<?, ?>) data;
 75    return map.size();
 76  }
 77
 78  public Map<?, ?> getMap(Object data) {
 79    if (data == null) {
 80      return null;
 81    }
 82    Map<?, ?> map = (Map<?, ?>) data;
 83    return map;
 84  }
 85
 86  public final Category getCategory() {
 87    return Category.MAP;
 88  }
 89
 90  public String getTypeName() {
 91    return org.apache.hadoop.hive.serde.Constants.MAP_TYPE_NAME + "<"
 92        + mapKeyObjectInspector.getTypeName() + ","
 93        + mapValueObjectInspector.getTypeName() + ">";
 94  }
 95
 96  // /////////////////////////////
 97  // SettableMapObjectInspector
 98  @Override
 99  public Object create() {
100    Map<Object, Object> m = new HashMap<Object, Object>();
101    return m;
102  }
103
104  @Override
105  public Object clear(Object map) {
106    Map<Object, Object> m = (HashMap<Object, Object>) map;
107    m.clear();
108    return m;
109  }
110
111  @Override
112  public Object put(Object map, Object key, Object value) {
113    Map<Object, Object> m = (HashMap<Object, Object>) map;
114    m.put(key, value);
115    return m;
116  }
117
118  @Override
119  public Object remove(Object map, Object key) {
120    Map<Object, Object> m = (HashMap<Object, Object>) map;
121    m.remove(key);
122    return m;
123  }
124
125}