• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.google;
18 
19 import static com.google.common.collect.testing.Helpers.mapEntry;
20 
21 import com.google.common.annotations.GwtCompatible;
22 import com.google.common.collect.ImmutableMap;
23 import com.google.common.collect.Maps;
24 import com.google.common.collect.Ordering;
25 import com.google.common.collect.testing.AnEnum;
26 import com.google.common.collect.testing.SampleElements;
27 import com.google.common.collect.testing.TestEnumMapGenerator;
28 import com.google.common.collect.testing.TestListGenerator;
29 import com.google.common.collect.testing.TestStringListGenerator;
30 import com.google.common.collect.testing.TestStringMapGenerator;
31 import com.google.common.collect.testing.TestUnhashableCollectionGenerator;
32 import com.google.common.collect.testing.UnhashableObject;
33 
34 import java.util.Collection;
35 import java.util.EnumMap;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.Map.Entry;
39 
40 /**
41  * Generators of different types of map and related collections, such as
42  * keys, entries and values.
43  *
44  * @author Hayward Chan
45  */
46 @GwtCompatible
47 public class MapGenerators {
48   public static class ImmutableMapGenerator
49       extends TestStringMapGenerator {
create(Entry<String, String>[] entries)50     @Override protected Map<String, String> create(Entry<String, String>[] entries) {
51       ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
52       for (Entry<String, String> entry : entries) {
53         builder.put(entry.getKey(), entry.getValue());
54       }
55       return builder.build();
56     }
57   }
58 
59   public static class ImmutableMapCopyOfGenerator
60       extends TestStringMapGenerator {
create(Entry<String, String>[] entries)61     @Override protected Map<String, String> create(Entry<String, String>[] entries) {
62       Map<String, String> builder = Maps.newLinkedHashMap();
63       for (Entry<String, String> entry : entries) {
64         builder.put(entry.getKey(), entry.getValue());
65       }
66       return ImmutableMap.copyOf(builder);
67     }
68   }
69 
70   public static class ImmutableMapUnhashableValuesGenerator
71       extends TestUnhashableCollectionGenerator<Collection<UnhashableObject>> {
72 
create( UnhashableObject[] elements)73     @Override public Collection<UnhashableObject> create(
74         UnhashableObject[] elements) {
75       ImmutableMap.Builder<Integer, UnhashableObject> builder = ImmutableMap.builder();
76       int key = 1;
77       for (UnhashableObject value : elements) {
78         builder.put(key++, value);
79       }
80       return builder.build().values();
81     }
82   }
83 
84   public static class ImmutableMapKeyListGenerator extends TestStringListGenerator {
85     @Override
create(String[] elements)86     public List<String> create(String[] elements) {
87       ImmutableMap.Builder<String, Integer> builder = ImmutableMap.builder();
88       for (int i = 0; i < elements.length; i++) {
89         builder.put(elements[i], i);
90       }
91       return builder.build().keySet().asList();
92     }
93   }
94 
95   public static class ImmutableMapValueListGenerator extends TestStringListGenerator {
96     @Override
create(String[] elements)97     public List<String> create(String[] elements) {
98       ImmutableMap.Builder<Integer, String> builder = ImmutableMap.builder();
99       for (int i = 0; i < elements.length; i++) {
100         builder.put(i, elements[i]);
101       }
102       return builder.build().values().asList();
103     }
104   }
105 
106   public static class ImmutableMapEntryListGenerator
107       implements TestListGenerator<Entry<String, Integer>> {
108 
109     @Override
samples()110     public SampleElements<Entry<String, Integer>> samples() {
111       return new SampleElements<Entry<String, Integer>>(
112           mapEntry("foo", 5),
113           mapEntry("bar", 3),
114           mapEntry("baz", 17),
115           mapEntry("quux", 1),
116           mapEntry("toaster", -2));
117     }
118 
119     @SuppressWarnings("unchecked")
120     @Override
createArray(int length)121     public Entry<String, Integer>[] createArray(int length) {
122       return new Entry[length];
123     }
124 
125     @Override
order(List<Entry<String, Integer>> insertionOrder)126     public Iterable<Entry<String, Integer>> order(List<Entry<String, Integer>> insertionOrder) {
127       return insertionOrder;
128     }
129 
130     @Override
create(Object... elements)131     public List<Entry<String, Integer>> create(Object... elements) {
132       ImmutableMap.Builder<String, Integer> builder = ImmutableMap.builder();
133       for (Object o : elements) {
134         @SuppressWarnings("unchecked")
135         Entry<String, Integer> entry = (Entry<String, Integer>) o;
136         builder.put(entry);
137       }
138       return builder.build().entrySet().asList();
139     }
140   }
141 
142   public static class ImmutableEnumMapGenerator extends TestEnumMapGenerator {
143     @Override
create(Entry<AnEnum, String>[] entries)144     protected Map<AnEnum, String> create(Entry<AnEnum, String>[] entries) {
145       Map<AnEnum, String> map = Maps.newHashMap();
146       for (Entry<AnEnum, String> entry : entries) {
147         // checkArgument(!map.containsKey(entry.getKey()));
148         map.put(entry.getKey(), entry.getValue());
149       }
150       return Maps.immutableEnumMap(map);
151     }
152   }
153 
154   public static class ImmutableMapCopyOfEnumMapGenerator extends TestEnumMapGenerator {
155     @Override
create(Entry<AnEnum, String>[] entries)156     protected Map<AnEnum, String> create(Entry<AnEnum, String>[] entries) {
157       EnumMap<AnEnum, String> map = new EnumMap<AnEnum, String>(AnEnum.class);
158       for (Entry<AnEnum, String> entry : entries) {
159         map.put(entry.getKey(), entry.getValue());
160       }
161       return ImmutableMap.copyOf(map);
162     }
163 
164     @Override
order(List<Entry<AnEnum, String>> insertionOrder)165     public Iterable<Entry<AnEnum, String>> order(List<Entry<AnEnum, String>> insertionOrder) {
166       return new Ordering<Entry<AnEnum, String>>() {
167 
168         @Override
169         public int compare(Entry<AnEnum, String> left, Entry<AnEnum, String> right) {
170           return left.getKey().compareTo(right.getKey());
171         }
172 
173       }.sortedCopy(insertionOrder);
174     }
175   }
176 
177   private static String toStringOrNull(Object o) {
178     return (o == null) ? null : o.toString();
179   }
180 }
181