I have a base entity which has a couple of properties and the concrete implementation with a couple of extra ones. On top of that I need the base class to access "self" of the child which I do through generics. Here is a simplified example of what I am trying:
@Data
@SuperBuilder(toBuilder = true)
@ToString(callSuper = true)
public abstract class Entity<ENTITY extends Entity<ENTITY, BUILDER>, BUILDER extends Entity.EntityBuilder<ENTITY, BUILDER, ENTITY, BUILDER>> implements Mutable<ENTITY> {
//...
public abstract ENTITY mutate(Consumer<Context<ENTITY, BUILDER>> context) {
var builder = createBuilder();
context.accept(new Context<>(self(), builder));
return builder.build();
}
protected abstract ENTITY self();
protected abstract BUILDER createBuilder();
}
@Data
@SuperBuilder(toBuilder = true)
public class SubEntity extends Entity<SubEntity, Entity.EntityBuilder<?, ?>> {
//...
@Override
protected SubEntity self() {
return this;
}
@Override
protected SubEntity.SubEntityBuilder<?, ?> createBuilder() {
return toBuilder();
}
}
Thanks in advance.