Class CollectionGenerator<T extends Collection>

Type Parameters:
T - the type of collection generated
All Implemented Interfaces:
Gen<T>, Shrink<T>
Direct Known Subclasses:
ListGenerator, SetGenerator

public abstract class CollectionGenerator<T extends Collection> extends ComponentizedGenerator<T>

Base class for generators of Collections.

The generated collection has a number of elements limited by GenerationStatus.size(), or else by the attributes of a Size marking. The individual elements will have a type corresponding to the collection's type argument.

  • Field Details

    • sizeRange

      private Size sizeRange
    • distinct

      private boolean distinct
  • Constructor Details

    • CollectionGenerator

      protected CollectionGenerator(Class<T> type)
  • Method Details

    • configure

      public void configure(Size size)

      Tells this generator to add elements to the generated collection a number of times within a specified minimum and/or maximum, inclusive, chosen with uniform distribution.

      Note that some kinds of collections disallow duplicates, so the number of elements added may not be equal to the collection's Collection.size().

      Parameters:
      size - annotation that gives the size constraints
    • configure

      public void configure(Distinct distinct)
      Tells this generator to add elements which are distinct from each other.
      Parameters:
      distinct - Generated elements will be distinct if this param is not null
    • setDistinct

      protected final void setDistinct(boolean distinct)
    • generate

      public T generate(SourceOfRandomness random, GenerationStatus status)
      Description copied from interface: Gen
      Generates a value, possibly influenced by a source of randomness and metadata about the generation.
      Parameters:
      random - source of randomness to be used when generating the value
      status - an object that can be used to influence the generated value. For example, generating lists can use the size method to generate lists with a given number of elements.
      Returns:
      the generated value
    • doShrink

      public List<T> doShrink(SourceOfRandomness random, T larger)
      Description copied from class: Generator

      Gives some objects that are "smaller" than a given "larger" object.

      Unless overridden, a generator will produce an empty list of "smaller" values.

      Overrides:
      doShrink in class Generator<T extends Collection>
      Parameters:
      random - source of randomness to use in shrinking, if desired
      larger - the larger object
      Returns:
      objects that are "smaller" than the larger object. Smaller objects should be provided before bigger objects to make shrinking process faster.
    • numberOfNeededComponents

      public int numberOfNeededComponents()
      Description copied from class: Generator

      This is intended for use only by junit-quickcheck itself, and not by creators of custom generators.

      Overrides:
      numberOfNeededComponents in class Generator<T extends Collection>
      Returns:
      how many component generators this generator needs
      See Also:
    • magnitude

      public BigDecimal magnitude(Object value)
      Description copied from class: Generator

      Gives a hint to the shrinking process as to the magnitude of the given value. The shrinking process will prefer trying values of greater magnitude before values of lesser magnitude. If not overridden, this implementation returns "one".

      Note to generator writers: Do not worry about normalizing a magnitude to a positive value; the shrinking mechanism will take care of it.

      Overrides:
      magnitude in class Generator<T extends Collection>
      Parameters:
      value - the value to assess
      Returns:
      a measure of the given value's magnitude
      See Also:
    • empty

      protected final T empty()
    • inSizeRange

      private boolean inSizeRange(T items)
    • size

      private int size(SourceOfRandomness random, GenerationStatus status)
    • removals

      private List<T> removals(List<?> items)
    • convert

      private T convert(List<?> items)