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 com.google.common.collect.Lists.charactersOf;
020import static java.lang.System.arraycopy;
021import static java.util.Arrays.asList;
022
023import com.google.common.annotations.GwtCompatible;
024import com.google.common.collect.ImmutableList;
025import com.google.common.collect.testing.TestCharacterListGenerator;
026import com.google.common.collect.testing.TestListGenerator;
027import com.google.common.collect.testing.TestStringListGenerator;
028import com.google.common.collect.testing.TestUnhashableCollectionGenerator;
029import com.google.common.collect.testing.UnhashableObject;
030import com.google.common.primitives.Chars;
031import java.util.Collections;
032import java.util.List;
033import org.jspecify.annotations.NullMarked;
034
035/**
036 * Common generators of different types of lists.
037 *
038 * @author Hayward Chan
039 */
040@GwtCompatible
041@NullMarked
042public final class ListGenerators {
043
044  private ListGenerators() {}
045
046  public static class ImmutableListOfGenerator extends TestStringListGenerator {
047    @Override
048    protected List<String> create(String[] elements) {
049      return ImmutableList.copyOf(elements);
050    }
051  }
052
053  public static class BuilderAddListGenerator extends TestStringListGenerator {
054    @Override
055    protected List<String> create(String[] elements) {
056      ImmutableList.Builder<String> builder = ImmutableList.<String>builder();
057      for (String element : elements) {
058        builder.add(element);
059      }
060      return builder.build();
061    }
062  }
063
064  public static class BuilderAddAllListGenerator extends TestStringListGenerator {
065    @Override
066    protected List<String> create(String[] elements) {
067      return ImmutableList.<String>builder().addAll(asList(elements)).build();
068    }
069  }
070
071  public static class BuilderReversedListGenerator extends TestStringListGenerator {
072    @Override
073    protected List<String> create(String[] elements) {
074      List<String> list = asList(elements);
075      Collections.reverse(list);
076      return ImmutableList.copyOf(list).reverse();
077    }
078  }
079
080  public static class ImmutableListHeadSubListGenerator extends TestStringListGenerator {
081    @Override
082    protected List<String> create(String[] elements) {
083      String[] suffix = {"f", "g"};
084      String[] all = new String[elements.length + suffix.length];
085      arraycopy(elements, 0, all, 0, elements.length);
086      arraycopy(suffix, 0, all, elements.length, suffix.length);
087      return ImmutableList.copyOf(all).subList(0, elements.length);
088    }
089  }
090
091  public static class ImmutableListTailSubListGenerator extends TestStringListGenerator {
092    @Override
093    protected List<String> create(String[] elements) {
094      String[] prefix = {"f", "g"};
095      String[] all = new String[elements.length + prefix.length];
096      arraycopy(prefix, 0, all, 0, 2);
097      arraycopy(elements, 0, all, 2, elements.length);
098      return ImmutableList.copyOf(all).subList(2, elements.length + 2);
099    }
100  }
101
102  public static class ImmutableListMiddleSubListGenerator extends TestStringListGenerator {
103    @Override
104    protected List<String> create(String[] elements) {
105      String[] prefix = {"f", "g"};
106      String[] suffix = {"h", "i"};
107
108      String[] all = new String[2 + elements.length + 2];
109      arraycopy(prefix, 0, all, 0, 2);
110      arraycopy(elements, 0, all, 2, elements.length);
111      arraycopy(suffix, 0, all, 2 + elements.length, 2);
112
113      return ImmutableList.copyOf(all).subList(2, elements.length + 2);
114    }
115  }
116
117  public static class CharactersOfStringGenerator extends TestCharacterListGenerator {
118    @Override
119    public List<Character> create(Character[] elements) {
120      char[] chars = Chars.toArray(asList(elements));
121      return charactersOf(String.copyValueOf(chars));
122    }
123  }
124
125  public static class CharactersOfCharSequenceGenerator extends TestCharacterListGenerator {
126    @Override
127    public List<Character> create(Character[] elements) {
128      char[] chars = Chars.toArray(asList(elements));
129      StringBuilder str = new StringBuilder();
130      str.append(chars);
131      return charactersOf(str);
132    }
133  }
134
135  private abstract static class TestUnhashableListGenerator
136      extends TestUnhashableCollectionGenerator<List<UnhashableObject>>
137      implements TestListGenerator<UnhashableObject> {}
138
139  public static class UnhashableElementsImmutableListGenerator extends TestUnhashableListGenerator {
140    @Override
141    public List<UnhashableObject> create(UnhashableObject[] elements) {
142      return ImmutableList.copyOf(elements);
143    }
144  }
145}