Created
January 8, 2018 20:51
-
-
Save nhojpatrick/5a05ea543615e264d06e2c0494db8175 to your computer and use it in GitHub Desktop.
Generics Issue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package tld.examples.builders; | |
| public abstract class AbstractEntity { | |
| protected String varAbstractEntity; | |
| public AbstractEntity() { | |
| } | |
| public AbstractEntity(final AbstractEntityBuilder abstractEntityBuilder) { | |
| this.varAbstractEntity = abstractEntityBuilder.entity.varAbstractEntity; | |
| } | |
| public String getVarAbstractEntity() { | |
| return this.varAbstractEntity; | |
| } | |
| public static class AbstractEntityBuilder< | |
| B extends AbstractEntityBuilder, | |
| E extends AbstractEntity> { | |
| protected final E entity; | |
| public AbstractEntityBuilder(final E abstractEntity) { | |
| this.entity = abstractEntity; | |
| } | |
| public B withVarAbstractEntity(final String varAbstractEntity) { | |
| this.entity.varAbstractEntity = varAbstractEntity; | |
| return (B) this; | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package tld.examples.builders; | |
| public abstract class AbstractMiddleEntity | |
| extends AbstractEntity { | |
| protected String varAbstractMiddleEntity; | |
| public AbstractMiddleEntity() { | |
| super(); | |
| } | |
| public AbstractMiddleEntity(final AbstractMiddleEntityBuilder abstractMiddleEntityBuilder) { | |
| super(abstractMiddleEntityBuilder); | |
| // this.varAbstractMiddleEntity = abstractMiddleEntityBuilder.entity.varAbstractMiddleEntity; // why won't this line see varAbstractMiddleEntity | |
| } | |
| public String getVarAbstractMiddleEntity() { | |
| return this.varAbstractMiddleEntity; | |
| } | |
| public static class AbstractMiddleEntityBuilder< | |
| B extends AbstractMiddleEntityBuilder, | |
| E extends AbstractMiddleEntity> | |
| extends AbstractEntityBuilder<B, E> { | |
| public AbstractMiddleEntityBuilder(final E abstractEntity) { | |
| super(abstractEntity); | |
| } | |
| public B withVarAbstractMiddleEntity(final String varAbstractMiddleEntity) { | |
| this.entity.varAbstractMiddleEntity = varAbstractMiddleEntity; | |
| return (B) this; | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package tld.examples.builders; | |
| public class AlphaEntity | |
| extends AbstractEntity { | |
| private String varAlphaEntityPre; | |
| private String varAlphaEntityPost; | |
| private AlphaEntity() { | |
| super(); | |
| } | |
| public AlphaEntity(final AlphaEntityBuilder alphaEntityBuilder) { | |
| super(alphaEntityBuilder); | |
| this.varAlphaEntityPre = alphaEntityBuilder.entity.varAlphaEntityPre; | |
| this.varAlphaEntityPost = alphaEntityBuilder.entity.varAlphaEntityPost; | |
| } | |
| public String getVarAlphaEntityPre() { | |
| return this.varAlphaEntityPre; | |
| } | |
| public String getVarAlphaEntityPost() { | |
| return this.varAlphaEntityPost; | |
| } | |
| public static class AlphaEntityBuilder | |
| extends AbstractEntityBuilder<AlphaEntityBuilder, AlphaEntity> { | |
| public AlphaEntityBuilder() { | |
| super(new AlphaEntity()); | |
| } | |
| public AlphaEntityBuilder withVarAlphaEntityPre(final String varAlphaEntityPre) { | |
| this.entity.varAlphaEntityPre = varAlphaEntityPre; | |
| return this; | |
| } | |
| public AlphaEntityBuilder withVarAlphaEntityPost(final String varAlphaEntityPost) { | |
| this.entity.varAlphaEntityPost = varAlphaEntityPost; | |
| return this; | |
| } | |
| public AlphaEntity build() { | |
| return new AlphaEntity(this); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package tld.examples.builders; | |
| public class AlphaEntityTest { | |
| public static void main(final String[] args) { | |
| testAlphaEntity(); | |
| } | |
| public static void testAlphaEntity() { | |
| System.out.println("Alpha Post"); | |
| final AlphaEntity.AlphaEntityBuilder alphaEntityBuilder = new AlphaEntity.AlphaEntityBuilder() | |
| .withVarAlphaEntityPre("Alpha.AlphaEntityPre") | |
| .withVarAbstractEntity("Alpha.AbstractEntity") | |
| .withVarAlphaEntityPost("Alpha.AlphaEntityPost"); | |
| final AlphaEntity alphaEntity = alphaEntityBuilder.build(); | |
| System.out.println(alphaEntity.getVarAbstractEntity()); | |
| System.out.println(alphaEntity.getVarAlphaEntityPre()); | |
| System.out.println(alphaEntity.getVarAlphaEntityPost()); | |
| System.out.println("Alpha Post"); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package tld.examples.builders; | |
| public class BravoEntity | |
| extends AbstractMiddleEntity { | |
| private String varBravoEntityPre; | |
| private String varBravoEntityPost; | |
| private BravoEntity() { | |
| super(); | |
| } | |
| public BravoEntity(final BravoEntityBuilder bravoEntityBuilder) { | |
| super(bravoEntityBuilder); | |
| this.varBravoEntityPre = bravoEntityBuilder.entity.varBravoEntityPre; | |
| this.varBravoEntityPost = bravoEntityBuilder.entity.varBravoEntityPost; | |
| } | |
| public String getVarBravoEntityPre() { | |
| return this.varBravoEntityPre; | |
| } | |
| public String getVarBravoEntityPost() { | |
| return this.varBravoEntityPost; | |
| } | |
| public static class BravoEntityBuilder | |
| extends AbstractMiddleEntityBuilder<BravoEntityBuilder, BravoEntity> { | |
| public BravoEntityBuilder() { | |
| super(new BravoEntity()); | |
| } | |
| public BravoEntityBuilder withVarBravoEntityPre(final String varBravoEntityPre) { | |
| this.entity.varBravoEntityPre = varBravoEntityPre; | |
| return this; | |
| } | |
| public BravoEntityBuilder withVarBravoEntityPost(final String varBravoEntityPost) { | |
| this.entity.varBravoEntityPost = varBravoEntityPost; | |
| return this; | |
| } | |
| public BravoEntity build() { | |
| return new BravoEntity(this); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package tld.examples.builders; | |
| public class BravoEntityTest { | |
| public static void main(final String[] args) { | |
| testBravoEntity(); | |
| } | |
| public static void testBravoEntity() { | |
| System.out.println("Bravo Pre"); | |
| final BravoEntity.BravoEntityBuilder bravoEntityBuilder = new BravoEntity.BravoEntityBuilder() | |
| .withVarBravoEntityPre("Bravo.BravoEntityPre") | |
| .withVarAbstractEntity("Bravo.AbstractEntity") | |
| .withVarBravoEntityPost("Bravo.BravoEntityPost") | |
| .withVarAbstractMiddleEntity("Alpha.AbstractMiddleEntity"); | |
| final BravoEntity bravoEntity = bravoEntityBuilder.build(); | |
| System.out.println(bravoEntity.getVarAbstractEntity()); | |
| System.out.println(bravoEntity.getVarAbstractMiddleEntity()); | |
| System.out.println(bravoEntity.getVarBravoEntityPre()); | |
| System.out.println(bravoEntity.getVarBravoEntityPost()); | |
| System.out.println("Bravo Post"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment