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; 20import static com.google.common.collect.testing.Helpers.mapEntry; 21 22import com.google.common.annotations.GwtCompatible; 23import com.google.common.collect.ImmutableSortedMap; 24import com.google.common.collect.Ordering; 25import com.google.common.collect.testing.SampleElements; 26import com.google.common.collect.testing.TestListGenerator; 27import com.google.common.collect.testing.TestStringListGenerator; 28import com.google.common.collect.testing.TestStringSortedMapGenerator; 29 30import java.util.List; 31import java.util.Map.Entry; 32import java.util.SortedMap; 33 34/** 35 * Generators of sorted maps and derived collections. 36 * 37 * @author Kevin Bourrillion 38 * @author Jesse Wilson 39 * @author Jared Levy 40 * @author Hayward Chan 41 * @author Chris Povirk 42 * @author Louis Wasserman 43 */ 44@GwtCompatible 45public class SortedMapGenerators { 46 public static class ImmutableSortedMapGenerator extends TestStringSortedMapGenerator { 47 @Override public SortedMap<String, String> create(Entry<String, String>[] entries) { 48 ImmutableSortedMap.Builder<String, String> builder = ImmutableSortedMap.naturalOrder(); 49 for (Entry<String, String> entry : entries) { 50 checkNotNull(entry); 51 builder.put(entry.getKey(), entry.getValue()); 52 } 53 return builder.build(); 54 } 55 } 56 57 public static class ImmutableSortedMapEntryListGenerator 58 implements TestListGenerator<Entry<String, Integer>> { 59 60 @Override 61 public SampleElements<Entry<String, Integer>> samples() { 62 return new SampleElements<Entry<String, Integer>>( 63 mapEntry("foo", 5), 64 mapEntry("bar", 3), 65 mapEntry("baz", 17), 66 mapEntry("quux", 1), 67 mapEntry("toaster", -2)); 68 } 69 70 @SuppressWarnings("unchecked") 71 @Override 72 public Entry<String, Integer>[] createArray(int length) { 73 return new Entry[length]; 74 } 75 76 @Override 77 public Iterable<Entry<String, Integer>> order(List<Entry<String, Integer>> insertionOrder) { 78 return new Ordering<Entry<String, Integer>>() { 79 @Override 80 public int compare(Entry<String, Integer> left, Entry<String, Integer> right) { 81 return left.getKey().compareTo(right.getKey()); 82 } 83 }.sortedCopy(insertionOrder); 84 } 85 86 @Override 87 public List<Entry<String, Integer>> create(Object... elements) { 88 ImmutableSortedMap.Builder<String, Integer> builder = ImmutableSortedMap.naturalOrder(); 89 for (Object o : elements) { 90 @SuppressWarnings("unchecked") 91 Entry<String, Integer> entry = (Entry<String, Integer>) o; 92 builder.put(entry); 93 } 94 return builder.build().entrySet().asList(); 95 } 96 } 97 98 public static class ImmutableSortedMapKeyListGenerator extends TestStringListGenerator { 99 @Override protected List<String> create(String[] elements) { 100 ImmutableSortedMap.Builder<String, Integer> builder = ImmutableSortedMap.naturalOrder(); 101 for (int i = 0; i < elements.length; i++) { 102 builder.put(elements[i], i); 103 } 104 return builder.build().keySet().asList(); 105 } 106 107 @Override 108 public List<String> order(List<String> insertionOrder) { 109 return Ordering.natural().sortedCopy(insertionOrder); 110 } 111 } 112 113 public static class ImmutableSortedMapValueListGenerator extends TestStringListGenerator { 114 @Override protected List<String> create(String[] elements) { 115 ImmutableSortedMap.Builder<Integer, String> builder = ImmutableSortedMap.naturalOrder(); 116 for (int i = 0; i < elements.length; i++) { 117 builder.put(i, elements[i]); 118 } 119 return builder.build().values().asList(); 120 } 121 } 122} 123