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