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>

public class CollectionGenerator<T> extends Object implements 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 Details

    • CollectionGenerator

      public CollectionGenerator(TypedGenerator<T> wrapped, TypedGenerator<Integer> sizeGenerator)
      Parameters:
      wrapped - must not be null
      sizeGenerator - must not be null
    • CollectionGenerator

      public CollectionGenerator(TypedGenerator<T> wrapped, int lowerBound, int upperBound)
      Constructor.
      Parameters:
      wrapped - generator, must not be null
      lowerBound - defines the lower bound of the integer generator that determines the of Collection size
      upperBound - defines the upper bound of the integer generator that determines the of Collection size
    • CollectionGenerator

      Constructor. using 2 and 12 as bounds of the Collection size to be created.
      Parameters:
      wrapped - generator, must not be null
  • Method Details

    • next

      public T next()
      Description copied from interface: TypedGenerator
      Generates the next instance based on the generator's configuration. Implementations must ensure thread-safety.
      Specified by:
      next in interface TypedGenerator<T>
      Returns:
      the next object from the contained TypedGenerator
    • list

      public List<T> list(int count)
      Returns a List of 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

      public Set<T> set(int count)
      Returns a Set of the elements provided by the generator
      Parameters:
      count - the number of elements within the Set. It defines an upper bound of elements, but depending on the elements / the entropy of the generator there may be a lower number of elements.
      Returns:
      a Set with a given number of elements as maximum.
    • sortedSet

      public SortedSet<T> sortedSet(int count)
      Returns a SortedSet of the elements provided by the generator
      Parameters:
      count - the number of elements within the Set. It defines an upper bound of elements, but depending on the elements / the entropy of the generator there may be a lower number of elements.
      Returns:
      a Set with a given number of elements as maximum.
    • set

      public Set<T> set()
      Returns:
      a Set with a random number of elements as maximum.
    • list

      public List<T> list()
      Returns:
      a List with a random number of elements as maximum.
    • sortedSet

      public SortedSet<T> sortedSet()
      Returns:
      a SortedSet with a random number of elements as maximum.
    • nextCollection

      public Iterable<T> nextCollection(Class<? extends Iterable<?>> expectedType)
      Generates a concrete Iterable. It is smart enough to determine whether the elements are to be wrapped in a List, Set, Collection or SortedSet.
      Parameters:
      expectedType - type of the expected Iterable
      Returns:
      depending on the given expectedType a corresponding Iterable, Collection, List, SortedSet or Set