View Javadoc
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  
17  package com.google.common.collect.testing;
18  
19  import static com.google.common.collect.testing.Helpers.orderEntriesByKey;
20  
21  import com.google.common.annotations.GwtCompatible;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.Map.Entry;
25  
26  /**
27   * Implementation helper for {@link TestMapGenerator} for use with enum maps.
28   *
29   * @author Kevin Bourrillion
30   */
31  @GwtCompatible
32  public abstract class TestEnumMapGenerator implements TestMapGenerator<AnEnum, String> {
33  
34    @Override
35    public SampleElements<Entry<AnEnum, String>> samples() {
36      return new SampleElements<>(
37          Helpers.mapEntry(AnEnum.A, "January"),
38          Helpers.mapEntry(AnEnum.B, "February"),
39          Helpers.mapEntry(AnEnum.C, "March"),
40          Helpers.mapEntry(AnEnum.D, "April"),
41          Helpers.mapEntry(AnEnum.E, "May"));
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(Entry<AnEnum, String>[] entries);
58  
59    @Override
60    @SuppressWarnings("unchecked")
61    public final Entry<AnEnum, String>[] createArray(int length) {
62      return new Entry[length];
63    }
64  
65    @Override
66    public final AnEnum[] createKeyArray(int length) {
67      return new AnEnum[length];
68    }
69  
70    @Override
71    public final String[] createValueArray(int length) {
72      return new String[length];
73    }
74  
75    /** Returns the elements sorted in natural order. */
76    @Override
77    public Iterable<Entry<AnEnum, String>> order(List<Entry<AnEnum, String>> insertionOrder) {
78      return orderEntriesByKey(insertionOrder);
79    }
80  }