Package org.instancio.generator.specs
Interface CollectionGeneratorSpec<T>
- Type Parameters:
T
- element type
- All Superinterfaces:
GeneratorSpec<Collection<T>>
,NullableGeneratorSpec<Collection<T>>
,SubtypeGeneratorSpec<Collection<T>>
- All Known Implementing Classes:
CollectionGenerator
,CollectionGeneratorSpecImpl
public interface CollectionGeneratorSpec<T>
extends NullableGeneratorSpec<Collection<T>>, SubtypeGeneratorSpec<Collection<T>>
Generator spec for collections.
-
Method Summary
Modifier and TypeMethodDescriptionmaxSize
(int size) Maximum size of collection to generate.minSize
(int size) Minimum size of collection to generate.nullable()
Indicates thatnull
value can be generated for the collection.Indicates thatnull
values can be generated for collection elements.size
(int size) Size of collection to generate.Specifies the type of collection that should be generated.unique()
Specifies that a collection containing unique elements should be generated.Adds given elements to the generated collection at random positions.
-
Method Details
-
size
Size of collection to generate.- Parameters:
size
- of collection- Returns:
- spec builder
-
minSize
Minimum size of collection to generate.- Parameters:
size
- minimum size (inclusive)- Returns:
- spec builder
-
maxSize
Maximum size of collection to generate.- Parameters:
size
- maximum size (inclusive)- Returns:
- spec builder
-
nullable
CollectionGeneratorSpec<T> nullable()Indicates thatnull
value can be generated for the collection.- Specified by:
nullable
in interfaceNullableGeneratorSpec<T>
- Returns:
- spec builder
-
nullableElements
CollectionGeneratorSpec<T> nullableElements()Indicates thatnull
values can be generated for collection elements.- Returns:
- spec builder
-
subtype
Specifies the type of collection that should be generated.- Specified by:
subtype
in interfaceSubtypeGeneratorSpec<T>
- Parameters:
type
- of collection to generate- Returns:
- spec builder
- Since:
- 1.4.0
-
unique
CollectionGeneratorSpec<T> unique()Specifies that a collection containing unique elements should be generated.Special care must be taken when using this method and
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.- Returns:
- spec builder
- Since:
- 2.8.0
-
with
Adds 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")
- Parameters:
elements
- to add- Returns:
- spec builder
-