Debian Codegraph @

guava-testlib/src/com/google/common/collect/testing/OneSizeGenerator.java

raw
1 /*
2 * Copyright (C) 2008 The Guava Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.common.collect.testing;
18
19 import com.google.common.annotations.GwtCompatible;
20 import com.google.common.collect.testing.features.CollectionSize;
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.Collection;
24 import java.util.List;
25
26 /**
27 * Generator for collection of a particular size.
28 *
29 * @author George van den Driessche
30 */
31 @GwtCompatible
32 public final class OneSizeGenerator<T, E> implements OneSizeTestContainerGenerator<T, E> {
33 private final TestContainerGenerator<T, E> generator;
34 private final CollectionSize collectionSize;
35
36 public OneSizeGenerator(TestContainerGenerator<T, E> generator, CollectionSize collectionSize) {
37 this.generator = generator;
38 this.collectionSize = collectionSize;
39 }
40
41 @Override
42 public TestContainerGenerator<T, E> getInnerGenerator() {
43 return generator;
44 }
45
46 @Override
47 public SampleElements<E> samples() {
48 return generator.samples();
49 }
50
51 @Override
52 public T create(Object... elements) {
53 return generator.create(elements);
54 }
55
56 @Override
57 public E[] createArray(int length) {
58 return generator.createArray(length);
59 }
60
61 @Override
62 public T createTestSubject() {
63 Collection<E> elements = getSampleElements(getCollectionSize().getNumElements());
64 return generator.create(elements.toArray());
65 }
66
67 @Override
68 public Collection<E> getSampleElements(int howMany) {
69 SampleElements<E> samples = samples();
70 @SuppressWarnings("unchecked")
71 List<E> allSampleElements =
72 Arrays.asList(samples.e0(), samples.e1(), samples.e2(), samples.e3(), samples.e4());
73 return new ArrayList<>(allSampleElements.subList(0, howMany));
74 }
75
76 @Override
77 public CollectionSize getCollectionSize() {
78 return collectionSize;
79 }
80
81 @Override
82 public Iterable<E> order(List<E> insertionOrder) {
83 return generator.order(insertionOrder);
84 }
85 }