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;
027import org.jspecify.annotations.NullMarked;
028
029/**
030 * A skeleton generator for a {@code SetMultimap} implementation.
031 *
032 * @author Louis Wasserman
033 */
034@GwtCompatible
035@NullMarked
036public abstract class TestStringSetMultimapGenerator
037    implements TestSetMultimapGenerator<String, String> {
038
039  @Override
040  public SampleElements<Entry<String, String>> samples() {
041    return new SampleElements<>(
042        mapEntry("one", "January"),
043        mapEntry("two", "February"),
044        mapEntry("three", "March"),
045        mapEntry("four", "April"),
046        mapEntry("five", "May"));
047  }
048
049  @Override
050  public SampleElements<String> sampleKeys() {
051    return new SampleElements<>("one", "two", "three", "four", "five");
052  }
053
054  @Override
055  public SampleElements<String> sampleValues() {
056    return new SampleElements<>("January", "February", "March", "April", "May");
057  }
058
059  @Override
060  public Collection<String> createCollection(Iterable<? extends String> values) {
061    return copyToSet(values);
062  }
063
064  @Override
065  public final SetMultimap<String, String> create(Object... entries) {
066    @SuppressWarnings("unchecked")
067    Entry<String, String>[] array = (Entry<String, String>[]) new Entry<?, ?>[entries.length];
068    int i = 0;
069    for (Object o : entries) {
070      @SuppressWarnings("unchecked")
071      Entry<String, String> e = (Entry<String, String>) o;
072      array[i++] = e;
073    }
074    return create(array);
075  }
076
077  protected abstract SetMultimap<String, String> create(Entry<String, String>[] entries);
078
079  @Override
080  @SuppressWarnings("unchecked")
081  public final Entry<String, String>[] createArray(int length) {
082    return (Entry<String, String>[]) new Entry<?, ?>[length];
083  }
084
085  @Override
086  public final String[] createKeyArray(int length) {
087    return new String[length];
088  }
089
090  @Override
091  public final String[] createValueArray(int length) {
092    return new String[length];
093  }
094
095  /** Returns the original element list, unchanged. */
096  @Override
097  public Iterable<Entry<String, String>> order(List<Entry<String, String>> insertionOrder) {
098    return insertionOrder;
099  }
100}