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.base.Preconditions.checkNotNull; 20 21 import com.google.common.annotations.GwtCompatible; 22 import com.google.common.collect.ImmutableSortedMap; 23 import com.google.common.collect.ImmutableSortedMap.Builder; 24 import com.google.common.collect.testing.SampleElements; 25 import com.google.common.collect.testing.TestCollectionGenerator; 26 import com.google.common.collect.testing.TestMapEntrySetGenerator; 27 import com.google.common.collect.testing.TestStringSetGenerator; 28 29 import java.util.Collection; 30 import java.util.Collections; 31 import java.util.Comparator; 32 import java.util.List; 33 import java.util.Map.Entry; 34 import java.util.Set; 35 36 /** 37 * Generators of sorted maps and derived collections. 38 * 39 * @author Kevin Bourrillion 40 * @author Jesse Wilson 41 * @author Jared Levy 42 * @author Hayward Chan 43 */ 44 @GwtCompatible 45 public class SortedMapGenerators { 46 47 public static final Comparator<Entry<String, String>> ENTRY_COMPARATOR 48 = new Comparator<Entry<String, String>>() { 49 @Override 50 public int compare( 51 Entry<String, String> o1, Entry<String, String> o2) { 52 return o1.getKey().compareTo(o2.getKey()); 53 } 54 }; 55 56 public static class ImmutableSortedMapKeySetGenerator 57 extends TestStringSetGenerator { 58 59 @Override protected Set<String> create(String[] elements) { 60 Builder<String, Integer> builder = ImmutableSortedMap.naturalOrder(); 61 for (String key : elements) { 62 builder.put(key, 4); 63 } 64 return builder.build().keySet(); 65 } 66 @Override public List<String> order(List<String> insertionOrder) { 67 Collections.sort(insertionOrder); 68 return insertionOrder; 69 } 70 } 71 72 public static class ImmutableSortedMapValuesGenerator 73 implements TestCollectionGenerator<String> { 74 75 @Override 76 public SampleElements<String> samples() { 77 return new SampleElements.Strings(); 78 } 79 80 @Override 81 public Collection<String> create(Object... elements) { 82 Builder<Integer, String> builder = ImmutableSortedMap.naturalOrder(); 83 for (int i = 0; i < elements.length; i++) { 84 builder.put(i, toStringOrNull(elements[i])); 85 } 86 return builder.build().values(); 87 } 88 89 @Override 90 public String[] createArray(int length) { 91 return new String[length]; 92 } 93 94 @Override 95 public List<String> order(List<String> insertionOrder) { 96 return insertionOrder; 97 } 98 } 99 100 public static class ImmutableSortedMapSubMapEntryGenerator 101 extends TestMapEntrySetGenerator<String, String> { 102 103 public ImmutableSortedMapSubMapEntryGenerator() { 104 super(new SampleElements.Strings(), new SampleElements.Strings()); 105 } 106 107 @Override public Set<Entry<String, String>> createFromEntries( 108 Entry<String, String>[] entries) { 109 Builder<String, String> builder = ImmutableSortedMap.naturalOrder(); 110 builder.put(SampleElements.Strings.BEFORE_FIRST, "begin"); 111 builder.put(SampleElements.Strings.AFTER_LAST, "end"); 112 for (Entry<String, String> entry : entries) { 113 checkNotNull(entry); 114 builder.put(entry.getKey(), entry.getValue()); 115 } 116 return builder.build().subMap(SampleElements.Strings.MIN_ELEMENT, 117 SampleElements.Strings.AFTER_LAST).entrySet(); 118 } 119 @Override public List<Entry<String, String>> order( 120 List<Entry<String, String>> insertionOrder) { 121 Collections.sort(insertionOrder, ENTRY_COMPARATOR); 122 return insertionOrder; 123 } 124 } 125 126 public static class ImmutableSortedMapHeadMapKeySetGenerator 127 extends TestStringSetGenerator { 128 @Override protected Set<String> create(String[] elements) { 129 Builder<String, Integer> builder = ImmutableSortedMap.naturalOrder(); 130 builder.put(SampleElements.Strings.AFTER_LAST, -1); 131 for (String key : elements) { 132 builder.put(key, 4); 133 } 134 return builder.build().headMap( 135 SampleElements.Strings.AFTER_LAST).keySet(); 136 } 137 @Override public List<String> order(List<String> insertionOrder) { 138 Collections.sort(insertionOrder); 139 return insertionOrder; 140 } 141 } 142 143 public static class ImmutableSortedMapTailMapValuesGenerator 144 implements TestCollectionGenerator<String> { 145 146 @Override 147 public SampleElements<String> samples() { 148 return new SampleElements.Strings(); 149 } 150 151 @Override 152 public Collection<String> create(Object... elements) { 153 Builder<Integer, String> builder = ImmutableSortedMap.naturalOrder(); 154 builder.put(-1, "begin"); 155 for (int i = 0; i < elements.length; i++) { 156 builder.put(i, toStringOrNull(elements[i])); 157 } 158 return builder.build().tailMap(0).values(); 159 } 160 161 @Override 162 public String[] createArray(int length) { 163 return new String[length]; 164 } 165 166 @Override 167 public List<String> order(List<String> insertionOrder) { 168 return insertionOrder; 169 } 170 } 171 172 public static class ImmutableSortedMapEntrySetGenerator 173 extends TestMapEntrySetGenerator<String, String> { 174 175 public ImmutableSortedMapEntrySetGenerator() { 176 super(new SampleElements.Strings(), new SampleElements.Strings()); 177 } 178 179 @Override public Set<Entry<String, String>> createFromEntries( 180 Entry<String, String>[] entries) { 181 Builder<String, String> builder = ImmutableSortedMap.naturalOrder(); 182 for (Entry<String, String> entry : entries) { 183 checkNotNull(entry); 184 builder.put(entry.getKey(), entry.getValue()); 185 } 186 return builder.build().entrySet(); 187 } 188 189 @Override public List<Entry<String, String>> order( 190 List<Entry<String, String>> insertionOrder) { 191 Collections.sort(insertionOrder, ENTRY_COMPARATOR); 192 return insertionOrder; 193 } 194 } 195 196 private static String toStringOrNull(Object o) { 197 return (o == null) ? null : o.toString(); 198 } 199 } 200