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