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