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