forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fb7a8ae
commit c3e6f4c
Showing
10 changed files
with
394 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
...design-patterns-behavioral-2/src/main/java/com/baeldung/fluentinterface/HtmlDocument.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.baeldung.fluentinterface; | ||
|
||
import static java.util.Arrays.stream; | ||
import static java.util.stream.Collectors.joining; | ||
|
||
public class HtmlDocument { | ||
private final String content; | ||
|
||
private HtmlDocument(String html) { | ||
this.content = html; | ||
} | ||
|
||
public HtmlDocument() { | ||
this(""); | ||
} | ||
|
||
public String html() { | ||
return String.format("<html>%s</html>", content); | ||
} | ||
|
||
public HtmlDocument header(String header) { | ||
return new HtmlDocument(String.format("%s <h1>%s</h1>", content, header)); | ||
} | ||
|
||
public HtmlDocument secondaryHeader(String header) { | ||
return new HtmlDocument(String.format("%s <h2>%s</h2>", content, header)); | ||
} | ||
|
||
public HtmlDocument paragraph(String paragraph) { | ||
return new HtmlDocument(String.format("%s <p>%s</p>", content, paragraph)); | ||
} | ||
|
||
public HtmlDocument horizontalLine() { | ||
return new HtmlDocument(String.format("%s <hr>", content)); | ||
} | ||
|
||
public HtmlDocument orderedList(String... items) { | ||
String listItems = stream(items).map(el -> String.format("<li>%s</li>", el)).collect(joining()); | ||
return new HtmlDocument(String.format("%s <ol>%s</ol>", content, listItems)); | ||
} | ||
|
||
public HtmlDocument unorderedList(String... items) { | ||
String listItems = stream(items).map(el -> String.format("<li>%s</li>", el)).collect(joining()); | ||
return new HtmlDocument(String.format("%s <ul>%s</ul>", content, listItems)); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...n-patterns-behavioral-2/src/main/java/com/baeldung/fluentinterface/LargeHtmlDocument.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.baeldung.fluentinterface; | ||
|
||
import com.baeldung.fluentinterface.components.HtmlElement; | ||
import com.baeldung.fluentinterface.components.HtmlHeader; | ||
import com.baeldung.fluentinterface.components.HtmlList; | ||
|
||
import static java.lang.String.format; | ||
import static java.util.Arrays.stream; | ||
import static java.util.stream.Collectors.joining; | ||
|
||
public class LargeHtmlDocument { | ||
private final String content; | ||
|
||
private LargeHtmlDocument(String html) { | ||
this.content = html; | ||
} | ||
|
||
public LargeHtmlDocument() { | ||
this(""); | ||
} | ||
|
||
public String html() { | ||
return format("<html>%s</html>", content); | ||
} | ||
|
||
public LargeHtmlDocument head(HtmlElement head) { | ||
return new LargeHtmlDocument(format("%s <head>%s</head>", content, head.html())); | ||
} | ||
public LargeHtmlDocument body(HtmlElement body) { | ||
return new LargeHtmlDocument(format("%s <body>%s</body>", content, body.html())); | ||
} | ||
public LargeHtmlDocument footer(HtmlElement footer) { | ||
return new LargeHtmlDocument(format("%s <footer>%s</footer>", content, footer.html())); | ||
} | ||
|
||
private LargeHtmlDocument append(String html) { | ||
return new LargeHtmlDocument(format("%s %s", content, html)); | ||
} | ||
|
||
} |
65 changes: 65 additions & 0 deletions
65
...modules/design-patterns-behavioral-2/src/main/java/com/baeldung/fluentinterface/User.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.baeldung.fluentinterface; | ||
|
||
public class User { | ||
private String firstName; | ||
private String lastName; | ||
private String email; | ||
private String username; | ||
private Long id; | ||
|
||
public String name() { | ||
return firstName + " " + lastName; | ||
} | ||
|
||
public User(String firstName, String lastName, String email, String username, Long id) { | ||
this.firstName = firstName; | ||
this.lastName = lastName; | ||
this.email = email; | ||
this.username = username; | ||
this.id = id; | ||
} | ||
|
||
public static Builder builder() { | ||
return new Builder(); | ||
} | ||
|
||
public static class Builder { | ||
private String firstName; | ||
private String lastName; | ||
private String email; | ||
private String username; | ||
private Long id; | ||
|
||
private Builder() { | ||
} | ||
|
||
public Builder firstName(String firstName) { | ||
this.firstName = firstName; | ||
return this; | ||
} | ||
|
||
public Builder lastName(String lastName) { | ||
this.lastName = lastName; | ||
return this; | ||
} | ||
|
||
public Builder email(String email) { | ||
this.email = email; | ||
return this; | ||
} | ||
|
||
public Builder username(String username) { | ||
this.username = username; | ||
return this; | ||
} | ||
|
||
public Builder id(Long id) { | ||
this.id = id; | ||
return this; | ||
} | ||
|
||
public User build() { | ||
return new User(firstName, lastName, email, username, id); | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...ns-behavioral-2/src/main/java/com/baeldung/fluentinterface/components/HorizontalLine.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.baeldung.fluentinterface.components; | ||
|
||
public class HorizontalLine implements HtmlElement { | ||
@Override | ||
public String html() { | ||
return "<hr>"; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...-patterns-behavioral-2/src/main/java/com/baeldung/fluentinterface/components/HtmlDiv.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.baeldung.fluentinterface.components; | ||
|
||
public class HtmlDiv implements HtmlElement { | ||
|
||
private final String content; | ||
|
||
public HtmlDiv(String content) { | ||
this.content = content; | ||
} | ||
|
||
public HtmlDiv() { | ||
this.content = ""; | ||
} | ||
|
||
public HtmlDiv append(HtmlElement element) { | ||
return new HtmlDiv(content + element.html()); | ||
} | ||
public HtmlDiv text(String text) { | ||
return new HtmlDiv(content + text); | ||
} | ||
public HtmlDiv paragraph(String text) { | ||
return new HtmlDiv(content + text); | ||
} | ||
|
||
@Override | ||
public String html() { | ||
return String.format("<div>%s</div>", content); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...terns-behavioral-2/src/main/java/com/baeldung/fluentinterface/components/HtmlElement.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.baeldung.fluentinterface.components; | ||
|
||
public interface HtmlElement { | ||
String html(); | ||
} |
34 changes: 34 additions & 0 deletions
34
...tterns-behavioral-2/src/main/java/com/baeldung/fluentinterface/components/HtmlHeader.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.baeldung.fluentinterface.components; | ||
|
||
public class HtmlHeader implements HtmlElement { | ||
|
||
private final Type type; | ||
private final String value; | ||
|
||
public HtmlHeader(Type type, String value) { | ||
this.type = type; | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public String html() { | ||
return String.format("<%s>%s</%s>", type.tag(), value, type.tag()); | ||
} | ||
|
||
public enum Type { | ||
PRIMARY("h1"), | ||
SECONDARY("h2"), | ||
THIRD("h3"), | ||
FOURTH("h4"); | ||
|
||
private final String tag; | ||
|
||
Type(String tag) { | ||
this.tag = tag; | ||
} | ||
|
||
public String tag() { | ||
return tag; | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...patterns-behavioral-2/src/main/java/com/baeldung/fluentinterface/components/HtmlList.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.baeldung.fluentinterface.components; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static java.lang.String.format; | ||
import static java.util.stream.Collectors.joining; | ||
|
||
public class HtmlList implements HtmlElement { | ||
|
||
private final Type type; | ||
private final List<String> items; | ||
|
||
public HtmlList(Type type, String... items) { | ||
this.type = type; | ||
this.items = Arrays.asList(items); | ||
} | ||
|
||
@Override | ||
public String html() { | ||
String listItems = items.stream().map(el -> format("<li>%s</li>", el)).collect(joining()); | ||
return String.format("<%s>%s</%s>", type.tag(), listItems, type.tag()); | ||
} | ||
|
||
public enum Type { | ||
ORDERED("ol"), | ||
UNORDERED("ul"); | ||
|
||
private final String tag; | ||
|
||
Type(String tag) { | ||
this.tag = tag; | ||
} | ||
|
||
public String tag() { | ||
return tag; | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...patterns-behavioral-2/src/main/java/com/baeldung/fluentinterface/components/HtmlSpan.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.baeldung.fluentinterface.components; | ||
|
||
public class HtmlSpan implements HtmlElement { | ||
|
||
private final String content; | ||
|
||
public HtmlSpan(String content) { | ||
this.content = content; | ||
} | ||
|
||
public HtmlSpan() { | ||
this.content = ""; | ||
} | ||
|
||
public HtmlSpan append(HtmlElement element) { | ||
return new HtmlSpan(content + element.html()); | ||
} | ||
|
||
public HtmlSpan paragraph(String text) { | ||
return new HtmlSpan(content + text); | ||
} | ||
|
||
@Override | ||
public String html() { | ||
return String.format("<span>%s</span>", content); | ||
} | ||
} |
Oops, something went wrong.