Added Maps2.fromKeys

This commit is contained in:
Andrew Phillips 2011-07-07 21:34:30 -04:00
parent b889188b86
commit 5b82761396
2 changed files with 55 additions and 2 deletions

View File

@ -26,16 +26,17 @@ import static com.google.common.collect.Maps.filterKeys;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import com.google.common.base.Function;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.collect.ImmutableMap.Builder;
/**
* General utilities used in jclouds code for {@link Map}s.
* General utilities used in jclouds code for {@link Map Maps}.
*
* @author Adrian Cole
*/
@ -120,5 +121,25 @@ public class Maps2 {
return toReturn;
}
}
/**
* Constructs a map with the given keys where values are generated by the given function.
* Supports duplicate and {@code null} values, but {@code null} keys are <em>not</em> allowed.
*
* @param <K> the type of the keys
* @param <V> the type of the values
* @param keys the keys to be included in the map. Keys must be non-<code>null</code>
* @param valueFunction the function that produces values for the keys
* @return a map containing the keys from the given set with values which are generated from
* the keys
* @see Maps#uniqueIndex(Iterable, Function)
*/
public static <K, V> Map<K, V> fromKeys(Set<K> keys, Function<? super K, V> valueFunction) {
Map<K, V> result = Maps.newHashMapWithExpectedSize(keys.size());
for (K key : keys) {
result.put(checkNotNull(key), valueFunction.apply(key));
}
return result;
}
}

View File

@ -18,7 +18,11 @@
*/
package org.jclouds.util;
import static com.google.common.base.Functions.constant;
import static com.google.common.base.Functions.identity;
import static com.google.common.collect.Sets.newHashSet;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import java.util.Map;
@ -26,6 +30,8 @@ import org.testng.annotations.Test;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
/**
* @author Adrian Cole
@ -54,4 +60,30 @@ public class Maps2Test {
}), ImmutableMap.of("foo", "bar"));
}
public void testFromKeysEmptyKeys() {
assertTrue(Maps2.fromKeys(ImmutableSet.of(), identity()).isEmpty(),
"Expected returned map to be empty");
}
@Test(expectedExceptions = { NullPointerException.class })
public void testFromKeysNullKey() {
Maps2.fromKeys(newHashSet((Object) null), constant("const"));
}
public void testFromKeys() {
// ImmutableMap doesn't support null values
Map<String, String> expected = Maps.newHashMap();
expected.put("foo", "foo");
expected.put("bar", "foo");
expected.put("baz", null);
assertEquals(Maps2.fromKeys(ImmutableSet.of("foo", "bar", "baz"),
new Function<String, String>() {
@Override
public String apply(String input) {
return (input.equals("baz") ? null : "foo");
}
}), expected);
}
}