Package org.instancio
Interface ScopeableSelector
- All Superinterfaces:
ConvertibleToScope,GroupableSelector,LenientSelector,TargetSelector,WithinScope
- All Known Subinterfaces:
FieldSelectorBuilder,PredicateSelector,Selector,TypeSelectorBuilder
public interface ScopeableSelector
extends GroupableSelector, ConvertibleToScope, LenientSelector, WithinScope
Represents a selector that can be scoped using
within(Scope...scopes).- Since:
- 3.0.0
-
Method Summary
Modifier and TypeMethodDescriptionlenient()Marks this selector as lenient, which prevents the selector from producing "unused selector" error if it does not match any target.Specifies the scope for this selector in order to narrow down its target.Methods inherited from interface org.instancio.ConvertibleToScope
toScope
-
Method Details
-
lenient
GroupableSelector lenient()Marks this selector as lenient, which prevents the selector from producing "unused selector" error if it does not match any target. This provides an alternative toLenientModeApi.lenient(), which treats all selectors as lenient (not recommended).This method can be useful when using Instancio to create a generic
Model, or in a helper method for creating objects of arbitrary types.For example, the following method will set all
lastUpdatedfields to a date in the past:static <T> Model<T> baseModel(Class<T> klass) { TargetSelector lastUpdated = Select.fields() .ofType(Instant.class).named("lastUpdated").lenient(); return Instancio.of(klass) .generate(lastUpdated, gen -> gen.temporal().instant().past()) .toModel(); }Marking the selector as lenient will prevent the unused selector error if a given
klassdoes not have alastUpdatedfield.See also: Selector Strictness section of the User Guide.
- Specified by:
lenientin interfaceLenientSelector- Returns:
- a lenient selector
- Since:
- 4.4.0
- See Also:
-
within
Specifies the scope for this selector in order to narrow down its target.For example, given the following classes:
record Phone(String countryCode, String number) {} record Person(Phone home, Phone cell) {}setting
homeandcellphone numbers to different values would require differentiating between twofield(Phone::number)selectors. This can be achieved using scopes as follows:Scope homePhone = field(Person::home).toScope(); Scope cellPhone = field(Person::cell).toScope(); Person person = Instancio.of(Person.class) .set(field(Phone::number).within(homePhone), "777-88-99") .set(field(Phone::number).within(cellPhone), "123-45-67") .create();See Selector Scopes section of the user guide for details.
- Specified by:
withinin interfaceWithinScope- Parameters:
scopes- one or more scopes to apply- Returns:
- a selector with the specified scope
- Since:
- 3.0.0
-