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