BAEL-16653 (#7691)
* added module 'design-patterns-behavioral' * added module 'design-patterns-structural' * added module 'design-patterns-creational' * added module 'design-patterns-architectural' * added module 'design-patterns-functional' * moved facade code examples from design-patterns to design-patterns-structural * moved singleton examples from design-patterns to design-patterns-creational * moved bridge examples from design-patterns to design-patterns-structural * moved creational examples from design-patterns to design-patterns-creational * moved observer examples from design-patterns to design-patterns-behavioral * moved flyweight examples from design-patterns to design-patterns-creational * moved service locator examples from design-patterns to design-patterns-architectural * moved double checked locking singleton examples from design-patterns to design-patterns-creational * moved composite examples from design-patterns to design-patterns-structural * moved visitor examples from design-patterns to design-patterns-behavioral * moved dao examples from design-patterns to design-patterns-architectural * moved interpreter examples from design-patterns to design-patterns-behavioral * moved state examples from design-patterns to design-patterns-behavioral * moved decorator examples from design-patterns to design-patterns-structural * renamed LogerUtil to LoggerUtil * moved template method examples from design-patterns to design-patterns-behavioral * moved chain of responsibility examples from design-patterns to design-patterns-behavioral * moved command examples from design-patterns to design-patterns-behavioral * moved constructor vs static factory method examples from design-patterns to design-patterns-creational * moved adapter examples from design-patterns to design-patterns-structural * moved currying examples from design-patterns to design-patterns-functional * moved proxy examples from design-patterns to design-patterns-structural * moved persistence.xml from design-patterns to design-patterns-architectural * deleted empty module: design-patterns * moved mediator examples from design-patterns-2 to design-patterns-behavioral * moved null object examples from design-patterns-2 to design-patterns-behavioral * moved null check examples from design-patterns to design-patterns-behavioral * moved freebuilder examples from design-patterns-2 to design-patterns-creational * added module design-patterns-behavioral-2 * moved memento examples from design-patterns-2 to design-patterns-behavioral-2 * removed empty module design-patterns-2 * changed http to https in readmes in modules design-patterns-*
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
package com.baeldung.currying;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.Objects;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class Letter {
|
||||
private String returningAddress;
|
||||
private String insideAddress;
|
||||
private LocalDate dateOfLetter;
|
||||
private String salutation;
|
||||
private String body;
|
||||
private String closing;
|
||||
|
||||
Letter(String returningAddress, String insideAddress, LocalDate dateOfLetter, String salutation, String body, String closing) {
|
||||
this.returningAddress = returningAddress;
|
||||
this.insideAddress = insideAddress;
|
||||
this.dateOfLetter = dateOfLetter;
|
||||
this.salutation = salutation;
|
||||
this.body = body;
|
||||
this.closing = closing;
|
||||
}
|
||||
|
||||
Letter(String salutation, String body) {
|
||||
this(null, null, LocalDate.now(), salutation, body, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
Letter letter = (Letter) o;
|
||||
return Objects.equals(returningAddress, letter.returningAddress) &&
|
||||
Objects.equals(insideAddress, letter.insideAddress) &&
|
||||
Objects.equals(dateOfLetter, letter.dateOfLetter) &&
|
||||
Objects.equals(salutation, letter.salutation) &&
|
||||
Objects.equals(body, letter.body) &&
|
||||
Objects.equals(closing, letter.closing);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(returningAddress, insideAddress, dateOfLetter, salutation, body, closing);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Letter{" + "returningAddress='" + returningAddress + '\'' + ", insideAddress='" + insideAddress + '\''
|
||||
+ ", dateOfLetter=" + dateOfLetter + ", salutation='" + salutation + '\'' + ", body='" + body + '\''
|
||||
+ ", closing='" + closing + '\'' + '}';
|
||||
}
|
||||
|
||||
static Letter createLetter(String salutation, String body) {
|
||||
return new Letter(salutation, body);
|
||||
}
|
||||
|
||||
static BiFunction<String, String, Letter> SIMPLE_LETTER_CREATOR = //
|
||||
(salutation, body) -> new Letter(salutation, body);
|
||||
|
||||
static Function<String, Function<String, Letter>> SIMPLE_CURRIED_LETTER_CREATOR = //
|
||||
saluation -> body -> new Letter(saluation, body);
|
||||
|
||||
static Function<String, Function<String, Function<LocalDate, Function<String, Function<String, Function<String, Letter>>>>>> LETTER_CREATOR = //
|
||||
returnAddress
|
||||
-> closing
|
||||
-> dateOfLetter
|
||||
-> insideAddress
|
||||
-> salutation
|
||||
-> body
|
||||
-> new Letter(returnAddress, insideAddress, dateOfLetter, salutation, body, closing);
|
||||
|
||||
static AddReturnAddress builder() {
|
||||
return returnAddress
|
||||
-> closing
|
||||
-> dateOfLetter
|
||||
-> insideAddress
|
||||
-> salutation
|
||||
-> body
|
||||
-> new Letter(returnAddress, insideAddress, dateOfLetter, salutation, body, closing);
|
||||
}
|
||||
|
||||
interface AddReturnAddress {
|
||||
Letter.AddClosing withReturnAddress(String returnAddress);
|
||||
}
|
||||
|
||||
interface AddClosing {
|
||||
Letter.AddDateOfLetter withClosing(String closing);
|
||||
}
|
||||
|
||||
interface AddDateOfLetter {
|
||||
Letter.AddInsideAddress withDateOfLetter(LocalDate dateOfLetter);
|
||||
}
|
||||
|
||||
interface AddInsideAddress {
|
||||
Letter.AddSalutation withInsideAddress(String insideAddress);
|
||||
}
|
||||
|
||||
interface AddSalutation {
|
||||
Letter.AddBody withSalutation(String salutation);
|
||||
}
|
||||
|
||||
interface AddBody {
|
||||
Letter withBody(String body);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.baeldung.currying;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
import static com.baeldung.currying.Letter.LETTER_CREATOR;
|
||||
import static com.baeldung.currying.Letter.SIMPLE_CURRIED_LETTER_CREATOR;
|
||||
import static com.baeldung.currying.Letter.SIMPLE_LETTER_CREATOR;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class LetterUnitTest {
|
||||
private static final String BODY = "BODY";
|
||||
private static final String SALUTATION = "SALUTATION";
|
||||
private static final String RETURNING_ADDRESS = "RETURNING ADDRESS";
|
||||
private static final String INSIDE_ADDRESS = "INSIDE ADDRESS";
|
||||
private static final LocalDate DATE_OF_LETTER = LocalDate.of(2013, 3, 1);
|
||||
private static final String CLOSING = "CLOSING";
|
||||
private static final Letter SIMPLE_LETTER = new Letter(SALUTATION, BODY);
|
||||
private static final Letter LETTER = new Letter(RETURNING_ADDRESS, INSIDE_ADDRESS, DATE_OF_LETTER, SALUTATION, BODY, CLOSING);
|
||||
|
||||
@Test
|
||||
public void whenStaticCreateMethodIsCalled_thenaSimpleLetterIsCreated() {
|
||||
assertEquals(SIMPLE_LETTER, Letter.createLetter(SALUTATION, BODY));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStaticBiFunctionIsCalled_thenaSimpleLetterIsCreated() {
|
||||
assertEquals(SIMPLE_LETTER, SIMPLE_LETTER_CREATOR.apply(SALUTATION, BODY));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStaticSimpleCurriedFunctionIsCalled_thenaSimpleLetterIsCreated() {
|
||||
assertEquals(SIMPLE_LETTER, SIMPLE_CURRIED_LETTER_CREATOR.apply(SALUTATION).apply(BODY));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStaticCurriedFunctionIsCalled_thenaLetterIsCreated() {
|
||||
assertEquals(LETTER, LETTER_CREATOR
|
||||
.apply(RETURNING_ADDRESS)
|
||||
.apply(CLOSING)
|
||||
.apply(DATE_OF_LETTER)
|
||||
.apply(INSIDE_ADDRESS)
|
||||
.apply(SALUTATION)
|
||||
.apply(BODY));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStaticBuilderIsCalled_thenaLetterIsCreated() {
|
||||
assertEquals(LETTER, Letter.builder()
|
||||
.withReturnAddress(RETURNING_ADDRESS)
|
||||
.withClosing(CLOSING)
|
||||
.withDateOfLetter(DATE_OF_LETTER)
|
||||
.withInsideAddress(INSIDE_ADDRESS)
|
||||
.withSalutation(SALUTATION)
|
||||
.withBody(BODY));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user