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;
025
026/**
027 * Implementation helper for {@link TestMapGenerator} for use with maps of strings.
028 *
029 * @author Chris Povirk
030 * @author Jared Levy
031 * @author George van den Driessche
032 */
033@GwtCompatible
034@ElementTypesAreNonnullByDefault
035public abstract class TestStringMapGenerator implements TestMapGenerator<String, String> {
036
037  @Override
038  public SampleElements<Entry<String, String>> samples() {
039    return new SampleElements<>(
040        mapEntry("one", "January"),
041        mapEntry("two", "February"),
042        mapEntry("three", "March"),
043        mapEntry("four", "April"),
044        mapEntry("five", "May"));
045  }
046
047  @Override
048  public Map<String, String> create(Object... entries) {
049    @SuppressWarnings("unchecked")
050    Entry<String, String>[] array = (Entry<String, String>[]) new Entry<?, ?>[entries.length];
051    int i = 0;
052    for (Object o : entries) {
053      @SuppressWarnings("unchecked")
054      Entry<String, String> e = (Entry<String, String>) o;
055      array[i++] = e;
056    }
057    return create(array);
058  }
059
060  protected abstract Map<String, String> create(Entry<String, String>[] entries);
061
062  @Override
063  @SuppressWarnings("unchecked")
064  public final Entry<String, String>[] createArray(int length) {
065    return (Entry<String, String>[]) new Entry<?, ?>[length];
066  }
067
068  @Override
069  public final String[] createKeyArray(int length) {
070    return new String[length];
071  }
072
073  @Override
074  public final String[] createValueArray(int length) {
075    return new String[length];
076  }
077
078  /** Returns the original element list, unchanged. */
079  @Override
080  public Iterable<Entry<String, String>> order(List<Entry<String, String>> insertionOrder) {
081    return insertionOrder;
082  }
083}