Skip to content

Commit

Permalink
BAEL-6227: fluent interfaces code
Browse files Browse the repository at this point in the history
  • Loading branch information
etrandafir93 committed Mar 12, 2023
1 parent fb7a8ae commit c3e6f4c
Show file tree
Hide file tree
Showing 10 changed files with 394 additions and 0 deletions.
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));
}
}
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));
}

}
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);
}
}
}
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>";
}
}
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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.baeldung.fluentinterface.components;

public interface HtmlElement {
String html();
}
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;
}
}
}
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;
}
}
}
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);
}
}
Loading

0 comments on commit c3e6f4c

Please sign in to comment.