package com.metamx.common.collect;


import com.google.common.base.Preconditions;
import com.google.common.collect.Iterators;

import java.lang.Iterable;import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class Utils
{
  public static <K, V> Map<K, V> zipMap(K[] keys, V[] values) {
    return zipMap(keys, values, false);
  }
  
  public static <K, V> Map<K, V> zipMap(K[] keys, V[] values, boolean allowUnusedKeys)
  {
    Preconditions.checkArgument(values.length <= keys.length,
                                "number of values[%d] exceeds number of keys[%d]",
                                values.length, keys.length);

    Preconditions.checkArgument(allowUnusedKeys || values.length == keys.length,
                                "number of values[%d] less than number of keys[%d]",
                                values.length, keys.length);


    Map<K, V> retVal = new HashMap<K, V>();

    for(int i = 0; i < values.length; ++i) retVal.put(keys[i], values[i]);

    return retVal;
  }

  public static <K, V> Map<K, V> zipMap(Iterable<K> keys, Iterable<V> values) {
    return zipMap(keys, values, false);
  }
  
  public static <K, V> Map<K, V> zipMap(Iterable<K> keys, Iterable<V> values, boolean allowUnusedKeys)
  {
    Map<K, V> retVal = new HashMap<K, V>();

    Iterator<K> keysIter = keys.iterator();
    Iterator<V> valsIter = values.iterator();

    while (keysIter.hasNext()) {
      final K key = keysIter.next();

      Preconditions.checkArgument(valsIter.hasNext() || allowUnusedKeys,
                                  "number of values[%d] less than number of keys",
                                  retVal.size());
      
      if(valsIter.hasNext()) retVal.put(key, valsIter.next());
    }

    Preconditions.checkArgument(!valsIter.hasNext(),
                                "number of values[%d] exceeds number of keys[%d]",
                                retVal.size() + Iterators.size(valsIter), retVal.size());

    return retVal;
  }
}

@@ -550,33 +550,33 @@
 mber of values%5B%25
-d
+s
 %5D exceeds number
@@ -577,33 +577,33 @@
 number of keys%5B%25
-d
+s
 %5D%22,%0A            
@@ -776,33 +776,33 @@
 mber of values%5B%25
-d
+s
 %5D less than numb
@@ -805,33 +805,33 @@
 number of keys%5B%25
-d
+s
 %5D%22,%0A            
@@ -1593,17 +1593,17 @@
 values%5B%25
-d
+s
 %5D less t
@@ -1854,17 +1854,17 @@
 values%5B%25
-d
+s
 %5D exceed
@@ -1881,17 +1881,17 @@
 f keys%5B%25
-d
+s
 %5D%22,%0A    

correct argument checking logic in Utils.zipMap