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.base.Preconditions.checkNotNull; 20 21import com.google.common.annotations.GwtCompatible; 22import com.google.common.collect.ImmutableMap; 23import com.google.common.collect.ImmutableMap.Builder; 24import com.google.common.collect.testing.SampleElements; 25import com.google.common.collect.testing.TestCollectionGenerator; 26import com.google.common.collect.testing.TestListGenerator; 27import com.google.common.collect.testing.TestMapEntrySetGenerator; 28import com.google.common.collect.testing.TestStringSetGenerator; 29import com.google.common.collect.testing.TestUnhashableCollectionGenerator; 30import com.google.common.collect.testing.UnhashableObject; 31 32import java.util.Collection; 33import java.util.List; 34import java.util.Map.Entry; 35import java.util.Set; 36 37/** 38 * Generators of different types of map and related collections, such as 39 * keys, entries and values. 40 * 41 * @author Hayward Chan 42 */ 43@GwtCompatible 44public class MapGenerators { 45 46 public static class ImmutableMapKeySetGenerator 47 extends TestStringSetGenerator { 48 @Override protected Set<String> create(String[] elements) { 49 Builder<String, Integer> builder = ImmutableMap.builder(); 50 for (String key : elements) { 51 builder.put(key, 4); 52 } 53 return builder.build().keySet(); 54 } 55 } 56 57 public static class ImmutableMapValuesGenerator 58 implements TestCollectionGenerator<String> { 59 60 @Override 61 public SampleElements<String> samples() { 62 return new SampleElements.Strings(); 63 } 64 65 @Override 66 public Collection<String> create(Object... elements) { 67 Builder<Object, String> builder = ImmutableMap.builder(); 68 for (Object key : elements) { 69 builder.put(key, String.valueOf(key)); 70 } 71 return builder.build().values(); 72 } 73 74 @Override 75 public String[] createArray(int length) { 76 return new String[length]; 77 } 78 79 @Override 80 public List<String> order(List<String> insertionOrder) { 81 return insertionOrder; 82 } 83 } 84 85 public static class ImmutableMapUnhashableValuesGenerator 86 extends TestUnhashableCollectionGenerator<Collection<UnhashableObject>> { 87 88 @Override public Collection<UnhashableObject> create( 89 UnhashableObject[] elements) { 90 Builder<Integer, UnhashableObject> builder = ImmutableMap.builder(); 91 int key = 1; 92 for (UnhashableObject value : elements) { 93 builder.put(key++, value); 94 } 95 return builder.build().values(); 96 } 97 } 98 99 public static class ImmutableMapEntrySetGenerator 100 extends TestMapEntrySetGenerator<String, String> { 101 102 public ImmutableMapEntrySetGenerator() { 103 super(new SampleElements.Strings(), new SampleElements.Strings()); 104 } 105 106 @Override public Set<Entry<String, String>> createFromEntries( 107 Entry<String, String>[] entries) { 108 Builder<String, String> builder = ImmutableMap.builder(); 109 for (Entry<String, String> entry : entries) { 110 // This null-check forces NPE to be thrown for tests with null 111 // elements. Those tests aren't useful in testing entry sets 112 // because entry sets never have null elements. 113 checkNotNull(entry); 114 builder.put(entry.getKey(), entry.getValue()); 115 } 116 return builder.build().entrySet(); 117 } 118 } 119 120 public static class ImmutableMapValueListGenerator 121 implements TestListGenerator<String> { 122 @Override 123 public SampleElements<String> samples() { 124 return new SampleElements.Strings(); 125 } 126 127 @Override 128 public List<String> create(Object... elements) { 129 Builder<Integer, String> builder = ImmutableMap.builder(); 130 for (int i = 0; i < elements.length; i++) { 131 builder.put(i, toStringOrNull(elements[i])); 132 } 133 return builder.build().values().asList(); 134 } 135 136 @Override 137 public String[] createArray(int length) { 138 return new String[length]; 139 } 140 141 @Override 142 public Iterable<String> order(List<String> insertionOrder) { 143 return insertionOrder; 144 } 145 } 146 147 private static String toStringOrNull(Object o) { 148 return (o == null) ? null : o.toString(); 149 } 150} 151