Package de.cuioss.test.generator.impl
Class CollectionGenerator<T>
java.lang.Object
de.cuioss.test.generator.impl.CollectionGenerator<T>
- Type Parameters:
T- The type of elements to be generated
- All Implemented Interfaces:
TypedGenerator<T>
Enhances a
TypedGenerator with collection generation capabilities.
This wrapper adds methods for creating Lists, Sets, and other collection types
from any existing generator.
Features:
- Generates Lists with configurable size
- Creates Sets (both regular and sorted)
- Supports any collection type that can be built from Lists or Sets
- Thread-safe if the wrapped generator is thread-safe
Example usage:
// Create a generator for integers
TypedGenerator<Integer> intGen = Generators.integers(1, 100);
// Create a collection generator
var collectionGen = new CollectionGenerator<>(intGen);
// Generate collections
List<Integer> list = collectionGen.list(5); // List of 5 integers
Set<Integer> set = collectionGen.set(3); // Set of 3 integers
Collection<Integer> coll = collectionGen.next(); // Random size collection
- Author:
- Oliver Wolff
-
Constructor Summary
ConstructorsConstructorDescriptionCollectionGenerator(TypedGenerator<T> wrapped) Constructor. using 2 and 12 as bounds of theCollectionsize to be created.CollectionGenerator(TypedGenerator<T> wrapped, int lowerBound, int upperBound) Constructor.CollectionGenerator(TypedGenerator<T> wrapped, TypedGenerator<Integer> sizeGenerator) -
Method Summary
Modifier and TypeMethodDescriptionlist()list(int count) Returns aListof the elements provided by the generatornext()Generates the next instance based on the generator's configuration.nextCollection(Class<? extends Iterable<?>> expectedType) Generates a concreteIterable.set()set(int count) Returns aSetof the elements provided by the generatorsortedSet(int count) Returns aSortedSetof the elements provided by the generatorMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface de.cuioss.test.generator.TypedGenerator
getType
-
Constructor Details
-
CollectionGenerator
- Parameters:
wrapped- must not be nullsizeGenerator- must not be null
-
CollectionGenerator
Constructor.- Parameters:
wrapped- generator, must not be nulllowerBound- defines the lower bound of the integer generator that determines the ofCollectionsizeupperBound- defines the upper bound of the integer generator that determines the ofCollectionsize
-
CollectionGenerator
Constructor. using 2 and 12 as bounds of theCollectionsize to be created.- Parameters:
wrapped- generator, must not be null
-
-
Method Details
-
next
Description copied from interface:TypedGeneratorGenerates the next instance based on the generator's configuration. Implementations must ensure thread-safety.- Specified by:
nextin interfaceTypedGenerator<T>- Returns:
- the next object from the contained
TypedGenerator
-
list
Returns aListof the elements provided by the generator- Parameters:
count- the number of elements within the list- Returns:
- a list with a given number of elements.
-
set
Returns aSetof the elements provided by the generator -
sortedSet
Returns aSortedSetof the elements provided by the generator -
set
- Returns:
- a
Setwith a random number of elements as maximum.
-
list
- Returns:
- a
Listwith a random number of elements as maximum.
-
sortedSet
- Returns:
- a
SortedSetwith a random number of elements as maximum.
-
nextCollection
Generates a concreteIterable. It is smart enough to determine whether the elements are to be wrapped in aList,Set,CollectionorSortedSet.
-