001/*
002 * Copyright (C) 2012 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package com.google.common.collect.testing.google;
017
018import static com.google.common.collect.testing.Helpers.copyToSet;
019import static com.google.common.collect.testing.Helpers.mapEntry;
020
021import com.google.common.annotations.GwtCompatible;
022import com.google.common.collect.SetMultimap;
023import com.google.common.collect.testing.SampleElements;
024import java.util.Collection;
025import java.util.List;
026import java.util.Map.Entry;
027
028/**
029 * A skeleton generator for a {@code SetMultimap} implementation.
030 *
031 * @author Louis Wasserman
032 */
033@GwtCompatible
034@ElementTypesAreNonnullByDefault
035public abstract class TestStringSetMultimapGenerator
036    implements TestSetMultimapGenerator<String, String> {
037
038  @Override
039  public SampleElements<Entry<String, String>> samples() {
040    return new SampleElements<>(
041        mapEntry("one", "January"),
042        mapEntry("two", "February"),
043        mapEntry("three", "March"),
044        mapEntry("four", "April"),
045        mapEntry("five", "May"));
046  }
047
048  @Override
049  public SampleElements<String> sampleKeys() {
050    return new SampleElements<>("one", "two", "three", "four", "five");
051  }
052
053  @Override
054  public SampleElements<String> sampleValues() {
055    return new SampleElements<>("January", "February", "March", "April", "May");
056  }
057
058  @Override
059  public Collection<String> createCollection(Iterable<? extends String> values) {
060    return copyToSet(values);
061  }
062
063  @Override
064  public final SetMultimap<String, String> create(Object... entries) {
065    @SuppressWarnings("unchecked")
066    Entry<String, String>[] array = (Entry<String, String>[]) new Entry<?, ?>[entries.length];
067    int i = 0;
068    for (Object o : entries) {
069      @SuppressWarnings("unchecked")
070      Entry<String, String> e = (Entry<String, String>) o;
071      array[i++] = e;
072    }
073    return create(array);
074  }
075
076  protected abstract SetMultimap<String, String> create(Entry<String, String>[] entries);
077
078  @Override
079  @SuppressWarnings("unchecked")
080  public final Entry<String, String>[] createArray(int length) {
081    return (Entry<String, String>[]) new Entry<?, ?>[length];
082  }
083
084  @Override
085  public final String[] createKeyArray(int length) {
086    return new String[length];
087  }
088
089  @Override
090  public final String[] createValueArray(int length) {
091    return new String[length];
092  }
093
094  /** Returns the original element list, unchanged. */
095  @Override
096  public Iterable<Entry<String, String>> order(List<Entry<String, String>> insertionOrder) {
097    return insertionOrder;
098  }
099}