Skip to content

Commit 9578f0d

Browse files
Jan Hauerashleyfrieze
authored andcommitted
BAEL-2400: Adds examples for currying
1 parent c9ac3be commit 9578f0d

3 files changed

Lines changed: 169 additions & 0 deletions

File tree

‎patterns/design-patterns/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@
1919
- [The Command Pattern in Java](http://www.baeldung.com/java-command-pattern)
2020
- [Java Constructors vs Static Factory Methods](https://www.baeldung.com/java-constructors-vs-static-factory-methods)
2121
- [The Adapter Pattern in Java](https://www.baeldung.com/java-adapter-pattern)
22+
- [Currying in Java](https://baeldung.com/currying-in-java)
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package com.baeldung.currying;
2+
3+
import java.time.LocalDate;
4+
import java.util.Objects;
5+
import java.util.function.BiFunction;
6+
import java.util.function.Function;
7+
8+
public class Letter {
9+
private String returningAddress;
10+
private String insideAddress;
11+
private LocalDate dateOfLetter;
12+
private String salutation;
13+
private String body;
14+
private String closing;
15+
16+
Letter(String returningAddress, String insideAddress, LocalDate dateOfLetter, String salutation, String body, String closing) {
17+
this.returningAddress = returningAddress;
18+
this.insideAddress = insideAddress;
19+
this.dateOfLetter = dateOfLetter;
20+
this.salutation = salutation;
21+
this.body = body;
22+
this.closing = closing;
23+
}
24+
25+
Letter(String salutation, String body) {
26+
this(null, null, LocalDate.now(), salutation, body, null);
27+
}
28+
29+
@Override
30+
public boolean equals(Object o) {
31+
if (this == o) {
32+
return true;
33+
}
34+
if (o == null || getClass() != o.getClass()) {
35+
return false;
36+
}
37+
Letter letter = (Letter) o;
38+
return Objects.equals(returningAddress, letter.returningAddress) &&
39+
Objects.equals(insideAddress, letter.insideAddress) &&
40+
Objects.equals(dateOfLetter, letter.dateOfLetter) &&
41+
Objects.equals(salutation, letter.salutation) &&
42+
Objects.equals(body, letter.body) &&
43+
Objects.equals(closing, letter.closing);
44+
}
45+
46+
@Override
47+
public int hashCode() {
48+
return Objects.hash(returningAddress, insideAddress, dateOfLetter, salutation, body, closing);
49+
}
50+
51+
@Override
52+
public String toString() {
53+
return "Letter{" + "returningAddress='" + returningAddress + '\'' + ", insideAddress='" + insideAddress + '\''
54+
+ ", dateOfLetter=" + dateOfLetter + ", salutation='" + salutation + '\'' + ", body='" + body + '\''
55+
+ ", closing='" + closing + '\'' + '}';
56+
}
57+
58+
static Letter createLetter(String salutation, String body) {
59+
return new Letter(salutation, body);
60+
}
61+
62+
static BiFunction<String, String, Letter> SIMPLE_LETTER_CREATOR = //
63+
(salutation, body) -> new Letter(salutation, body);
64+
65+
static Function<String, Function<String, Letter>> SIMPLE_CURRIED_LETTER_CREATOR = //
66+
saluation -> body -> new Letter(saluation, body);
67+
68+
static Function<String, Function<String, Function<LocalDate, Function<String, Function<String, Function<String, Letter>>>>>> LETTER_CREATOR = //
69+
returnAddress
70+
-> closing
71+
-> dateOfLetter
72+
-> insideAddress
73+
-> salutation
74+
-> body
75+
-> new Letter(returnAddress, insideAddress, dateOfLetter, salutation, body, closing);
76+
77+
static AddReturnAddress builder() {
78+
return returnAddress
79+
-> closing
80+
-> dateOfLetter
81+
-> insideAddress
82+
-> salutation
83+
-> body
84+
-> new Letter(returnAddress, insideAddress, dateOfLetter, salutation, body, closing);
85+
}
86+
87+
interface AddReturnAddress {
88+
Letter.AddClosing withReturnAddress(String returnAddress);
89+
}
90+
91+
interface AddClosing {
92+
Letter.AddDateOfLetter withClosing(String closing);
93+
}
94+
95+
interface AddDateOfLetter {
96+
Letter.AddInsideAddress withDateOfLetter(LocalDate dateOfLetter);
97+
}
98+
99+
interface AddInsideAddress {
100+
Letter.AddSalutation withInsideAddress(String insideAddress);
101+
}
102+
103+
interface AddSalutation {
104+
Letter.AddBody withSalutation(String salutation);
105+
}
106+
107+
interface AddBody {
108+
Letter withBody(String body);
109+
}
110+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.baeldung.currying;
2+
3+
import org.junit.Test;
4+
5+
import java.time.LocalDate;
6+
7+
import static com.baeldung.currying.Letter.LETTER_CREATOR;
8+
import static com.baeldung.currying.Letter.SIMPLE_CURRIED_LETTER_CREATOR;
9+
import static com.baeldung.currying.Letter.SIMPLE_LETTER_CREATOR;
10+
import static org.junit.Assert.assertEquals;
11+
12+
public class LetterUnitTest {
13+
private static final String BODY = "BODY";
14+
private static final String SALUTATION = "SALUTATION";
15+
private static final String RETURNING_ADDRESS = "RETURNING ADDRESS";
16+
private static final String INSIDE_ADDRESS = "INSIDE ADDRESS";
17+
private static final LocalDate DATE_OF_LETTER = LocalDate.of(2013, 3, 1);
18+
private static final String CLOSING = "CLOSING";
19+
private static final Letter SIMPLE_LETTER = new Letter(SALUTATION, BODY);
20+
private static final Letter LETTER = new Letter(RETURNING_ADDRESS, INSIDE_ADDRESS, DATE_OF_LETTER, SALUTATION, BODY, CLOSING);
21+
22+
@Test
23+
public void whenStaticCreateMethodIsCalled_thenaSimpleLetterIsCreated() {
24+
assertEquals(SIMPLE_LETTER, Letter.createLetter(SALUTATION, BODY));
25+
}
26+
27+
@Test
28+
public void whenStaticBiFunctionIsCalled_thenaSimpleLetterIsCreated() {
29+
assertEquals(SIMPLE_LETTER, SIMPLE_LETTER_CREATOR.apply(SALUTATION, BODY));
30+
}
31+
32+
@Test
33+
public void whenStaticSimpleCurriedFunctionIsCalled_thenaSimpleLetterIsCreated() {
34+
assertEquals(SIMPLE_LETTER, SIMPLE_CURRIED_LETTER_CREATOR.apply(SALUTATION).apply(BODY));
35+
}
36+
37+
@Test
38+
public void whenStaticCurriedFunctionIsCalled_thenaLetterIsCreated() {
39+
assertEquals(LETTER, LETTER_CREATOR
40+
.apply(RETURNING_ADDRESS)
41+
.apply(CLOSING)
42+
.apply(DATE_OF_LETTER)
43+
.apply(INSIDE_ADDRESS)
44+
.apply(SALUTATION)
45+
.apply(BODY));
46+
}
47+
48+
@Test
49+
public void whenStaticBuilderIsCalled_thenaLetterIsCreated() {
50+
assertEquals(LETTER, Letter.builder()
51+
.withReturnAddress(RETURNING_ADDRESS)
52+
.withClosing(CLOSING)
53+
.withDateOfLetter(DATE_OF_LETTER)
54+
.withInsideAddress(INSIDE_ADDRESS)
55+
.withSalutation(SALUTATION)
56+
.withBody(BODY));
57+
}
58+
}

0 commit comments

Comments
 (0)