/*
 * Copyright (C) 2015 Google Inc.
 *
 * Licensed 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 com.google.cloud.dataflow.sdk.coders;

import com.google.api.client.util.Preconditions;
import com.google.cloud.dataflow.sdk.util.PropertyNames;
import com.google.cloud.dataflow.sdk.util.common.ElementByteSizeObserver;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * A {@code MapCoder} encodes {@code Map}s.
 *
 * @param <K> the type of the keys of the KVs being transcoded
 * @param <V> the type of the values of the KVs being transcoded
 */
@SuppressWarnings("serial")
public class MapCoder<K, V> extends MapCoderBase<Map<K, V>> {
  /**
   * Produces a MapCoder with the given keyCoder and valueCoder.
   */
  public static <K, V> MapCoder<K, V> of(
      Coder<K> keyCoder,
      Coder<V> valueCoder) {
    return new MapCoder<>(keyCoder, valueCoder);
  }

  @JsonCreator
  public static MapCoder<?, ?> of(
      @JsonProperty(PropertyNames.COMPONENT_ENCODINGS)
      List<Coder<?>> components) {
    Preconditions.checkArgument(components.size() == 2,
        "Expecting 2 components, got " + components.size());
    return of((Coder<?>) components.get(0), (Coder<?>) components.get(1));
  }

  /**
   * Returns the key and value for an arbitrary element of this map,
   * if it is non-empty, otherwise returns {@code null}.
   */
   public static <K, V> List<Object> getInstanceComponents(
       Map<K, V> exampleValue) {
     for (Map.Entry<K, V> entry : exampleValue.entrySet()) {
       return Arrays.asList(entry.getKey(), entry.getValue());
     }
     return null;
   }

  public Coder<K> getKeyCoder() {
    return keyCoder;
  }

  public Coder<V> getValueCoder() {
    return valueCoder;
  }

  /////////////////////////////////////////////////////////////////////////////

  Coder<K> keyCoder;
  Coder<V> valueCoder;

  MapCoder(Coder<K> keyCoder, Coder<V> valueCoder) {
    this.keyCoder = keyCoder;
    this.valueCoder = valueCoder;
  }

  @Override
  public void encode(
      Map<K, V> map,
      OutputStream outStream,
      Context context)
      throws IOException, CoderException  {
    if (map == null) {
      throw new CoderException("cannot encode a null Map");
    }
    DataOutputStream dataOutStream = new DataOutputStream(outStream);
    dataOutStream.writeInt(map.size());
    for (Entry<K, V> entry : map.entrySet()) {
      keyCoder.encode(entry.getKey(), outStream, context.nested());
      valueCoder.encode(entry.getValue(), outStream, context.nested());
    }
    dataOutStream.flush();
  }

  @Override
  public Map<K, V> decode(InputStream inStream, Context context)
      throws IOException, CoderException {
    DataInputStream dataInStream = new DataInputStream(inStream);
    int size = dataInStream.readInt();
    Map<K, V> retval = new HashMap<>();
    for (int i = 0; i < size; ++i) {
      K key = keyCoder.decode(inStream, context.nested());
      V value = valueCoder.decode(inStream, context.nested());
      retval.put(key, value);
    }
    return retval;
  }

  @Override
  public List<? extends Coder<?>> getCoderArguments() {
    return Arrays.asList(keyCoder, valueCoder);
  }

  /**
   * Not all maps have a deterministic encoding.
   *
   * <p>For example, HashMap comparison does not depend on element order, so
   * two HashMap instances may be equal but produce different encodings.
   */
  @Override
  public void verifyDeterministic() throws NonDeterministicException {
    throw new NonDeterministicException(this,
        "Ordering of entries in a Map may be non-deterministic.");
  }

  /**
   * Notifies ElementByteSizeObserver about the byte size of the
   * encoded value using this coder.
   */
  @Override
  public void registerByteSizeObserver(
      Map<K, V> map, ElementByteSizeObserver observer, Context context)
      throws Exception {
    observer.update(4L);
    for (Entry<K, V> entry : map.entrySet()) {
      keyCoder.registerByteSizeObserver(
          entry.getKey(), observer, context.nested());
      valueCoder.registerByteSizeObserver(
          entry.getValue(), observer, context.nested());
    }
  }
}

@@ -815,16 +815,55 @@
 bserver;
+%0Aimport com.google.common.collect.Maps;
 %0A%0Aimport
@@ -1143,34 +1143,8 @@
 ys;%0A
-import java.util.HashMap;%0A
 impo
@@ -3636,22 +3636,44 @@
 l = 
+Maps.
 new
- 
 HashMap
-%3C%3E(
+WithExpectedSize(size
 );%0A 

use the encoded size hint to allocate the Map