I am trying to use lombok's @Builder with inheritance and Jackson.
I was building things from https://reinhard.codes/2015/09/16/lomboks-builder-annotation-and-inheritance/, https://gist.github.com/pcarrier/14d3a8e249d804cfbdee and Builder pattern with inheritance
Here is what i have
UserInput.java
@JsonDeserialize(builder = UserInput.UserInputBuilder.class)
@Builder
@Data
public class UserInput {
private int userId;
private UsersChoice usersChoice;
private ChoiceAttributes choiceAttributes;
@JsonPOJOBuilder(withPrefix = "")
public static final class UserInputBuilder {
}
public enum UserChoice {
CHOICE1,
CHOICE2
}
}
Based on user choice, corresponding ChoiceAttributes builder should be used.
ChoiceAttributes.java
public abstract class ChoiceAttributes {
//nothing to do here
public static class ChoiceAttributesBuilder {
}
public static ChoiceAttributesBuilder getMeMyBuilderBasedOnUserChoice(UserChoice userChoice)
{
ChoiceAttributesBuilder choiceAttributesBuilder = null;
switch(userChoice){
case CHOICE1:
choiceAttributesBuilder = new ChoiceAttributesForChoice1.ChoiceAttributesForChoice1Builder(); //err!! stuck?
}
}
ChoiceAttributesForChoice1.java
@JsonDeserialize(builder = ChoiceAttributesForChoice1.ChoiceAttributesForChoice1Builder.class)
@Builder
@Data
public class ChoiceAttributesForChoice1 extends ChoiceAttributes {
private int x;
//and so on
@JsonPOJOBuilder(withPrefix = "")
public static class ChoiceAttributesForChoice1Builder extends ChoiceAttributesBuilder {
}
}
ChoiceAttributesForChoice2.java
@JsonDeserialize(builder = ChoiceAttributesForChoice2.ChoiceAttributesForChoice2Builder.class)
@Builder
@Data
public class ChoiceAttributesForChoice2 extends ChoiceAttributes {
private float y;
//and so on
@JsonPOJOBuilder(withPrefix = "")
public static class ChoiceAttributesForChoice2Builder extends ChoiceAttributesBuilder {
}
}
Is there a better way to achieve this? How can i build "UserInput" using its builder and ChoiceAttributes based on UsersChoice?