Class MapGenerator<K,V>

java.lang.Object
org.instancio.internal.generator.AbstractGenerator<Map<K,V>>
org.instancio.internal.generator.util.MapGenerator<K,V>
All Implemented Interfaces:
Generator<Map<K,V>>, GeneratorSpec<Map<K,V>>, MapGeneratorSpec<K,V>
Direct Known Subclasses:
ConcurrentHashMapGenerator, ConcurrentSkipListMapGenerator, MapGeneratorSpecImpl, TreeMapGenerator

public class MapGenerator<K,V> extends AbstractGenerator<Map<K,V>> implements MapGeneratorSpec<K,V>
  • Field Details

    • minSize

      protected int minSize
    • maxSize

      protected int maxSize
    • nullable

      protected boolean nullable
    • nullableKeys

      protected boolean nullableKeys
    • nullableValues

      protected boolean nullableValues
    • mapType

      protected Class<?> mapType
    • isDelegating

      protected boolean isDelegating
  • Constructor Details

  • Method Details

    • subtype

      public MapGeneratorSpec<K,V> subtype(Class<?> type)
      Description copied from interface: MapGeneratorSpec
      Specifies the type of map that should be generated.
      Specified by:
      subtype in interface MapGeneratorSpec<K,V>
      Parameters:
      type - of collection to generate
      Returns:
      spec builder
    • size

      public MapGeneratorSpec<K,V> size(int size)
      Description copied from interface: MapGeneratorSpec
      Size of map to generate.
      Specified by:
      size in interface MapGeneratorSpec<K,V>
      Parameters:
      size - of map
      Returns:
      spec builder
    • minSize

      public MapGeneratorSpec<K,V> minSize(int size)
      Description copied from interface: MapGeneratorSpec
      Minimum size of map to generate.
      Specified by:
      minSize in interface MapGeneratorSpec<K,V>
      Parameters:
      size - minimum size (inclusive)
      Returns:
      spec builder
    • maxSize

      public MapGeneratorSpec<K,V> maxSize(int size)
      Description copied from interface: MapGeneratorSpec
      Maximum size of map to generate.
      Specified by:
      maxSize in interface MapGeneratorSpec<K,V>
      Parameters:
      size - maximum size (inclusive)
      Returns:
      spec builder
    • nullable

      public MapGeneratorSpec<K,V> nullable()
      Description copied from interface: MapGeneratorSpec
      Indicates that null value can be generated for the map.
      Specified by:
      nullable in interface MapGeneratorSpec<K,V>
      Returns:
      spec builder
    • nullableKeys

      public MapGeneratorSpec<K,V> nullableKeys()
      Description copied from interface: MapGeneratorSpec
      Indicates that null values can be generated for map keys.
      Specified by:
      nullableKeys in interface MapGeneratorSpec<K,V>
      Returns:
      spec builder
    • nullableValues

      public MapGeneratorSpec<K,V> nullableValues()
      Description copied from interface: MapGeneratorSpec
      Indicates that null values can be generated for map values.
      Specified by:
      nullableValues in interface MapGeneratorSpec<K,V>
      Returns:
      spec builder
    • with

      public MapGeneratorSpec<K,V> with(K key, V value)
      Description copied from interface: MapGeneratorSpec
      Adds given key/value pair to the generated map. Note that the entry is added after the map has been generated.

      Example:

      
        // will generate a map of size 5
        generate(field("someMap"), gen -> gen.map()
                .size(3)
                .with("key1", "value1")
                .with("key2", "value2")
       
      Specified by:
      with in interface MapGeneratorSpec<K,V>
      Parameters:
      key - to add
      value - to add
      Returns:
      spec builder
      See Also:
    • withKeys

      @SafeVarargs public final MapGeneratorSpec<K,V> withKeys(K... keys)
      Description copied from interface: MapGeneratorSpec
      Adds given keys to the map in the order they are provided.

      If the resulting map size is equal to the number of specified keys, then the map will contain only the specified keys and no randomly generated keys.

      Examples:

      
        // will generate a map of size 1 containing only "key1"
        generate(field("someMap"), gen -> gen.map().size(1).withKeys("key1", "key2")
      
        // will generate a map of size 2 containing "key1" and "key2"
        generate(field("someMap"), gen -> gen.map().size(2).withKeys("key1", "key2")
      
        // will generate a map of size 5 containing "key1", "key2", and 3 randomly generated keys
        generate(field("someMap"), gen -> gen.map().size(5).withKeys("key1", "key2")
       
      Specified by:
      withKeys in interface MapGeneratorSpec<K,V>
      Parameters:
      keys - to add
      Returns:
      spec builder
      See Also:
    • generate

      public Map<K,V> generate(Random random)
      Description copied from interface: Generator
      Returns a generated value.

      If this method produces random data, the data needs to be generated using the provided Random instance. This ensures generated values are reproducible for a given seed value.

      Specified by:
      generate in interface Generator<K>
      Parameters:
      random - provider for generating random values
      Returns:
      generated value or null if value is nullable, could not be generated, or generation is delegated to the engine
    • hints

      public Hints hints()
      Description copied from interface: Generator
      Hints provided by the generator to the engine.

      The most important hint for custom generators is AfterGenerate. This hint indicates whether the object created by this generator:

      • should be populated (for example, if it has null fields)
      • can be modified using selectors

      For example, setting the hint to AfterGenerate.POPULATE_NULLS will cause Instancio to populate null fields on the object returned by this generator:

      
         @Override
         public Hints hints() {
             return Hints.afterGenerate(AfterGenerate.POPULATE_NULLS);
         }
       

      If the action is not specified, default behaviour will be based on the AfterGenerate value configured in the Settings using the key Keys.AFTER_GENERATE_HINT.

      In addition, the following hints can be provided for populating data structures:

      Specified by:
      hints in interface Generator<K>
      Overrides:
      hints in class AbstractGenerator<Map<K,V>>
      Returns:
      hints from this generator to the engine
      See Also: