Created
January 4, 2018 22:05
-
-
Save culturedsys/0a9418f0b2bb0ba00f7d10879368a34d to your computer and use it in GitHub Desktop.
This file contains 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
public class BuilderExample { | |
public static void main(String... args) { | |
Client client = Client | |
.build(Client.parameters().withRegion("us-east-1").withCredentials(new Credentials("username"))); | |
client.action(); | |
} | |
public static abstract class Parameter<T> { | |
public abstract boolean isPresent(); | |
} | |
public static class Absent<T> extends Parameter<T> { | |
@Override | |
public boolean isPresent() { | |
return false; | |
} | |
} | |
public static class Present<T> extends Parameter<T> { | |
private T value; | |
public Present(T value) { | |
this.value = value; | |
} | |
@Override | |
public boolean isPresent() { | |
return true; | |
} | |
public T get() { | |
return value; | |
} | |
} | |
public static class Client { | |
private String region; | |
private Credentials credentials; | |
private boolean flag; | |
private Client(String region, Credentials credentials, boolean flag) { | |
this.region = region; | |
this.credentials = credentials; | |
this.flag = flag; | |
} | |
public void action() { | |
System.out.println("Running in region " + region); | |
System.out.println("with username " + credentials.getUsername()); | |
System.out.println("flag is " + flag); | |
} | |
public static Client build( | |
Parameters<Present<String>, Present<Credentials>, ? extends Parameter<Boolean>> parameters) { | |
String region = parameters.getRegion().get(); | |
Credentials credentials = parameters.getCredentials().get(); | |
boolean flag = (parameters.getFlag().isPresent()) ? ((Present<Boolean>) parameters.getFlag()).get() : false; | |
return new Client(region, credentials, flag); | |
} | |
public static Parameters<Absent<String>, Absent<Credentials>, Absent<Boolean>> parameters() { | |
return new Parameters<>(new Absent<>(), new Absent<>(), new Absent<>()); | |
} | |
public static class Parameters<RegionType extends Parameter<String>, CredentialsType extends Parameter<Credentials>, FlagType extends Parameter<Boolean>> { | |
private RegionType region; | |
private CredentialsType credentials; | |
private FlagType flag; | |
public Parameters(RegionType region, CredentialsType credentials, FlagType flag) { | |
this.region = region; | |
this.credentials = credentials; | |
this.flag = flag; | |
} | |
public Parameters<Present<String>, CredentialsType, FlagType> withRegion(String region) { | |
return new Parameters<>(new Present<>(region), credentials, flag); | |
} | |
public Parameters<RegionType, Present<Credentials>, FlagType> withCredentials(Credentials credentials) { | |
return new Parameters<>(region, new Present<>(credentials), flag); | |
} | |
public Parameters<RegionType, CredentialsType, Present<Boolean>> withFlag(boolean flag) { | |
return new Parameters<>(region, credentials, new Present<>(flag)); | |
} | |
public RegionType getRegion() { | |
return region; | |
} | |
public CredentialsType getCredentials() { | |
return credentials; | |
} | |
public FlagType getFlag() { | |
return flag; | |
} | |
} | |
} | |
public static class Credentials { | |
private String username; | |
public Credentials(String username) { | |
this.username = username; | |
} | |
public String getUsername() { | |
return username; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment