Package org.instancio

Interface TypeSelectorBuilder

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

public interface TypeSelectorBuilder extends DepthSelector, DepthPredicateSelector, ScopeableSelector
A builder for constructing predicate-based type selectors.

An instance of the builder can be obtained using Select.types(). Other methods from this class can be chained to form logical AND relationships, for example:


 types().of(BaseEntity.class).annotated(Entity.class).annotated(Table.class)
 

will match BaseEntity including its subtypes, that are annotated with @Entity and @Table.

Since:
1.6.0
See Also:
  • Method Details

    • of

      Matches specified type, including subtypes. For example, selecting types of(Collection.class} will match List, Set, etc.
      Parameters:
      type - to match
      Returns:
      selector builder
      Since:
      1.6.0
    • annotated

      <A extends Annotation> TypeSelectorBuilder annotated(Class<? extends A> annotation)
      Matches types annotated with the specified annotation, ignoring inherited annotations.

      Note: this method only matches annotations declared on classes. To select annotated fields, use FieldSelectorBuilder.annotated(Class).

      The method can be chained to require multiple annotations.

      Type Parameters:
      A - annotation type
      Parameters:
      annotation - declared annotation to match
      Returns:
      selector builder
      Since:
      1.6.0
    • excluding

      TypeSelectorBuilder excluding(Class<?> type)
      Excludes specified class from matching.

      The method can be chained to exclude multiple types.

      Parameters:
      type - type to exclude
      Returns:
      selector builder
      Since:
      1.6.0
    • atDepth

      ScopeableSelector atDepth(int depth)
      Restricts this selector's targets to the specified depth. The selector will only apply to targets at the specified depth.

      When a selector with atDepth(N) is converted using toScope(), the semantics of the Selector.within(Scope...) method still apply, meaning that the selection applies to targets at depth equal to or greater than N.

      The root object is considered to be at depth 0.

      Specified by:
      atDepth in interface DepthSelector
      Parameters:
      depth - the depth at which the selector should apply
      Returns:
      a selector restricted to the specified depth
      Since:
      2.14.0
      See Also:
    • atDepth

      ScopeableSelector atDepth(Predicate<Integer> atDepth)
      Restricts this selector's targets to a depth that satisfies the given predicate.

      For example, a predicate atDepth(depth -> depth == 1) will restrict the selector to targets at depth 1 only.

      The root object is considered to be at depth 0.

      Specified by:
      atDepth in interface DepthPredicateSelector
      Parameters:
      atDepth - the predicate specifying the acceptable depth
      Returns:
      a selector restricted to the specified depth
      Since:
      2.14.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 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.2.0
    • toScope

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