Class InstancioApiImpl<T>
- All Implemented Interfaces:
InstancioApi<T>
- Direct Known Subclasses:
InstancioOfClassApiImpl
-
Constructor Summary
ConstructorDescriptionInstancioApiImpl
(Class<T> klass) InstancioApiImpl
(Model<T> model) InstancioApiImpl
(TypeTokenSupplier<T> typeToken) -
Method Summary
Modifier and TypeMethodDescriptionprotected void
addTypeParameters
(Class<?>... type) asResult()
Returns aResult
containing the created object and seed value used to generate its values.create()
Creates a new instance of a class and populates it with data.<V> InstancioApi<T>
generate
(TargetSelector selector, GeneratorSpecProvider<V> gen) Generates a random value for a field or class using a built-in generator.ignore
(TargetSelector selector) Specifies that a class or field should be ignored.lenient()
Disables strict mode in which unused selectors trigger an error.<V> InstancioApi<T>
onComplete
(TargetSelector selector, OnCompleteCallback<V> callback) A callback that gets invoked after an object has been fully populated.<V> InstancioApi<T>
set
(TargetSelector selector, V value) Sets a value to matching selector targets.stream()
Creates an infinite stream of distinct, fully populated objects.subtype
(TargetSelector selector, Class<?> subtype) Maps target field or class to the given subtype.<V> InstancioApi<T>
supply
(TargetSelector selector, Supplier<V> supplier) Supplies an object using aSupplier
to matching selector targets.<V> InstancioApi<T>
supply
(TargetSelector selector, Generator<V> generator) Supplies an object using aGenerator
to matching selector targets.toModel()
Creates a model containing all the information for populating a class.withNullable
(TargetSelector selector) Specifies that a field or class is nullable.withSeed
(long seed) Sets the seed value for the random number generator.withSettings
(Settings settings) Override defaultSettings
for generating values.
-
Constructor Details
-
InstancioApiImpl
-
InstancioApiImpl
-
InstancioApiImpl
-
-
Method Details
-
addTypeParameters
-
ignore
Description copied from interface:InstancioApi
Specifies that a class or field should be ignored.Example:
Person person = Instancio.of(Person.class) .ignore(field("pets")) // Person.pets field .ignore(field(Address.class, "phoneNumbers")) // Address.phoneNumbers field .ignore(allStrings()) .create();
will create a fully populated person, but will ignore the
address
field and all string fields.- Specified by:
ignore
in interfaceInstancioApi<T>
- Parameters:
selector
- for fields and/or classes this method should be applied to- Returns:
- API builder reference
-
generate
Description copied from interface:InstancioApi
Generates a random value for a field or class using a built-in generator.Example:
Person person = Instancio.of(Person.class) .generate(field("age"), gen -> gen.ints().min(18).max(100)) .generate(field("name"), gen -> gen.string().min(5).allowEmpty()) .generate(field(Address.class, "phoneNumbers"), gen -> gen.collection().minSize(5)) .generate(field(Address.class, "city"), gen -> gen.oneOf("Burnaby", "Vancouver", "Richmond")) .create();
- Specified by:
generate
in interfaceInstancioApi<T>
- Type Parameters:
V
- type of object to generate- Parameters:
selector
- for fields and/or classes this method should be applied togen
- provider of customisable built-in generators (also known as specs)- Returns:
- API builder reference
-
onComplete
Description copied from interface:InstancioApi
A callback that gets invoked after an object has been fully populated.Example:
// Sets countryCode field on all instances of Phone to the specified value Person person = Instancio.of(Person.class) .onComplete(all(Phone.class), (Phone phone) -> phone.setCountryCode("+1")) .create();
- Specified by:
onComplete
in interfaceInstancioApi<T>
- Type Parameters:
V
- type of object handled by the callback- Parameters:
selector
- for fields and/or classes this method should be applied tocallback
- to invoke after object has been populated- Returns:
- API builder reference
-
set
Description copied from interface:InstancioApi
Sets a value to matching selector targets.Example: if a
Person
has aList<PhoneNumber>
, the following will set all generated phone numbers' country codes to "+1".Person person = Instancio.of(Person.class) .supply(field(PhoneNumber.class, "phoneNumbers"), "+1") .create();
For supplying random values, see
InstancioApi.supply(TargetSelector, Generator)
.- Specified by:
set
in interfaceInstancioApi<T>
- Type Parameters:
V
- type of the value- Parameters:
selector
- for fields and/or classes this method should be applied tovalue
- value to set- Returns:
- API builder reference
- See Also:
-
supply
Description copied from interface:InstancioApi
Supplies an object using aGenerator
to matching selector targets.Example:
Person person = Instancio.of(Person.class) .supply(field(Address.class, "phoneNumbers"), random -> List.of( // Generate phone numbers with a random country code, either US or Mexico new PhoneNumber(random.from("+1", "+52"), "123-55-66"), new PhoneNumber(random.from("+1", "+52"), "123-77-88"))) .create();
Instancio may or may not further populate the generated object, for example filling in
null
fields. This behaviour is controlled by theAfterGenerate
hint specified byGenerator.hints()
. Refer to theGenerator.hints()
Javadoc for details.- Specified by:
supply
in interfaceInstancioApi<T>
- Type Parameters:
V
- type of the value to generate- Parameters:
selector
- for fields and/or classes this method should be applied togenerator
- that will provide the values- Returns:
- API builder reference
- See Also:
-
supply
Description copied from interface:InstancioApi
Supplies an object using aSupplier
to matching selector targets.Example:
Person person = Instancio.of(Person.class) .supply(all(LocalDateTime.class), () -> LocalDateTime.now()) // set all dates to current time .supply(field("fullName"), () -> "Homer Simpson") // set Person.fullName .supply(field(Address.class, "phoneNumbers"), () -> List.of( // set Address.phoneNumbers new PhoneNumber("+1", "123-45-67"), new PhoneNumber("+1", "345-67-89"))) .create();
Note: Instancio will not
- populate or modify objects supplied by this method
- apply other
set()
,supply()
, orgenerate()
} methods with matching selectors to the supplied object
If you require the supplied object to be populated and/or selectors to be applied, use the
InstancioApi.supply(TargetSelector, Generator)
method.- Specified by:
supply
in interfaceInstancioApi<T>
- Type Parameters:
V
- type of the value to generate- Parameters:
selector
- for fields and/or classes this method should be applied tosupplier
- providing the value for given selector- Returns:
- API builder reference
- See Also:
-
subtype
Description copied from interface:InstancioApi
Maps target field or class to the given subtype. This can be used in the following cases:- to specify an implementation for interfaces or abstract classes
- to override default implementations used by Instancio
When Instancio encounters an interface or an abstract type it is not aware of (for example, that is not part of the JDK), it will not be able to instantiate it. This method can be used to specify an implementation to use in such cases. For example:
WidgetContainer container = Instancio.of(WidgetContainer.class) .subtype(all(AbstractWidget.class), ConcreteWidget.class) .create();
Override default implementations
By default, Instancio uses certain defaults for collection classes, for example
ArrayList
forList
. If an alternative implementation is required, this method allows to specify it:Person person = Instancio.of(Person.class) .subtype(all(List.class), LinkedList.class) .create();
will use the
LinkedList
implementation for allList
s.- Specified by:
subtype
in interfaceInstancioApi<T>
- Parameters:
selector
- for fields and/or classes this method should be applied tosubtype
- to map the selector to- Returns:
- API builder reference
-
withSeed
Description copied from interface:InstancioApi
Sets the seed value for the random number generator. If the seed is not specified, a random seed will be used. Specifying the seed is useful for reproducing test results. By specifying the seed value, the same random data will be generated again.Example:
// Generates a different UUID each time UUID result = Instancio.create(UUID.class); // Generates the same UUID each time UUID result = Instancio.of(UUID.class) .withSeed(1234) .create();
- Specified by:
withSeed
in interfaceInstancioApi<T>
- Parameters:
seed
- for the random number generator- Returns:
- API builder reference
-
withNullable
Description copied from interface:InstancioApi
Specifies that a field or class is nullable. By default, Instancio assigns non-null values. If marked as nullable, Instancio will randomly assign either a null or non-null value to fields of this type.Example:
Person person = Instancio.of(Person.class) .withNullable(allStrings()) .withNullable(field(Address.class)) .create();
- Specified by:
withNullable
in interfaceInstancioApi<T>
- Parameters:
selector
- for fields and/or classes this method should be applied to- Returns:
- API builder reference
-
withSettings
Description copied from interface:InstancioApi
Override defaultSettings
for generating values. TheSettings
class supports various parameters, such as collection sizes, string lengths, numeric ranges, and so on. For a list of overridable settings, refer to theKeys
class.- Specified by:
withSettings
in interfaceInstancioApi<T>
- Parameters:
settings
- to use- Returns:
- API builder reference
- See Also:
-
lenient
Description copied from interface:InstancioApi
Disables strict mode in which unused selectors trigger an error. In lenient mode unused selectors are simply ignored.This method is a shorthand for:
Example example = Instancio.of(Example.class) .withSettings(Settings.create().set(Keys.MODE, Mode.LENIENT)) .create();
- Specified by:
lenient
in interfaceInstancioApi<T>
- Returns:
- API builder reference
-
toModel
Description copied from interface:InstancioApi
Creates a model containing all the information for populating a class.The model can be useful when class population needs to be customised and the customisations need to be re-used in different parts of the code.
Example:
Model<Person> personModel = Instancio.of(Person.class) .supply(field("fullName"), () -> "Jane Doe") .toModel(); // Re-use the model to create instances of Person class // without duplicating the model's details Person person = Instancio.of(personModel).create();
Since the internal data of the model is not part of the public API, the
Model
interface does not contain any methods.- Specified by:
toModel
in interfaceInstancioApi<T>
- Returns:
- a model containing all the details
-
create
Description copied from interface:InstancioApi
Creates a new instance of a class and populates it with data.Example:
Person person = Instancio.of(Person.class).create();
The returned object will have all its fields populated with random data, including collection and array fields.
- Specified by:
create
in interfaceInstancioApi<T>
- Returns:
- a fully populated object
-
asResult
Description copied from interface:InstancioApi
Returns aResult
containing the created object and seed value used to generate its values. The seed value can be used to reproduce the same object again.- Specified by:
asResult
in interfaceInstancioApi<T>
- Returns:
- result containing the created object
-
stream
Description copied from interface:InstancioApi
Creates an infinite stream of distinct, fully populated objects.Example:
List<Person> persons = Instancio.of(Person.class) .stream() .limit(5) .collect(Collectors.toList());
- Specified by:
stream
in interfaceInstancioApi<T>
- Returns:
- an infinite stream of distinct, populated objects
-