Last active
December 4, 2015 22:44
-
-
Save jedws/8926f3a712f8f513432c to your computer and use it in GitHub Desktop.
fork of https://gist.github.com/luuuis/571ea46b38aa45a00bfe (to demonstrate some name changes) which is Forked from https://gist.github.com/dwijnand/3741951 and adapted to JSD (originally: http://blog.rafaelferreira.net/2008/07/type-safe-builder-pattern-in-scala.html)
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
package com.atlassian.crowd.plugin.usermanagement.rest.controller; | |
import com.atlassian.fugue.Option; | |
import static com.atlassian.crowd.plugin.usermanagement.rest.controller.Visibility.Internal; | |
import static com.atlassian.fugue.Option.some; | |
class Main { | |
public static void main(String[] args) { | |
CreateComment comment1 = CreateComment.build( | |
CreateComment.builder() | |
.withIssue("ISSUE-1") | |
.withBody("comment 1") | |
); | |
CreateComment comment2 = CreateComment.build( | |
CreateComment.builder() | |
.withIssue("ISSUE-2") | |
.withBody("comment 2") | |
.withVisibility(Internal) | |
); | |
CreateComment doesNotCompile = CreateComment.build( | |
CreateComment.builder().withBody("i'm missing an issue key!") | |
); | |
} | |
} | |
enum Visibility {Public, Internal} | |
public final class CreateComment { | |
public final String issue; | |
public final String body; | |
public final Option<Visibility> visibility; | |
public CreateComment(Builder<Yes, Yes> builder) { | |
issue = builder.issue; | |
body = builder.body; | |
visibility = builder.visibility; | |
} | |
public static Builder<No, No> builder() { | |
return new Builder<No, No>(null, null, null); | |
} | |
public static CreateComment build(Builder<Yes, Yes> builder) { | |
return new CreateComment(builder); | |
} | |
public static final class Builder<HasIssue, HasBody> { | |
private final String issue; | |
private final String body; | |
private final Option<Visibility> visibility; | |
private Builder(String issue, String body, Option<Visibility> visibility) { | |
this.issue = issue; | |
this.body = body; | |
this.visibility = visibility; | |
} | |
public Builder<Yes, HasBody> withIssue(String newBrand) { | |
return new Builder<Yes, HasBody>(newBrand, body, visibility); | |
} | |
public Builder<HasIssue, Yes> withBody(String newIsDouble) { | |
return new Builder<HasIssue, Yes>(issue, newIsDouble, visibility); | |
} | |
public Builder<HasIssue, HasBody> withVisibility(Visibility newVisibility) { | |
return new Builder<HasIssue, HasBody>(issue, body, some(newVisibility)); | |
} | |
} | |
} | |
final class Yes {} | |
final class No {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment