View Javadoc
1   /*
2    * Copyright (C) 2008 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 com.google.common.annotations.GwtCompatible;
20  import java.util.List;
21  import java.util.Map;
22  import java.util.Map.Entry;
23  
24  /**
25   * Implementation helper for {@link TestMapGenerator} for use with maps of
26   * strings.
27   *
28   * @author Chris Povirk
29   * @author Jared Levy
30   * @author George van den Driessche
31   */
32  @GwtCompatible
33  public abstract class TestStringMapGenerator implements TestMapGenerator<String, String> {
34  
35    @Override
36    public SampleElements<Map.Entry<String, String>> samples() {
37      return new SampleElements<>(
38          Helpers.mapEntry("one", "January"),
39          Helpers.mapEntry("two", "February"),
40          Helpers.mapEntry("three", "March"),
41          Helpers.mapEntry("four", "April"),
42          Helpers.mapEntry("five", "May"));
43    }
44  
45    @Override
46    public Map<String, String> create(Object... entries) {
47      @SuppressWarnings("unchecked")
48      Entry<String, String>[] array = new Entry[entries.length];
49      int i = 0;
50      for (Object o : entries) {
51        @SuppressWarnings("unchecked")
52        Entry<String, String> e = (Entry<String, String>) o;
53        array[i++] = e;
54      }
55      return create(array);
56    }
57  
58    protected abstract Map<String, String> create(Entry<String, String>[] entries);
59  
60    @Override
61    @SuppressWarnings("unchecked")
62    public final Entry<String, String>[] createArray(int length) {
63      return new Entry[length];
64    }
65  
66    @Override
67    public final String[] createKeyArray(int length) {
68      return new String[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<String, String>> order(List<Entry<String, String>> insertionOrder) {
79      return insertionOrder;
80    }
81  }