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 java.util.List;
20import java.util.Map;
21import java.util.Map.Entry;
22
23/**
24 * Implementation helper for {@link TestMapGenerator} for use with enum maps.
25 *
26 * <p>This class is GWT compatible.
27 *
28 * @author Kevin Bourrillion
29 */
30public abstract class TestEnumMapGenerator
31    implements TestMapGenerator<AnEnum, String> {
32
33  @Override
34  public SampleElements<Entry<AnEnum, String>> samples() {
35    return new SampleElements<Entry<AnEnum, String>>(
36        Helpers.mapEntry(AnEnum.A, "January"),
37        Helpers.mapEntry(AnEnum.B, "February"),
38        Helpers.mapEntry(AnEnum.C, "March"),
39        Helpers.mapEntry(AnEnum.D, "April"),
40        Helpers.mapEntry(AnEnum.E, "May")
41    );
42  }
43
44  @Override
45  public final Map<AnEnum, String> create(Object... entries) {
46    @SuppressWarnings("unchecked")
47    Entry<AnEnum, String>[] array = new Entry[entries.length];
48    int i = 0;
49    for (Object o : entries) {
50      @SuppressWarnings("unchecked")
51      Entry<AnEnum, String> e = (Entry<AnEnum, String>) o;
52      array[i++] = e;
53    }
54    return create(array);
55  }
56
57  protected abstract Map<AnEnum, String> create(
58      Entry<AnEnum, String>[] entries);
59
60  @Override
61  @SuppressWarnings("unchecked")
62  public final Entry<AnEnum, String>[] createArray(int length) {
63    return new Entry[length];
64  }
65
66  @Override
67  public final AnEnum[] createKeyArray(int length) {
68    return new AnEnum[length];
69  }
70
71  @Override
72  public final String[] createValueArray(int length) {
73    return new String[length];
74  }
75
76  /** Returns the original element list, unchanged. */
77  @Override
78  public Iterable<Entry<AnEnum, String>> order(
79      List<Entry<AnEnum, String>> insertionOrder) {
80    return insertionOrder;
81  }
82}