Skip to content
This repository was archived by the owner on Jun 10, 2024. It is now read-only.

Commit b0987ef

Browse files
committed
iluwatar#1842 Improved example
1 parent 02d2598 commit b0987ef

File tree

2 files changed

+13
-61
lines changed

2 files changed

+13
-61
lines changed

currying/src/main/java/com/iluwatar/currying/App.java

+6-31
Original file line numberDiff line numberDiff line change
@@ -22,38 +22,13 @@
2222
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2323
* THE SOFTWARE.
2424
*/
25-
/*
26-
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
27-
*
28-
* The MIT License
29-
* Copyright © 2014-2022 Ilkka Seppälä
30-
*
31-
* Permission is hereby granted, free of charge, to any person obtaining a copy
32-
* of this software and associated documentation files (the "Software"), to deal
33-
* in the Software without restriction, including without limitation the rights
34-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
35-
* copies of the Software, and to permit persons to whom the Software is
36-
* furnished to do so, subject to the following conditions:
37-
*
38-
* The above copyright notice and this permission notice shall be included in
39-
* all copies or substantial portions of the Software.
40-
*
41-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
42-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
43-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
44-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
45-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
46-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
47-
* THE SOFTWARE.
48-
*/
4925
package com.iluwatar.currying;
5026

5127
import java.time.LocalDate;
5228

5329
/**
5430
* This is a Javadoc comment to pass the style check.
5531
*/
56-
5732
public class App {
5833
/**
5934
* This is a Javadoc comment to pass the style check.
@@ -69,13 +44,9 @@ public static void main(String[] args) {
6944
Book.AddTitle kingHorrorBooksFunc = horrorBookFunc.withAuthor("Stephen King");
7045
Book.AddTitle rowlingFantasyBooksFunc = fantasyBookFunc.withAuthor("J.K. Rowling");
7146

72-
// Creates horror books by Stephen King
47+
// Creates books by Stephen King (horror and fantasy genres)
7348
Book shining = kingHorrorBooksFunc.withTitle("The Shining")
7449
.withPublicationDate(LocalDate.of(1977, 1, 28));
75-
Book it = kingHorrorBooksFunc.withTitle("It")
76-
.withPublicationDate(LocalDate.of(1986, 9, 15));
77-
78-
// Creates fantasy books by Stephen King
7950
Book darkTower = kingFantasyBooksFunc.withTitle("The Dark Tower: Gunslinger")
8051
.withPublicationDate(LocalDate.of(1982, 6, 10));
8152

@@ -91,10 +62,14 @@ public static void main(String[] args) {
9162
.withTitle("Foundation")
9263
.withPublicationDate(LocalDate.of(1942, 5, 1));
9364

65+
System.out.println("Stephen King Books:");
9466
System.out.println(shining);
95-
System.out.println(it);
9667
System.out.println(darkTower);
68+
69+
System.out.println("J.K. Rowling Books:");
9770
System.out.println(chamberOfSecrets);
71+
72+
System.out.println("Sci-fi Books:");
9873
System.out.println(dune);
9974
System.out.println(foundation);
10075
}

currying/src/main/java/com/iluwatar/currying/Book.java

+7-30
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,18 @@
3232
* This is a Javadoc comment to pass the style check.
3333
*/
3434
public class Book {
35-
private Genre genre;
36-
private String author;
37-
private String title;
38-
private LocalDate publicationDate;
35+
private final Genre genre;
36+
private final String author;
37+
private final String title;
38+
private final LocalDate publicationDate;
3939

40-
/**
41-
* This is a Javadoc comment to pass the style check.
42-
*/
4340
Book(Genre genre, String author, String title, LocalDate publicationDate) {
4441
this.genre = genre;
4542
this.author = author;
4643
this.title = title;
4744
this.publicationDate = publicationDate;
4845
}
4946

50-
/**
51-
* This is a Javadoc comment to pass the style check.
52-
*/
5347
@Override
5448
public boolean equals(Object o) {
5549
if (this == o) {
@@ -65,25 +59,19 @@ public boolean equals(Object o) {
6559
&& Objects.equals(publicationDate, book.publicationDate);
6660
}
6761

68-
/**
69-
* This is a Javadoc comment to pass the style check.
70-
*/
7162
@Override
7263
public int hashCode() {
7364
return Objects.hash(author, genre, title, publicationDate);
7465
}
7566

76-
/**
77-
* This is a Javadoc comment to pass the style check.
78-
*/
7967
@Override
8068
public String toString() {
8169
return "Book{" + "genre=" + genre + ", author='" + author + '\''
8270
+ ", title='" + title + '\'' + ", publicationDate=" + publicationDate + '}';
8371
}
8472

8573
/**
86-
* This is a Javadoc comment to pass the style check.
74+
* Curried unary book create functions.
8775
*/
8876
static Function<Genre, Function<String, Function<String, Function<LocalDate, Book>>>> BOOK_CREATOR = //
8977
genre
@@ -93,7 +81,8 @@ public String toString() {
9381
-> new Book(genre, author, title, publicationDate);
9482

9583
/**
96-
* This is a Javadoc comment to pass the style check.
84+
* Implements the builder pattern using functional interfaces to create a more readable
85+
* book creator function.
9786
*/
9887
public static AddGenre builder() {
9988
return genre
@@ -103,30 +92,18 @@ public static AddGenre builder() {
10392
-> new Book(genre, author, title, publicationDate);
10493
}
10594

106-
/**
107-
* This is a Javadoc comment to pass the style check.
108-
*/
10995
public interface AddGenre {
11096
Book.AddAuthor withGenre(Genre genre);
11197
}
11298

113-
/**
114-
* This is a Javadoc comment to pass the style check.
115-
*/
11699
public interface AddAuthor {
117100
Book.AddTitle withAuthor(String author);
118101
}
119102

120-
/**
121-
* This is a Javadoc comment to pass the style check.
122-
*/
123103
public interface AddTitle {
124104
Book.AddPublicationDate withTitle(String title);
125105
}
126106

127-
/**
128-
* This is a Javadoc comment to pass the style check.
129-
*/
130107
public interface AddPublicationDate {
131108
Book withPublicationDate(LocalDate publicationDate);
132109
}

0 commit comments

Comments
 (0)