Package org.instancio

Interface WithinScope

All Known Subinterfaces:
FieldSelectorBuilder, PredicateSelector, ScopeableSelector, Selector, TypeSelectorBuilder

public interface WithinScope
Adds ability to narrow down selector targets using Scope.
Since:
4.1.0
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    within(Scope... scopes)
    Specifies the scope for this selector in order to narrow down its target.
  • Method Details

    • within

      TargetSelector within(Scope... scopes)
      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 home and cell phone numbers to different values would require differentiating between two field(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.

      Parameters:
      scopes - one or more scopes to apply
      Returns:
      a selector with the specified scope
      Since:
      4.1.0