TestEnumMapGenerator.java revision 7dd252788645e940eada959bdde927426e2531c9
1/*
2 * Copyright (C) 2009 The Guava Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.google.common.collect.testing;
18
19import static com.google.common.collect.testing.Helpers.orderEntriesByKey;
20
21import com.google.common.annotations.GwtCompatible;
22
23import java.util.List;
24import java.util.Map;
25import java.util.Map.Entry;
26
27/**
28 * Implementation helper for {@link TestMapGenerator} for use with enum maps.
29 *
30 * <p>This class is GWT compatible.
31 *
32 * @author Kevin Bourrillion
33 */
34@GwtCompatible
35public abstract class TestEnumMapGenerator
36    implements TestMapGenerator<AnEnum, String> {
37
38  @Override
39  public SampleElements<Entry<AnEnum, String>> samples() {
40    return new SampleElements<Entry<AnEnum, String>>(
41        Helpers.mapEntry(AnEnum.A, "January"),
42        Helpers.mapEntry(AnEnum.B, "February"),
43        Helpers.mapEntry(AnEnum.C, "March"),
44        Helpers.mapEntry(AnEnum.D, "April"),
45        Helpers.mapEntry(AnEnum.E, "May")
46    );
47  }
48
49  @Override
50  public final Map<AnEnum, String> create(Object... entries) {
51    @SuppressWarnings("unchecked")
52    Entry<AnEnum, String>[] array = new Entry[entries.length];
53    int i = 0;
54    for (Object o : entries) {
55      @SuppressWarnings("unchecked")
56      Entry<AnEnum, String> e = (Entry<AnEnum, String>) o;
57      array[i++] = e;
58    }
59    return create(array);
60  }
61
62  protected abstract Map<AnEnum, String> create(
63      Entry<AnEnum, String>[] entries);
64
65  @Override
66  @SuppressWarnings("unchecked")
67  public final Entry<AnEnum, String>[] createArray(int length) {
68    return new Entry[length];
69  }
70
71  @Override
72  public final AnEnum[] createKeyArray(int length) {
73    return new AnEnum[length];
74  }
75
76  @Override
77  public final String[] createValueArray(int length) {
78    return new String[length];
79  }
80
81  /** Returns the elements sorted in natural order. */
82  @Override
83  public Iterable<Entry<AnEnum, String>> order(
84      List<Entry<AnEnum, String>> insertionOrder) {
85    return orderEntriesByKey(insertionOrder);
86  }
87}
88