Package org.instancio

Interface PredicateSelector

All Superinterfaces:
ConvertibleToScope, DepthPredicateSelector, DepthSelector, GroupableSelector, ScopeableSelector, TargetSelector, WithinScope

public interface PredicateSelector extends DepthSelector, DepthPredicateSelector, ScopeableSelector
A selector for matching targets using predicates.
Since:
1.6.0
See Also:
  • Method Details

    • 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 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.

      Specified by:
      within in interface ScopeableSelector
      Specified by:
      within in interface WithinScope
      Parameters:
      scopes - one or more scopes to apply
      Returns:
      a selector with the specified scope
      Since:
      4.1.0
    • atDepth

      ScopeableSelector atDepth(Predicate<Integer> predicate)
      Restricts this selector's target(s) to a depth that satisfies the given predicate.
      Specified by:
      atDepth in interface DepthPredicateSelector
      Parameters:
      predicate - specifying the depth
      Returns:
      selector restricted to the specified depth
    • atDepth

      ScopeableSelector atDepth(int depth)
      Restricts this selector's target(s) to the specified depth.

      When a selector atDepth(N) is converted toScope(), the semantics of Selector.within(Scope...) method still hold, meaning: at depth equal to or greater than N.

      Specified by:
      atDepth in interface DepthSelector
      Parameters:
      depth - the depth at which selector applies
      Returns:
      selector restricted to the specified depth
    • toScope

      Scope representation of a selector.
      Specified by:
      toScope in interface ConvertibleToScope
      Returns:
      scope
      Since:
      4.2.0