/*
 * 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.values;

import com.google.cloud.dataflow.sdk.transforms.SerializableComparator;

import java.io.Serializable;

/**
 * An immutable key/value pair.
 *
 * <p> Various
 * {@link com.google.cloud.dataflow.sdk.transforms.PTransform}s like
 * {@link com.google.cloud.dataflow.sdk.transforms.GroupByKey} and
 * {@link com.google.cloud.dataflow.sdk.transforms.Combine#perKey}
 * work on {@link PCollection}s of KVs.
 *
 * @param <K> the type of the key
 * @param <V> the type of the value
 */
public class KV<K, V> implements Serializable {
  private static final long serialVersionUID = 0;

  /** Returns a KV with the given key and value. */
  public static <K, V> KV<K, V> of(K key, V value) {
    return new KV<>(key, value);
  }

  /** Returns the key of this KV. */
  public K getKey() {
    return key;
  }

  /** Returns the value of this KV. */
  public V getValue() {
    return value;
  }


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

  final K key;
  final V value;

  private KV(K key, V value) {
    this.key = key;
    this.value = value;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o instanceof KV) {
      KV<?, ?> that = (KV<?, ?>) o;
      return (this.key == null ? that.key == null
              : this.key.equals(that.key))
          && (this.value == null ? that.value == null
              : this.value.equals(that.value));
    }
    return false;
  }

  /** Orders the {@link KV} by the key. A null key is less than any non-null key. */
  @SuppressWarnings("serial")
  public static class OrderByKey<K extends Comparable<? super K>, V> implements
      SerializableComparator<KV<K, V>> {
    @Override
    public int compare(KV<K, V> a, KV<K, V> b) {
      if (a.key == null) {
        return b.key == null ? 0 : -1;
      } else if (b.key == null) {
        return 1;
      } else {
        return a.key.compareTo(b.key);
      }
    }
  }

  /** Orders the {@link KV} by the value. A null value is less than any non-null value. */
  @SuppressWarnings("serial")
  public static class OrderByValue<K, V extends Comparable<? super V>>
      implements SerializableComparator<KV<K, V>> {
    @Override
    public int compare(KV<K, V> a, KV<K, V> b) {
      if (a.value == null) {
        return b.value == null ? 0 : -1;
      } else if (b.value == null) {
        return 1;
      } else {
        return a.value.compareTo(b.value);
      }
    }
  }

  @Override
  public int hashCode() {
    return getClass().hashCode()
        + (key == null ? 0 : key.hashCode())
        + (value == null ? 0 : value.hashCode());
  }

  @Override
  public String toString() {
    return "KV(" + key + ", " + value + ")";
  }
}

@@ -712,36 +712,130 @@
 or;%0A
-%0Aimport java.io.Serializable
+import com.google.common.base.MoreObjects;%0A%0Aimport java.io.Serializable;%0Aimport java.util.Arrays;%0Aimport java.util.Objects
 ;%0A%0A/
@@ -1854,16 +1854,20 @@
 Object o
+ther
 ) %7B%0A    
@@ -1879,16 +1879,20 @@
 his == o
+ther
 ) %7B%0A    
@@ -1920,17 +1920,23 @@
     if (
-o
+!(other
  instanc
@@ -1942,21 +1942,46 @@
 ceof KV)
+)
  %7B%0A  
+    return false;%0A    %7D%0A
     KV%3C?
@@ -1985,20 +1985,23 @@
 V%3C?, ?%3E 
+o
 th
-at
+erKv
  = (KV%3C?
@@ -2011,94 +2011,123 @@
 %3E) o
+ther
 ;%0A    
-  return (this.key == null ? that.key == null%0A              : this.key.e
+// Arrays are very common as values and keys, so deepEquals is mandatory%0A    return Objects.deepE
 quals(th
 at.k
@@ -2126,107 +2126,58 @@
 s(th
-at
+is
 .key
-))%0A          && (this.value == null ? that.value == null%0A              : this.value.e
+, this.key)%0A        && Objects.deepE
 quals(th
 at.v
@@ -2176,42 +2176,32 @@
 s(th
-at
+is
 .value
-));%0A    %7D%0A    return false
+, otherKv.value)
 ;%0A  
@@ -3247,221 +3247,290 @@
     
-return getClass().hashCode()%0A        + (key == null ? 0 : key.hashCode())%0A        + (value == null ? 0 : value.hashCode());%0A  %7D%0A%0A  @Override%0A  public String toString() %7B%0A    return %22KV(%22 + key + %22, %22 + value + %22)%22
+// Objects.deepEquals requires Arrays.deepHashCode for correctness%0A    return Arrays.deepHashCode(new Object%5B%5D%7Bkey, value%7D);%0A  %7D%0A%0A  @Override%0A  public String toString() %7B%0A    return MoreObjects.toStringHelper(this)%0A        .addValue(key)%0A        .addValue(value)%0A        .toString()
 ;%0A  

Fix basic Object overrides for KV