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> NullableGeneratorSpec<Map<K,
,V>> SubtypeGeneratorSpec<Map<K,
V>>
- Direct Known Subclasses:
MapGeneratorSpecImpl
-
Field Summary
Modifier and TypeFieldDescriptionprotected boolean
protected Class<?>
protected int
protected int
protected boolean
protected boolean
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionReturns the public API method name of the generator spec.hints()
Hints provided by the generator to the engine.maxSize
(int size) Maximum size of map to generate.minSize
(int size) Minimum size of map to generate.nullable()
Indicates thatnull
value can be generated.nullable
(boolean isNullable) Indicates thatnull
values can be generated for map keys.Indicates thatnull
values can be generated for map values.size
(int size) Size of map to generate.Specifies the type of map that should be generated.tryGenerateNonNull
(Random random) Makes the best effort to return a non-null value.Adds given key/value pair to the generated map.final MapGeneratorSpec<K,
V> Adds given keys to the map in the order they are provided.Methods inherited from class org.instancio.internal.generator.AbstractGenerator
generate, getContext, isNullable
-
Field Details
-
minSize
protected int minSize -
maxSize
protected int maxSize -
nullableKeys
protected boolean nullableKeys -
nullableValues
protected boolean nullableValues -
mapType
-
isDelegating
protected boolean isDelegating
-
-
Constructor Details
-
MapGenerator
-
-
Method Details
-
apiMethod
Description copied from class:AbstractGenerator
Returns the public API method name of the generator spec. The returned name is used for reporting validation errors.- Specified by:
apiMethod
in classAbstractGenerator<Map<K,
V>> - Returns:
- spec name if defined, or
null
otherwise
-
subtype
Description copied from interface:MapGeneratorSpec
Specifies the type of map that should be generated.- Specified by:
subtype
in interfaceMapGeneratorSpec<K,
V> - Specified by:
subtype
in interfaceSubtypeGeneratorSpec<K>
- Parameters:
type
- of collection to generate- Returns:
- spec builder
-
size
Description copied from interface:MapGeneratorSpec
Size of map to generate.- Specified by:
size
in interfaceMapGeneratorSpec<K,
V> - Parameters:
size
- of map- Returns:
- spec builder
-
minSize
Description copied from interface:MapGeneratorSpec
Minimum size of map to generate.- Specified by:
minSize
in interfaceMapGeneratorSpec<K,
V> - Parameters:
size
- minimum size (inclusive)- Returns:
- spec builder
-
maxSize
Description copied from interface:MapGeneratorSpec
Maximum size of map to generate.- Specified by:
maxSize
in interfaceMapGeneratorSpec<K,
V> - Parameters:
size
- maximum size (inclusive)- Returns:
- spec builder
-
nullable
Description copied from interface:NullableGeneratorSpec
Indicates thatnull
value can be generated.- Specified by:
nullable
in interfaceMapGeneratorSpec<K,
V> - Specified by:
nullable
in interfaceNullableGeneratorSpec<K>
- Overrides:
nullable
in classAbstractGenerator<Map<K,
V>> - Returns:
- spec builder
-
nullable
- Overrides:
nullable
in classAbstractGenerator<Map<K,
V>>
-
nullableKeys
Description copied from interface:MapGeneratorSpec
Indicates thatnull
values can be generated for map keys.- Specified by:
nullableKeys
in interfaceMapGeneratorSpec<K,
V> - Returns:
- spec builder
-
nullableValues
Description copied from interface:MapGeneratorSpec
Indicates thatnull
values can be generated for map values.- Specified by:
nullableValues
in interfaceMapGeneratorSpec<K,
V> - Returns:
- spec builder
-
with
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 interfaceMapGeneratorSpec<K,
V> - Parameters:
key
- to addvalue
- to add- Returns:
- spec builder
- See Also:
-
withKeys
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 interfaceMapGeneratorSpec<K,
V> - Parameters:
keys
- to add- Returns:
- spec builder
- See Also:
-
tryGenerateNonNull
Description copied from class:AbstractGenerator
Makes the best effort to return a non-null value. However, in certain cases this method will produce anull
.- Specified by:
tryGenerateNonNull
in classAbstractGenerator<Map<K,
V>> - Parameters:
random
- for generating the value- Returns:
- generated value, either a null or non-null
-
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 populatenull
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 theSettings
using the keyKeys.AFTER_GENERATE_HINT
.In addition, the following hints can be provided for populating data structures:
- should be populated (for example, if it has
-