Package org.instancio.generators
Interface Generators
- All Superinterfaces:
CommonGeneratorSpecs
Defines all built-in generators available via the
InstancioApi.generate(TargetSelector, GeneratorSpecProvider).- Since:
- 1.0.1
-
Method Summary
Modifier and TypeMethodDescription<T> ArrayGeneratorSpec<T> array()Generator for arrays.atomic()Provides access to atomic generators.booleans()Generator forBooleanvalues.bytes()Generator forBytevalues.chars()Generator forCharactervalues.checksum()Provides access to checksum generators.<T> CollectionGeneratorSpec<T> Generator for collections.doubles()Generator forDoublevalues.<T> EmitGeneratorSpec<T> emit()Emits provided items to a selector's target.<E extends Enum<E>>
EnumGeneratorSpec<E> Generator for enum values.<E extends Enum<E>>
EnumSetGeneratorSpec<E> Generator forEnumSet.finance()Provides access to finance-related generators.floats()Generator forFloatvalues.hash()Generator for various types of hashes.id()Provides access to identifier generators.ints()Generator forIntegervalues.intSeq()AnIntegersequence generator.io()Provides access to IO generators.longs()Generator forLongvalues.longSeq()ALongsequence generator.<K,V> MapGeneratorSpec <K, V> map()Generator for maps.math()Provides generators forjava.mathclasses.net()Provides generators forjava.netclasses.nio()Provides access to NIO generators.<T> OneOfCollectionGeneratorSpec<T> oneOf(Collection<T> choices) Picks a random value from the given choices.<T> OneOfArrayGeneratorSpec<T> oneOf(T... choices) Picks a random value from the given choices.<T> OptionalGeneratorSpec<T> optional()Generator forOptional.shorts()Generator forShortvalues.spatial()Provides access to spatial data type related generators.string()Generator forStringvalues.temporal()Provides access to temporal generators.text()Provides access to text generators.
-
Method Details
-
string
StringGeneratorSpec string()Generator forStringvalues.- Specified by:
stringin interfaceCommonGeneratorSpecs- Returns:
- API builder reference
- Since:
- 1.0.1
-
booleans
BooleanGeneratorSpec booleans()Generator forBooleanvalues.- Specified by:
booleansin interfaceCommonGeneratorSpecs- Returns:
- API builder reference
- Since:
- 2.0.0
-
chars
CharacterGeneratorSpec chars()Generator forCharactervalues.- Specified by:
charsin interfaceCommonGeneratorSpecs- Returns:
- API builder reference
- Since:
- 2.0.0
-
bytes
NumberGeneratorSpec<Byte> bytes()Generator forBytevalues.- Specified by:
bytesin interfaceCommonGeneratorSpecs- Returns:
- API builder reference
- Since:
- 1.0.1
-
shorts
NumberGeneratorSpec<Short> shorts()Generator forShortvalues.- Specified by:
shortsin interfaceCommonGeneratorSpecs- Returns:
- API builder reference
- Since:
- 1.0.1
-
ints
NumberGeneratorSpec<Integer> ints()Generator forIntegervalues.- Specified by:
intsin interfaceCommonGeneratorSpecs- Returns:
- API builder reference
- Since:
- 1.0.1
-
intSeq
NumericSequenceGeneratorSpec<Integer> intSeq()AnIntegersequence generator.- Specified by:
intSeqin interfaceCommonGeneratorSpecs- Returns:
- API builder reference
- Since:
- 2.13.0
-
longs
NumberGeneratorSpec<Long> longs()Generator forLongvalues.- Specified by:
longsin interfaceCommonGeneratorSpecs- Returns:
- API builder reference
- Since:
- 1.0.1
-
longSeq
NumericSequenceGeneratorSpec<Long> longSeq()ALongsequence generator.- Specified by:
longSeqin interfaceCommonGeneratorSpecs- Returns:
- API builder reference
- Since:
- 2.13.0
-
floats
NumberGeneratorSpec<Float> floats()Generator forFloatvalues.- Specified by:
floatsin interfaceCommonGeneratorSpecs- Returns:
- API builder reference
- Since:
- 1.0.1
-
doubles
NumberGeneratorSpec<Double> doubles()Generator forDoublevalues.- Specified by:
doublesin interfaceCommonGeneratorSpecs- Returns:
- API builder reference
- Since:
- 1.0.1
-
enumOf
Generator for enum values.- Specified by:
enumOfin interfaceCommonGeneratorSpecs- Type Parameters:
E- enum type- Parameters:
enumClass- type of enum to generate- Returns:
- API builder reference
-
math
MathGenerators math()Provides generators forjava.mathclasses.- Specified by:
mathin interfaceCommonGeneratorSpecs- Returns:
- API builder reference
-
net
NetGenerators net()Provides generators forjava.netclasses.- Specified by:
netin interfaceCommonGeneratorSpecs- Returns:
- API builder reference
- Since:
- 2.3.0
-
oneOf
Picks a random value from the given choices.- Specified by:
oneOfin interfaceCommonGeneratorSpecs- Type Parameters:
T- element type- Parameters:
choices- to choose from- Returns:
- API builder reference
- Since:
- 1.0.1
-
oneOf
Picks a random value from the given choices.- Specified by:
oneOfin interfaceCommonGeneratorSpecs- Type Parameters:
T- element type- Parameters:
choices- to choose from- Returns:
- API builder reference
- Since:
- 1.0.1
-
optional
Generator forOptional.- Type Parameters:
T- the type of value- Returns:
- API builder reference
- Since:
- 2.14.0
-
array
Generator for arrays.- Type Parameters:
T- array component type- Returns:
- API builder reference
- Since:
- 1.0.1
-
collection
Generator for collections.- Type Parameters:
T- element type- Returns:
- API builder reference
- Since:
- 1.0.1
-
map
Generator for maps.- Type Parameters:
K- key typeV- value type- Returns:
- API builder reference
- Since:
- 1.0.1
-
enumSet
Generator forEnumSet.- Type Parameters:
E- enum type- Parameters:
enumClass- contained by the generated enum set- Returns:
- API builder reference
- Since:
- 2.0.0
-
emit
Emits provided items to a selector's target. The main use case for this generator us to customise fields of collection elements.For example, the snippet below generates a list of 7 orders with different statuses: 1 received, 1 shipped, 3 completed, and 2 cancelled.
List<Order> orders = Instancio.ofList(Order.class) .size(7) .generate(field(Order::getStatus), gen -> gen.emit() .items(OrderStatus.RECEIVED, OrderStatus.SHIPPED) .item(OrderStatus.COMPLETED, 3) .item(OrderStatus.CANCELLED, 2)) .create();Note that the number of items being emitted is equal to the collection size. If there are not enough items to emit for a given collection size, then by default, random values will be generated. This behaviour can be customised using the following methods:
EmitGeneratorSpec.whenEmptyEmitNull()EmitGeneratorSpec.whenEmptyEmitRandom()(default behaviour)EmitGeneratorSpec.whenEmptyRecycle()(reuse items)EmitGeneratorSpec.whenEmptyThrowException()
If the selector target is a group, then each selector will have its own copy of items, for example:
// Given record FooBar(String foo, String bar) {} TargetSelector selectorGroup = Select.all( field(FooBar::foo), field(FooBar::bar) ); // When FooBar result = Instancio.of(FooBar.class) .generate(selectorGroup, gen -> gen.emit().items("BAZ")) .create(); // Output: FooBar[foo=BAZ, bar=BAZ]Warning: special care must be taken to ensure that the selector associated with
emit()matches only the expected target and nothing else. Failure to do so may produce unexpected results.- Type Parameters:
T- the type to emit- Returns:
- emitting generator
- Since:
- 2.12.0
-
atomic
-
checksum
ChecksumGenerators checksum()Provides access to checksum generators.- Specified by:
checksumin interfaceCommonGeneratorSpecs- Returns:
- API builder reference
- Since:
- 2.16.0
-
io
IoGenerators io()Provides access to IO generators.- Specified by:
ioin interfaceCommonGeneratorSpecs- Returns:
- API builder reference
- Since:
- 2.2.0
-
nio
NioGenerators nio()Provides access to NIO generators.- Specified by:
nioin interfaceCommonGeneratorSpecs- Returns:
- API builder reference
- Since:
- 2.1.0
-
temporal
TemporalGenerators temporal()Provides access to temporal generators.- Specified by:
temporalin interfaceCommonGeneratorSpecs- Returns:
- API builder reference
-
text
TextGenerators text()Provides access to text generators.- Specified by:
textin interfaceCommonGeneratorSpecs- Returns:
- API builder reference
-
id
IdGenerators id()Provides access to identifier generators.- Specified by:
idin interfaceCommonGeneratorSpecs- Returns:
- API builder reference
- Since:
- 2.11.0
-
hash
HashGeneratorSpec hash()Generator for various types of hashes.- Specified by:
hashin interfaceCommonGeneratorSpecs- Returns:
- API builder reference
- Since:
- 2.11.0
-
finance
FinanceGenerators finance()Provides access to finance-related generators.- Specified by:
financein interfaceCommonGeneratorSpecs- Returns:
- API builder reference
- Since:
- 2.11.0
-
spatial
SpatialGenerators spatial()Provides access to spatial data type related generators.- Specified by:
spatialin interfaceCommonGeneratorSpecs- Returns:
- API builder reference
- Since:
- 4.4.0
-