Package org.instancio.generator.hints
Class MapHint
java.lang.Object
org.instancio.generator.hints.MapHint
This hint is for generators that create maps.
The responsibility of a generator is to provide an instance of a map to the engine. The map may be partially populated (with expected data). The engine takes care of populating the map with additional entries.
Sample use case
The goal is to set up a map with a couple of expected objects and let the engine populate the rest of the map with random data.
This can be achieved as follows:
class PhonesGenerator implements Generator<Map<Long, Phone>> {
@Override
public Map<Long, Phone> generate(Random random) {
Map<Long, Phone> map = new HashMap<>();
map.put(1L, Phone.builder().number("111-222-3333").phoneType(PhoneType.CELL).build());
map.put(2L, Phone.builder().number("111-444-5555").build()); // phoneType is null, but will be populated
return map;
}
@Override
public Hints hints() {
return Hints.builder()
// tells the engine to populate null fields in the objects created in the generate() method
.afterGenerate(AfterGenerate.POPULATE_NULLS)
.with(MapHint.builder()
// number of additional entries to be generated and added to the map by the engine
.generateEntries(3)
.build())
.build();
}
}
// Generator usage
Person person = Instancio.of(Person.class)
.supply(field("phones"), new PhonesGenerator())
.create();
Map<Long, Phone> map = person.getPhones();
// Expected results:
assertThat(map).hasSize(5);
assertThat(map).extractingByKey(1L)
.matches(p -> p.getNumber().equals("111-222-3333") && p.getPhoneType() == PhoneType.CELL);
assertThat(map).extractingByKey(2L)
.matches(p -> p.getNumber().equals("111-444-5555") && p.getPhoneType() != null);
Summary of results:
- The generated map contains our custom objects as well as generated phone objects.
- The
phoneType
field of "111-444-5555" has been populated since theAfterGenerate.POPULATE_NULLS
was specified.
- Since:
- 2.0.0
- See Also:
-
Nested Class Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic MapHint.Builder
builder()
static MapHint
empty()
Returns an empty hint containing default values.int
Indicates the number of entries the engine should generate and insert into the map.boolean
Indicates whether map keys can benull
.boolean
Indicates whether map values can benull
.toString()
<K,
V> Map<K, V> Returns additional entries provided by the generator to the engine that are to be inserted into the map.<K> List<K>
withKeys()
Returns keys provided by the generator to the engine that are to be inserted into the map.
-
Method Details
-
empty
Returns an empty hint containing default values.- Returns:
- empty hint
- Since:
- 2.0.0
-
generateEntries
public int generateEntries()Indicates the number of entries the engine should generate and insert into the map.- Returns:
- number of entries to generate
- Since:
- 2.0.0
-
nullableMapKeys
public boolean nullableMapKeys()Indicates whether map keys can benull
.- Returns:
true
if keys are nullable,false
otherwise- Since:
- 2.0.0
-
nullableMapValues
public boolean nullableMapValues()Indicates whether map values can benull
.- Returns:
true
if values are nullable,false
otherwise- Since:
- 2.0.0
-
withEntries
Returns additional entries provided by the generator to the engine that are to be inserted into the map.- Type Parameters:
K
- key typeV
- value type- Returns:
- specific entries to be inserted into the map
- Since:
- 2.0.0
-
withKeys
Returns keys provided by the generator to the engine that are to be inserted into the map.- Type Parameters:
K
- key type- Returns:
- keys to be inserted into the map
- Since:
- 2.0.0
-
toString
-
builder
-