com.google.common.collect.testing.google.MapGenerators Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of guava-testlib-jdk5 Show documentation
Show all versions of guava-testlib-jdk5 Show documentation
Guava testlib is a set of java classes used for more convenient
unit testing - particularly to assist the tests for Guava itself.
The newest version!
/*
* Copyright (C) 2009 The Guava Authors
*
* 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.common.collect.testing.google;
import static com.google.common.collect.testing.Helpers.mapEntry;
import com.google.common.annotations.GwtCompatible;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.common.collect.Ordering;
import com.google.common.collect.testing.AnEnum;
import com.google.common.collect.testing.SampleElements;
import com.google.common.collect.testing.TestEnumMapGenerator;
import com.google.common.collect.testing.TestListGenerator;
import com.google.common.collect.testing.TestStringListGenerator;
import com.google.common.collect.testing.TestStringMapGenerator;
import com.google.common.collect.testing.TestUnhashableCollectionGenerator;
import com.google.common.collect.testing.UnhashableObject;
import java.util.Collection;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
/**
* Generators of different types of map and related collections, such as
* keys, entries and values.
*
* @author Hayward Chan
*/
@GwtCompatible
public class MapGenerators {
public static class ImmutableMapGenerator
extends TestStringMapGenerator {
@Override protected Map create(Entry[] entries) {
ImmutableMap.Builder builder = ImmutableMap.builder();
for (Entry entry : entries) {
builder.put(entry.getKey(), entry.getValue());
}
return builder.build();
}
}
public static class ImmutableMapCopyOfGenerator
extends TestStringMapGenerator {
@Override protected Map create(Entry[] entries) {
Map builder = Maps.newLinkedHashMap();
for (Entry entry : entries) {
builder.put(entry.getKey(), entry.getValue());
}
return ImmutableMap.copyOf(builder);
}
}
public static class ImmutableMapUnhashableValuesGenerator
extends TestUnhashableCollectionGenerator> {
@Override public Collection create(
UnhashableObject[] elements) {
ImmutableMap.Builder builder = ImmutableMap.builder();
int key = 1;
for (UnhashableObject value : elements) {
builder.put(key++, value);
}
return builder.build().values();
}
}
public static class ImmutableMapKeyListGenerator extends TestStringListGenerator {
@Override
public List create(String[] elements) {
ImmutableMap.Builder builder = ImmutableMap.builder();
for (int i = 0; i < elements.length; i++) {
builder.put(elements[i], i);
}
return builder.build().keySet().asList();
}
}
public static class ImmutableMapValueListGenerator extends TestStringListGenerator {
@Override
public List create(String[] elements) {
ImmutableMap.Builder builder = ImmutableMap.builder();
for (int i = 0; i < elements.length; i++) {
builder.put(i, elements[i]);
}
return builder.build().values().asList();
}
}
public static class ImmutableMapEntryListGenerator
implements TestListGenerator> {
@Override
public SampleElements> samples() {
return new SampleElements>(
mapEntry("foo", 5),
mapEntry("bar", 3),
mapEntry("baz", 17),
mapEntry("quux", 1),
mapEntry("toaster", -2));
}
@SuppressWarnings("unchecked")
@Override
public Entry[] createArray(int length) {
return new Entry[length];
}
@Override
public Iterable> order(List> insertionOrder) {
return insertionOrder;
}
@Override
public List> create(Object... elements) {
ImmutableMap.Builder builder = ImmutableMap.builder();
for (Object o : elements) {
@SuppressWarnings("unchecked")
Entry entry = (Entry) o;
builder.put(entry);
}
return builder.build().entrySet().asList();
}
}
public static class ImmutableEnumMapGenerator extends TestEnumMapGenerator {
@Override
protected Map create(Entry[] entries) {
Map map = Maps.newHashMap();
for (Entry entry : entries) {
// checkArgument(!map.containsKey(entry.getKey()));
map.put(entry.getKey(), entry.getValue());
}
return Maps.immutableEnumMap(map);
}
}
public static class ImmutableMapCopyOfEnumMapGenerator extends TestEnumMapGenerator {
@Override
protected Map create(Entry[] entries) {
EnumMap map = new EnumMap(AnEnum.class);
for (Entry entry : entries) {
map.put(entry.getKey(), entry.getValue());
}
return ImmutableMap.copyOf(map);
}
@Override
public Iterable> order(List> insertionOrder) {
return new Ordering>() {
@Override
public int compare(Entry left, Entry right) {
return left.getKey().compareTo(right.getKey());
}
}.sortedCopy(insertionOrder);
}
}
private static String toStringOrNull(Object o) {
return (o == null) ? null : o.toString();
}
}