com.google.common.collect.testing.google.DerivedGoogleCollectionGenerators Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of guava-testlib Show documentation
Show all versions of guava-testlib Show documentation
Guava testlib is a set of java classes used for more convenient
unit testing - particularly to assist the tests for Guava itself.
/*
* Copyright (C) 2012 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 com.google.common.annotations.GwtCompatible;
import com.google.common.collect.BiMap;
import com.google.common.collect.testing.DerivedGenerator;
import com.google.common.collect.testing.Helpers;
import com.google.common.collect.testing.OneSizeTestContainerGenerator;
import com.google.common.collect.testing.SampleElements;
import com.google.common.collect.testing.TestMapGenerator;
import com.google.common.collect.testing.TestSetGenerator;
import com.google.common.collect.testing.TestSubjectGenerator;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.checkerframework.checker.nullness.qual.Nullable;
/**
* Derived suite generators for Guava collection interfaces, split out of the suite builders so that
* they are available to GWT.
*
* @author Louis Wasserman
*/
@GwtCompatible
@ElementTypesAreNonnullByDefault
public final class DerivedGoogleCollectionGenerators {
public static class MapGenerator
implements TestMapGenerator, DerivedGenerator {
private final OneSizeTestContainerGenerator, Entry> generator;
public MapGenerator(
OneSizeTestContainerGenerator, Entry> oneSizeTestContainerGenerator) {
this.generator = oneSizeTestContainerGenerator;
}
@Override
public SampleElements> samples() {
return generator.samples();
}
@Override
public Map create(Object... elements) {
return generator.create(elements);
}
@Override
public Entry[] createArray(int length) {
return generator.createArray(length);
}
@Override
public Iterable> order(List> insertionOrder) {
return generator.order(insertionOrder);
}
@SuppressWarnings("unchecked")
@Override
public K[] createKeyArray(int length) {
return (K[]) new Object[length];
}
@SuppressWarnings("unchecked")
@Override
public V[] createValueArray(int length) {
return (V[]) new Object[length];
}
@Override
public TestSubjectGenerator> getInnerGenerator() {
return generator;
}
}
public static class InverseBiMapGenerator
implements TestBiMapGenerator, DerivedGenerator {
private final OneSizeTestContainerGenerator, Entry> generator;
public InverseBiMapGenerator(
OneSizeTestContainerGenerator, Entry> oneSizeTestContainerGenerator) {
this.generator = oneSizeTestContainerGenerator;
}
@Override
public SampleElements> samples() {
SampleElements> samples = generator.samples();
return new SampleElements<>(
reverse(samples.e0()),
reverse(samples.e1()),
reverse(samples.e2()),
reverse(samples.e3()),
reverse(samples.e4()));
}
private Entry reverse(Entry entry) {
return Helpers.mapEntry(entry.getValue(), entry.getKey());
}
@SuppressWarnings("unchecked")
@Override
public BiMap create(Object... elements) {
Entry, ?>[] entries = new Entry, ?>[elements.length];
for (int i = 0; i < elements.length; i++) {
entries[i] = reverse((Entry) elements[i]);
}
return generator.create((Object[]) entries).inverse();
}
@SuppressWarnings("unchecked")
@Override
public Entry[] createArray(int length) {
return (Entry[]) new Entry, ?>[length];
}
@Override
public Iterable> order(List> insertionOrder) {
return insertionOrder;
}
@SuppressWarnings("unchecked")
@Override
public V[] createKeyArray(int length) {
return (V[]) new Object[length];
}
@SuppressWarnings("unchecked")
@Override
public K[] createValueArray(int length) {
return (K[]) new Object[length];
}
@Override
public TestSubjectGenerator> getInnerGenerator() {
return generator;
}
}
public static class BiMapValueSetGenerator
implements TestSetGenerator, DerivedGenerator {
private final OneSizeTestContainerGenerator, Entry> mapGenerator;
private final SampleElements samples;
public BiMapValueSetGenerator(
OneSizeTestContainerGenerator, Entry> mapGenerator) {
this.mapGenerator = mapGenerator;
SampleElements> mapSamples = this.mapGenerator.samples();
this.samples =
new SampleElements<>(
mapSamples.e0().getValue(),
mapSamples.e1().getValue(),
mapSamples.e2().getValue(),
mapSamples.e3().getValue(),
mapSamples.e4().getValue());
}
@Override
public SampleElements samples() {
return samples;
}
@Override
public Set create(Object... elements) {
@SuppressWarnings("unchecked")
V[] valuesArray = (V[]) elements;
// Start with a suitably shaped collection of entries
Collection> originalEntries = mapGenerator.getSampleElements(elements.length);
// Create a copy of that, with the desired value for each value
Collection> entries = new ArrayList<>(elements.length);
int i = 0;
for (Entry entry : originalEntries) {
entries.add(Helpers.mapEntry(entry.getKey(), valuesArray[i++]));
}
return mapGenerator.create(entries.toArray()).values();
}
@Override
public V[] createArray(int length) {
V[] vs =
((TestBiMapGenerator) mapGenerator.getInnerGenerator()).createValueArray(length);
return vs;
}
@Override
public Iterable order(List insertionOrder) {
return insertionOrder;
}
@Override
public TestSubjectGenerator> getInnerGenerator() {
return mapGenerator;
}
}
private DerivedGoogleCollectionGenerators() {}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy