Class CollectionGenerator<T>
- All Implemented Interfaces:
Generator<Collection<T>>,GeneratorSpec<Collection<T>>,CollectionGeneratorSpec<T>,NullableGeneratorSpec<Collection<T>>,SubtypeGeneratorSpec<Collection<T>>
- Direct Known Subclasses:
CollectionGeneratorSpecImpl
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected Class<?>protected booleanprotected intprotected intprotected booleanprotected boolean -
Constructor Summary
Constructors -
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 collection to generate.minSize(int size) Minimum size of collection to generate.nullable()Indicates thatnullvalue can be generated.nullable(boolean isNullable) Indicates thatnullvalues can be generated for collection elements.size(int size) Size of collection to generate.Specifies the type of collection that should be generated.protected Collection<T>tryGenerateNonNull(Random random) Makes the best effort to return a non-null value.unique()Specifies that a collection containing unique elements should be generated.final CollectionGeneratorSpec<T>Adds given elements to the generated collection at random positions.Methods inherited from class org.instancio.internal.generator.AbstractGenerator
generate, getContext, isNullable
-
Field Details
-
minSize
protected int minSize -
maxSize
protected int maxSize -
nullableElements
protected boolean nullableElements -
unique
protected boolean unique -
withElements
-
collectionType
-
isDelegating
protected boolean isDelegating
-
-
Constructor Details
-
CollectionGenerator
-
-
Method Details
-
apiMethod
Description copied from class:AbstractGeneratorReturns the public API method name of the generator spec. The returned name is used for reporting validation errors.- Specified by:
apiMethodin classAbstractGenerator<Collection<T>>- Returns:
- spec name if defined, or
nullotherwise
-
size
Description copied from interface:CollectionGeneratorSpecSize of collection to generate.- Specified by:
sizein interfaceCollectionGeneratorSpec<T>- Parameters:
size- of collection- Returns:
- spec builder
-
minSize
Description copied from interface:CollectionGeneratorSpecMinimum size of collection to generate.- Specified by:
minSizein interfaceCollectionGeneratorSpec<T>- Parameters:
size- minimum size (inclusive)- Returns:
- spec builder
-
maxSize
Description copied from interface:CollectionGeneratorSpecMaximum size of collection to generate.- Specified by:
maxSizein interfaceCollectionGeneratorSpec<T>- Parameters:
size- maximum size (inclusive)- Returns:
- spec builder
-
nullable
Description copied from interface:NullableGeneratorSpecIndicates thatnullvalue can be generated.- Specified by:
nullablein interfaceCollectionGeneratorSpec<T>- Specified by:
nullablein interfaceNullableGeneratorSpec<T>- Overrides:
nullablein classAbstractGenerator<Collection<T>>- Returns:
- spec builder
-
nullable
- Overrides:
nullablein classAbstractGenerator<Collection<T>>
-
nullableElements
Description copied from interface:CollectionGeneratorSpecIndicates thatnullvalues can be generated for collection elements.- Specified by:
nullableElementsin interfaceCollectionGeneratorSpec<T>- Returns:
- spec builder
-
subtype
Description copied from interface:CollectionGeneratorSpecSpecifies the type of collection that should be generated.- Specified by:
subtypein interfaceCollectionGeneratorSpec<T>- Specified by:
subtypein interfaceSubtypeGeneratorSpec<T>- Parameters:
type- of collection to generate- Returns:
- spec builder
-
unique
Description copied from interface:CollectionGeneratorSpecSpecifies that a collection containing unique elements should be generated.Special care must be taken when using this method and
CollectionGeneratorSpec.size(int). Consider the following example where the favourite numbers field is of typeSet<Integer>:Person person = Instancio.of(Person.class) .generate(allInts(), gen -> gen.ints().range(1, 5)) .generate(field(Person::getFavouriteNumbers), gen -> gen.collection().unique().size(10)) .create();Since the integer range is restricted to
1..5, it is impossible to generate a collection of 10 unique elements. In this case, a collection of size 5 will be generated.- Specified by:
uniquein interfaceCollectionGeneratorSpec<T>- Returns:
- spec builder
-
with
Description copied from interface:CollectionGeneratorSpecAdds given elements to the generated collection at random positions. Note that the elements are added after the collection has been generated.Example:
// will generate a collection of size 5 generate(field("someList"), gen -> gen.collection().size(3).with("element1", "element2")- Specified by:
within interfaceCollectionGeneratorSpec<T>- Parameters:
elements- to add- Returns:
- spec builder
-
tryGenerateNonNull
Description copied from class:AbstractGeneratorMakes the best effort to return a non-null value. However, in certain cases this method will produce anull.- Specified by:
tryGenerateNonNullin classAbstractGenerator<Collection<T>>- Parameters:
random- for generating the value- Returns:
- generated value, either a null or non-null
-
hints
Description copied from interface:GeneratorHints 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
nullfields) - can be modified using selectors
For example, setting the hint to
AfterGenerate.POPULATE_NULLSwill cause Instancio to populatenullfields 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
AfterGeneratevalue configured in theSettingsusing the keyKeys.AFTER_GENERATE_HINT.In addition, the following hints can be provided for populating data structures:
- Specified by:
hintsin interfaceGenerator<T>- Overrides:
hintsin classAbstractGenerator<Collection<T>>- Returns:
- hints from this generator to the engine
- See Also:
- should be populated (for example, if it has
-