Interface ChildEntityFieldDefinition<P,F>

Type Parameters:
P - The type of the parent entity.
F - The type of the field. This can be the type of the child entity or a collection of child entities.
All Known Implementing Classes:
FieldChildEntityFieldDefinition, GetterEvolverChildEntityFieldDefinition, GetterSetterChildEntityFieldDefinition

public interface ChildEntityFieldDefinition<P,F>
Functional interface describing how to get the child entity (or entity collection), and apply the evolved child entity (or entities in case of a collection) to the parent entity. The function can simply set a field or return a new instance of the parent entity with the evolved child entity. The value returned from this function will be used as the new parent entity and must return a non-null value.

There are three default ways to create an implementation:

Since:
5.0.0
Author:
Mitchell Herrijgers
  • Method Details

    • evolveParentBasedOnChildInput

      @Nonnull P evolveParentBasedOnChildInput(@Nonnull P parentEntity, @Nullable F childInput)
      Evolves the parent entity based on the provided child value. This can be a single entity, or a collection of entities. The evolver can return a new version of the parent entity, or it can simply set the field on the parent entity.
      Parameters:
      parentEntity - The parent entity to evolve.
      childInput - The child entity to use for evolution.
      Returns:
      The evolved parent entity.
    • getChildValue

      @Nullable F getChildValue(@Nonnull P parentEntity)
      Returns the type of the field.
      Parameters:
      parentEntity - The parent entity to get the child entities from.
      Returns:
      The type of the field.
    • forFieldName

      @Nonnull static <P, F> ChildEntityFieldDefinition<P,F> forFieldName(@Nonnull Class<P> parentClass, @Nonnull String fieldName)
      Creates a new FieldChildEntityFieldDefinition for the given field name. This will use reflection to get/set the field on the parent entity, and will automatically use getters, setters, and evolving methods if available.
      Type Parameters:
      P - The type of the parent entity.
      F - The type of the field.
      Parameters:
      parentClass - The class of the parent entity.
      fieldName - The name of the field to get/set on the parent entity.
      Returns:
      A new FieldChildEntityFieldDefinition for the given field name.
    • forGetterEvolver

      @Nonnull static <P, F> ChildEntityFieldDefinition<P,F> forGetterEvolver(@Nonnull Function<P,F> getter, @Nonnull BiFunction<P,F,P> evolver)
      Creates a new GetterEvolverChildEntityFieldDefinition for the given getter and evolver. The evolver will be used to create a new version of the parent entity on which the new child entities are set. The getter will be used to get the field from the parent entity.

      An example of a compatible method can be seen below:

           public record ParentEntity(List childEntities) {
               public ParentEntity evolveChildEntities(List childEntities) {
                   return new ParentEntity(childEntities);
               }
           }
       

      The field definition for this looks as follows:

           ChildEntityFieldDefinition.forGetterEvolver(
              ParentEntity::childEntities,
              ParentEntity::evolveChildEntities
           );
       
      Type Parameters:
      P - The type of the parent entity.
      F - The type of the field.
      Parameters:
      getter - The getter to get the field from the parent entity.
      evolver - The evolver to set the field on the parent entity.
      Returns:
      A new GetterEvolverChildEntityFieldDefinition for the given getter and setter.
    • forGetterSetter

      @Nonnull static <P, F> ChildEntityFieldDefinition<P,F> forGetterSetter(@Nonnull Function<P,F> getter, @Nonnull BiConsumer<P,F> setter)
      Creates a new GetterSetterChildEntityFieldDefinition for the given getter and setter. The setter will be used to set the field on the parent entity, and the setter will be used to set the field on the parent entity.
      Type Parameters:
      P - The type of the parent entity.
      F - The type of the field.
      Parameters:
      getter - The getter to get the field from the parent entity.
      setter - The setter to set the field on the parent entity.
      Returns:
      A new GetterSetterChildEntityFieldDefinition for the given getter and setter.