001/* 002 * Copyright (C) 2008 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; 018 019import static java.util.Arrays.asList; 020 021import com.google.common.annotations.GwtCompatible; 022import com.google.common.collect.testing.features.CollectionSize; 023import java.util.ArrayList; 024import java.util.Collection; 025import java.util.List; 026import org.jspecify.annotations.NullMarked; 027import org.jspecify.annotations.Nullable; 028 029/** 030 * Generator for collection of a particular size. 031 * 032 * @author George van den Driessche 033 */ 034@GwtCompatible 035@NullMarked 036public final class OneSizeGenerator<T, E extends @Nullable Object> 037 implements OneSizeTestContainerGenerator<T, E> { 038 private final TestContainerGenerator<T, E> generator; 039 private final CollectionSize collectionSize; 040 041 public OneSizeGenerator(TestContainerGenerator<T, E> generator, CollectionSize collectionSize) { 042 this.generator = generator; 043 this.collectionSize = collectionSize; 044 } 045 046 @Override 047 public TestContainerGenerator<T, E> getInnerGenerator() { 048 return generator; 049 } 050 051 @Override 052 public SampleElements<E> samples() { 053 return generator.samples(); 054 } 055 056 @Override 057 public T create(Object... elements) { 058 return generator.create(elements); 059 } 060 061 @Override 062 public E[] createArray(int length) { 063 return generator.createArray(length); 064 } 065 066 @Override 067 public T createTestSubject() { 068 Collection<E> elements = getSampleElements(getCollectionSize().getNumElements()); 069 return generator.create(elements.toArray()); 070 } 071 072 @Override 073 public Collection<E> getSampleElements(int howMany) { 074 SampleElements<E> samples = samples(); 075 List<E> allSampleElements = 076 asList(samples.e0(), samples.e1(), samples.e2(), samples.e3(), samples.e4()); 077 return new ArrayList<>(allSampleElements.subList(0, howMany)); 078 } 079 080 @Override 081 public CollectionSize getCollectionSize() { 082 return collectionSize; 083 } 084 085 @Override 086 public Iterable<E> order(List<E> insertionOrder) { 087 return generator.order(insertionOrder); 088 } 089}