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 java.util.Arrays.asList; 20 21 import com.google.common.annotations.GwtCompatible; 22 import com.google.common.collect.ImmutableList; 23 import com.google.common.collect.ImmutableList.Builder; 24 import com.google.common.collect.testing.TestListGenerator; 25 import com.google.common.collect.testing.TestStringListGenerator; 26 import com.google.common.collect.testing.TestUnhashableCollectionGenerator; 27 import com.google.common.collect.testing.UnhashableObject; 28 29 import java.util.Collections; 30 import java.util.List; 31 32 /** 33 * Common generators of different types of lists. 34 * 35 * @author Hayward Chan 36 */ 37 @GwtCompatible 38 public final class ListGenerators { 39 40 private ListGenerators() {} 41 42 public static class ImmutableListOfGenerator extends TestStringListGenerator { 43 @Override protected List<String> create(String[] elements) { 44 return ImmutableList.copyOf(elements); 45 } 46 } 47 48 public static class BuilderAddListGenerator extends TestStringListGenerator { 49 @Override protected List<String> create(String[] elements) { 50 Builder<String> builder = ImmutableList.<String>builder(); 51 for (String element : elements) { 52 builder.add(element); 53 } 54 return builder.build(); 55 } 56 } 57 58 public static class BuilderAddAllListGenerator 59 extends TestStringListGenerator { 60 @Override protected List<String> create(String[] elements) { 61 return ImmutableList.<String>builder() 62 .addAll(asList(elements)) 63 .build(); 64 } 65 } 66 67 public static class BuilderReversedListGenerator 68 extends TestStringListGenerator { 69 @Override protected List<String> create(String[] elements) { 70 List<String> list = asList(elements); 71 Collections.reverse(list); 72 return ImmutableList.copyOf(list).reverse(); 73 } 74 } 75 76 public static class ImmutableListHeadSubListGenerator 77 extends TestStringListGenerator { 78 @Override protected List<String> create(String[] elements) { 79 String[] suffix = {"f", "g"}; 80 String[] all = new String[elements.length + suffix.length]; 81 System.arraycopy(elements, 0, all, 0, elements.length); 82 System.arraycopy(suffix, 0, all, elements.length, suffix.length); 83 return ImmutableList.copyOf(all) 84 .subList(0, elements.length); 85 } 86 } 87 88 public static class ImmutableListTailSubListGenerator 89 extends TestStringListGenerator { 90 @Override protected List<String> create(String[] elements) { 91 String[] prefix = {"f", "g"}; 92 String[] all = new String[elements.length + prefix.length]; 93 System.arraycopy(prefix, 0, all, 0, 2); 94 System.arraycopy(elements, 0, all, 2, elements.length); 95 return ImmutableList.copyOf(all) 96 .subList(2, elements.length + 2); 97 } 98 } 99 100 public static class ImmutableListMiddleSubListGenerator 101 extends TestStringListGenerator { 102 @Override protected List<String> create(String[] elements) { 103 String[] prefix = {"f", "g"}; 104 String[] suffix = {"h", "i"}; 105 106 String[] all = new String[2 + elements.length + 2]; 107 System.arraycopy(prefix, 0, all, 0, 2); 108 System.arraycopy(elements, 0, all, 2, elements.length); 109 System.arraycopy(suffix, 0, all, 2 + elements.length, 2); 110 111 return ImmutableList.copyOf(all) 112 .subList(2, elements.length + 2); 113 } 114 } 115 116 private abstract static class TestUnhashableListGenerator 117 extends TestUnhashableCollectionGenerator<List<UnhashableObject>> 118 implements TestListGenerator<UnhashableObject> { 119 } 120 121 public static class UnhashableElementsImmutableListGenerator 122 extends TestUnhashableListGenerator { 123 @Override 124 public List<UnhashableObject> create(UnhashableObject[] elements) { 125 return ImmutableList.copyOf(elements); 126 } 127 } 128 } 129