Class MapGenerator<T extends Map>

Type Parameters:
T - the type of map generated
All Implemented Interfaces:
Gen<T>, Shrink<T>
Direct Known Subclasses:
HashMapGenerator, HashtableGenerator, LinkedHashMapGenerator

public abstract class MapGenerator<T extends Map> extends ComponentizedGenerator<T>

Base class for generators of Maps.

The generated map has a number of entries limited by GenerationStatus.size(), or else by the attributes of a Size marking. The individual keys and values will have types corresponding to the property parameter's type arguments.

  • Field Details

    • sizeRange

      private Size sizeRange
    • distinct

      private boolean distinct
  • Constructor Details

    • MapGenerator

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

    • configure

      public void configure(Size size)

      Tells this generator to add key-value pairs to the generated map a number of times within a specified minimum and/or maximum, inclusive, chosen with uniform distribution.

      Note that maps disallow duplicate keys, so the number of pairs added may not be equal to the map's Map.size().

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

      public void configure(Distinct distinct)
      Tells this generator to add entries whose keys are distinct from each other.
      Parameters:
      distinct - Keys of generated entries will be distinct if this param is not null
    • 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 Map>
      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 Map>
      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 Map>
      Parameters:
      value - the value to assess
      Returns:
      a measure of the given value's magnitude
      See Also:
    • empty

      protected final T empty()
    • okToAdd

      protected boolean okToAdd(Object key, Object value)
    • inSizeRange

      private boolean inSizeRange(T target)
    • size

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

      private List<T> removals(List<Map.Entry<?,?>> items)
    • convert

      private T convert(List<?> entries)
    • entryShrinker

      private Shrink<Map.Entry<?,?>> entryShrinker(Shrink<Object> keyShrinker, Shrink<Object> valueShrinker)
    • isKeyDistinct

      private static boolean isKeyDistinct(List<Map.Entry<?,?>> entries)