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:
- Using a Getter and Setter:
forGetterSetter(Function, BiConsumer). - Using a Getter and Evolver:
forGetterEvolver(Function, BiFunction). Works similarly to the Getter/Setter, but this Evolver returns a new version of the parent entity, supporting immutable objects. - Using a field name:
forFieldName(Class, String). This will use reflection to get/set the field on the parent entity. Will automatically use getters, setters, and evolving methods if available.
- Since:
- 5.0.0
- Author:
- Mitchell Herrijgers
-
Method Summary
Modifier and TypeMethodDescriptionevolveParentBasedOnChildInput(P parentEntity, F childInput) Evolves the parent entity based on the provided child value.static <P,F> ChildEntityFieldDefinition <P, F> forFieldName(Class<P> parentClass, String fieldName) Creates a newFieldChildEntityFieldDefinitionfor the given field name.static <P,F> ChildEntityFieldDefinition <P, F> forGetterEvolver(Function<P, F> getter, BiFunction<P, F, P> evolver) Creates a newGetterEvolverChildEntityFieldDefinitionfor the given getter and evolver.static <P,F> ChildEntityFieldDefinition <P, F> forGetterSetter(Function<P, F> getter, BiConsumer<P, F> setter) Creates a newGetterSetterChildEntityFieldDefinitionfor the given getter and setter.getChildValue(P parentEntity) Returns the type of the field.
-
Method Details
-
evolveParentBasedOnChildInput
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
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 newFieldChildEntityFieldDefinitionfor 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
FieldChildEntityFieldDefinitionfor 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 newGetterEvolverChildEntityFieldDefinitionfor 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(ListchildEntities) { 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
GetterEvolverChildEntityFieldDefinitionfor 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 newGetterSetterChildEntityFieldDefinitionfor 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
GetterSetterChildEntityFieldDefinitionfor the given getter and setter.
-