001/* 002 * Copyright (C) 2009 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.collect.testing.google; 018 019import static java.util.Arrays.asList; 020 021import com.google.common.annotations.GwtCompatible; 022import com.google.common.collect.ImmutableList; 023import com.google.common.collect.Lists; 024import com.google.common.collect.testing.TestCharacterListGenerator; 025import com.google.common.collect.testing.TestListGenerator; 026import com.google.common.collect.testing.TestStringListGenerator; 027import com.google.common.collect.testing.TestUnhashableCollectionGenerator; 028import com.google.common.collect.testing.UnhashableObject; 029import com.google.common.primitives.Chars; 030import java.util.Arrays; 031import java.util.Collections; 032import java.util.List; 033 034/** 035 * Common generators of different types of lists. 036 * 037 * @author Hayward Chan 038 */ 039@GwtCompatible 040@ElementTypesAreNonnullByDefault 041public final class ListGenerators { 042 043 private ListGenerators() {} 044 045 public static class ImmutableListOfGenerator extends TestStringListGenerator { 046 @Override 047 protected List<String> create(String[] elements) { 048 return ImmutableList.copyOf(elements); 049 } 050 } 051 052 public static class BuilderAddListGenerator extends TestStringListGenerator { 053 @Override 054 protected List<String> create(String[] elements) { 055 ImmutableList.Builder<String> builder = ImmutableList.<String>builder(); 056 for (String element : elements) { 057 builder.add(element); 058 } 059 return builder.build(); 060 } 061 } 062 063 public static class BuilderAddAllListGenerator extends TestStringListGenerator { 064 @Override 065 protected List<String> create(String[] elements) { 066 return ImmutableList.<String>builder().addAll(asList(elements)).build(); 067 } 068 } 069 070 public static class BuilderReversedListGenerator extends TestStringListGenerator { 071 @Override 072 protected List<String> create(String[] elements) { 073 List<String> list = asList(elements); 074 Collections.reverse(list); 075 return ImmutableList.copyOf(list).reverse(); 076 } 077 } 078 079 public static class ImmutableListHeadSubListGenerator extends TestStringListGenerator { 080 @Override 081 protected List<String> create(String[] elements) { 082 String[] suffix = {"f", "g"}; 083 String[] all = new String[elements.length + suffix.length]; 084 System.arraycopy(elements, 0, all, 0, elements.length); 085 System.arraycopy(suffix, 0, all, elements.length, suffix.length); 086 return ImmutableList.copyOf(all).subList(0, elements.length); 087 } 088 } 089 090 public static class ImmutableListTailSubListGenerator extends TestStringListGenerator { 091 @Override 092 protected List<String> create(String[] elements) { 093 String[] prefix = {"f", "g"}; 094 String[] all = new String[elements.length + prefix.length]; 095 System.arraycopy(prefix, 0, all, 0, 2); 096 System.arraycopy(elements, 0, all, 2, elements.length); 097 return ImmutableList.copyOf(all).subList(2, elements.length + 2); 098 } 099 } 100 101 public static class ImmutableListMiddleSubListGenerator extends TestStringListGenerator { 102 @Override 103 protected List<String> create(String[] elements) { 104 String[] prefix = {"f", "g"}; 105 String[] suffix = {"h", "i"}; 106 107 String[] all = new String[2 + elements.length + 2]; 108 System.arraycopy(prefix, 0, all, 0, 2); 109 System.arraycopy(elements, 0, all, 2, elements.length); 110 System.arraycopy(suffix, 0, all, 2 + elements.length, 2); 111 112 return ImmutableList.copyOf(all).subList(2, elements.length + 2); 113 } 114 } 115 116 public static class CharactersOfStringGenerator extends TestCharacterListGenerator { 117 @Override 118 public List<Character> create(Character[] elements) { 119 char[] chars = Chars.toArray(Arrays.asList(elements)); 120 return Lists.charactersOf(String.copyValueOf(chars)); 121 } 122 } 123 124 public static class CharactersOfCharSequenceGenerator extends TestCharacterListGenerator { 125 @Override 126 public List<Character> create(Character[] elements) { 127 char[] chars = Chars.toArray(Arrays.asList(elements)); 128 StringBuilder str = new StringBuilder(); 129 str.append(chars); 130 return Lists.charactersOf(str); 131 } 132 } 133 134 private abstract static class TestUnhashableListGenerator 135 extends TestUnhashableCollectionGenerator<List<UnhashableObject>> 136 implements TestListGenerator<UnhashableObject> {} 137 138 public static class UnhashableElementsImmutableListGenerator extends TestUnhashableListGenerator { 139 @Override 140 public List<UnhashableObject> create(UnhashableObject[] elements) { 141 return ImmutableList.copyOf(elements); 142 } 143 } 144}