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 040public final class ListGenerators { 041 042 private ListGenerators() {} 043 044 public static class ImmutableListOfGenerator extends TestStringListGenerator { 045 @Override 046 protected List<String> create(String[] elements) { 047 return ImmutableList.copyOf(elements); 048 } 049 } 050 051 public static class BuilderAddListGenerator extends TestStringListGenerator { 052 @Override 053 protected List<String> create(String[] elements) { 054 ImmutableList.Builder<String> builder = ImmutableList.<String>builder(); 055 for (String element : elements) { 056 builder.add(element); 057 } 058 return builder.build(); 059 } 060 } 061 062 public static class BuilderAddAllListGenerator extends TestStringListGenerator { 063 @Override 064 protected List<String> create(String[] elements) { 065 return ImmutableList.<String>builder().addAll(asList(elements)).build(); 066 } 067 } 068 069 public static class BuilderReversedListGenerator extends TestStringListGenerator { 070 @Override 071 protected List<String> create(String[] elements) { 072 List<String> list = asList(elements); 073 Collections.reverse(list); 074 return ImmutableList.copyOf(list).reverse(); 075 } 076 } 077 078 public static class ImmutableListHeadSubListGenerator extends TestStringListGenerator { 079 @Override 080 protected List<String> create(String[] elements) { 081 String[] suffix = {"f", "g"}; 082 String[] all = new String[elements.length + suffix.length]; 083 System.arraycopy(elements, 0, all, 0, elements.length); 084 System.arraycopy(suffix, 0, all, elements.length, suffix.length); 085 return ImmutableList.copyOf(all).subList(0, elements.length); 086 } 087 } 088 089 public static class ImmutableListTailSubListGenerator extends TestStringListGenerator { 090 @Override 091 protected List<String> create(String[] elements) { 092 String[] prefix = {"f", "g"}; 093 String[] all = new String[elements.length + prefix.length]; 094 System.arraycopy(prefix, 0, all, 0, 2); 095 System.arraycopy(elements, 0, all, 2, elements.length); 096 return ImmutableList.copyOf(all).subList(2, elements.length + 2); 097 } 098 } 099 100 public static class ImmutableListMiddleSubListGenerator extends TestStringListGenerator { 101 @Override 102 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).subList(2, elements.length + 2); 112 } 113 } 114 115 public static class CharactersOfStringGenerator extends TestCharacterListGenerator { 116 @Override 117 public List<Character> create(Character[] elements) { 118 char[] chars = Chars.toArray(Arrays.asList(elements)); 119 return Lists.charactersOf(String.copyValueOf(chars)); 120 } 121 } 122 123 public static class CharactersOfCharSequenceGenerator extends TestCharacterListGenerator { 124 @Override 125 public List<Character> create(Character[] elements) { 126 char[] chars = Chars.toArray(Arrays.asList(elements)); 127 StringBuilder str = new StringBuilder(); 128 str.append(chars); 129 return Lists.charactersOf(str); 130 } 131 } 132 133 private abstract static class TestUnhashableListGenerator 134 extends TestUnhashableCollectionGenerator<List<UnhashableObject>> 135 implements TestListGenerator<UnhashableObject> {} 136 137 public static class UnhashableElementsImmutableListGenerator extends TestUnhashableListGenerator { 138 @Override 139 public List<UnhashableObject> create(UnhashableObject[] elements) { 140 return ImmutableList.copyOf(elements); 141 } 142 } 143}