All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.pig.impl.util.avro.AvroMapWrapper Maven / Gradle / Ivy

There is a newer version: 0.17.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.pig.impl.util.avro;

import java.util.AbstractMap;
import java.util.Collection;
import java.util.Map;
import java.util.Set;

import org.apache.avro.util.Utf8;

import com.google.common.collect.Collections2;
import com.google.common.collect.Sets;
import com.google.common.base.Function;

/**
 * Wrapper for map objects, so we can translate UTF8 objects to
 * Strings if we encounter them.
 */
public final class AvroMapWrapper implements Map {

  /**
   * The map contained in the wrapper object.
   */
  private Map innerMap;
  private boolean isUtf8key;

  /**
   * Creates a new AvroMapWrapper object from the map object {@m}.
   * @param m The map to wrap.
   */
  public AvroMapWrapper(final Map m) {
    innerMap = m;
    if (m.keySet().size() > 0 && m.keySet().iterator().next() instanceof Utf8)
      isUtf8key = true;
    else
      isUtf8key = false;
  }

  @Override
  public int size() {
    return innerMap.size();
  }

  @Override
  public boolean isEmpty() {
    return innerMap.isEmpty();
  }

  @Override
  public boolean containsKey(final Object key) {
    return innerMap.containsKey(key);
  }

  @Override
  public boolean containsValue(final Object value) {
    return innerMap.containsValue(value);
  }

  @Override
  public Object get(final Object key) {
    Object v = null;
    if (isUtf8key && !(key instanceof Utf8)) {
      v = innerMap.get(new Utf8((String) key));
    } else {
      v = innerMap.get(key);
    }

    if (v instanceof Utf8) {
      return v.toString();
    } else {
      return v;
    }
  }

  @Override
  public Object put(final CharSequence key, final Object value) {
    return innerMap.put(key, value);
  }

  @Override
  public Object remove(final Object key) {
    return innerMap.remove(key);
  }

  @Override
  public void putAll(
      final Map m) {
    innerMap.putAll(m);
  }

  @Override
  public void clear() {
    innerMap.clear();
  }

  @Override
  public Set keySet() {
    return innerMap.keySet();
  }

  @SuppressWarnings({ "unchecked", "rawtypes" })
  @Override
  public Collection values() {
    return Collections2.transform(innerMap.values(),
        new Function() {
            @Override
            public Object apply(final Object v) {
              if (v instanceof Utf8) {
                return v.toString();
              } else {
                return v;
              }
            }
          }
        );
  }

  @Override
  public Set> entrySet() {
    Set> theSet =
        Sets.newHashSetWithExpectedSize(innerMap.size());
    for (java.util.Map.Entry e : innerMap.entrySet()) {
      CharSequence k = e.getKey();
      Object v = e.getValue();
      if (k instanceof Utf8) {
        k = k.toString();
      }
      if (v instanceof Utf8) {
        v = v.toString();
      }
      theSet.add(new AbstractMap.SimpleEntry(k, v));
    }

    return theSet;

  }

}