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