001/*
002 * Copyright (C) 2009 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;
020import static com.google.common.collect.testing.Helpers.orderEntriesByKey;
021
022import com.google.common.annotations.GwtCompatible;
023import java.util.List;
024import java.util.Map;
025import java.util.Map.Entry;
026
027/**
028 * Implementation helper for {@link TestMapGenerator} for use with enum maps.
029 *
030 * @author Kevin Bourrillion
031 */
032@GwtCompatible
033@ElementTypesAreNonnullByDefault
034public abstract class TestEnumMapGenerator implements TestMapGenerator<AnEnum, String> {
035
036  @Override
037  public SampleElements<Entry<AnEnum, String>> samples() {
038    return new SampleElements<>(
039        mapEntry(AnEnum.A, "January"),
040        mapEntry(AnEnum.B, "February"),
041        mapEntry(AnEnum.C, "March"),
042        mapEntry(AnEnum.D, "April"),
043        mapEntry(AnEnum.E, "May"));
044  }
045
046  @Override
047  public final Map<AnEnum, String> create(Object... entries) {
048    @SuppressWarnings("unchecked")
049    Entry<AnEnum, String>[] array = (Entry<AnEnum, String>[]) new Entry<?, ?>[entries.length];
050    int i = 0;
051    for (Object o : entries) {
052      @SuppressWarnings("unchecked")
053      Entry<AnEnum, String> e = (Entry<AnEnum, String>) o;
054      array[i++] = e;
055    }
056    return create(array);
057  }
058
059  protected abstract Map<AnEnum, String> create(Entry<AnEnum, String>[] entries);
060
061  @Override
062  @SuppressWarnings("unchecked")
063  public final Entry<AnEnum, String>[] createArray(int length) {
064    return (Entry<AnEnum, String>[]) new Entry<?, ?>[length];
065  }
066
067  @Override
068  public final AnEnum[] createKeyArray(int length) {
069    return new AnEnum[length];
070  }
071
072  @Override
073  public final String[] createValueArray(int length) {
074    return new String[length];
075  }
076
077  /** Returns the elements sorted in natural order. */
078  @Override
079  public Iterable<Entry<AnEnum, String>> order(List<Entry<AnEnum, String>> insertionOrder) {
080    return orderEntriesByKey(insertionOrder);
081  }
082}