001/*
002 * Copyright (C) 2008 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;
018
019import static com.google.common.collect.testing.Helpers.mapEntry;
020
021import com.google.common.annotations.GwtCompatible;
022import java.util.List;
023import java.util.Map;
024import java.util.Map.Entry;
025import org.jspecify.annotations.NullMarked;
026
027/**
028 * Implementation helper for {@link TestMapGenerator} for use with maps of strings.
029 *
030 * @author Chris Povirk
031 * @author Jared Levy
032 * @author George van den Driessche
033 */
034@GwtCompatible
035@NullMarked
036public abstract class TestStringMapGenerator implements TestMapGenerator<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 Map<String, String> create(Object... entries) {
050    @SuppressWarnings("unchecked")
051    Entry<String, String>[] array = (Entry<String, String>[]) new Entry<?, ?>[entries.length];
052    int i = 0;
053    for (Object o : entries) {
054      @SuppressWarnings("unchecked")
055      Entry<String, String> e = (Entry<String, String>) o;
056      array[i++] = e;
057    }
058    return create(array);
059  }
060
061  protected abstract Map<String, String> create(Entry<String, String>[] entries);
062
063  @Override
064  @SuppressWarnings("unchecked")
065  public final Entry<String, String>[] createArray(int length) {
066    return (Entry<String, String>[]) new Entry<?, ?>[length];
067  }
068
069  @Override
070  public final String[] createKeyArray(int length) {
071    return new String[length];
072  }
073
074  @Override
075  public final String[] createValueArray(int length) {
076    return new String[length];
077  }
078
079  /** Returns the original element list, unchanged. */
080  @Override
081  public Iterable<Entry<String, String>> order(List<Entry<String, String>> insertionOrder) {
082    return insertionOrder;
083  }
084}