Created
October 15, 2021 17:43
-
-
Save dfparker2002/ff2b4396256d2780e3d7b67e38c54b39 to your computer and use it in GitHub Desktop.
Java Currying example
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
// src article https://www.baeldung.com/java-currying | |
//src: https://github.com/iluwatar/java-design-patterns/blob/367b870f5b5b63be2c0b27158c97911e015f1c93/currying/src/main/java/com/iluwatar/currying/Staff.java | |
package com.iluwatar.currying; | |
import java.time.LocalDate; | |
import java.util.Objects; | |
import java.util.function.Function; | |
public class Staff { | |
private String firstName; | |
private String lastName; | |
private Gender gender; | |
private String email; | |
private LocalDate dateOfBirth; | |
public Staff(String firstName, String lastName, Gender gender, String email, LocalDate dateOfBirth) { | |
this.firstName = firstName; | |
this.lastName = lastName; | |
this.gender = gender; | |
this.email = email; | |
this.dateOfBirth = dateOfBirth; | |
} | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) return true; | |
if (o == null || getClass() != o.getClass()) return false; | |
Staff staff = (Staff) o; | |
return firstName.equals(staff.firstName) && lastName.equals(staff.lastName) && gender == staff.gender && email.equals(staff.email) && dateOfBirth.equals(staff.dateOfBirth); | |
} | |
@Override | |
public int hashCode() { | |
return Objects.hash(firstName, lastName, gender, email, dateOfBirth); | |
} | |
/** | |
* Use {@link Function} for currying | |
*/ | |
static Function<String, Function<String, Function<Gender, Function<String, Function<LocalDate, Staff>>>>> CREATOR = | |
firstName -> lastName -> gender -> email -> dateOfBirth -> new Staff(firstName, lastName, gender, email, dateOfBirth); | |
/** | |
* Use functional interfaces for currying | |
*/ | |
static AddFirstName builder() { | |
return firstName -> lastName -> gender -> email -> dateOfBirth -> new Staff(firstName, lastName, gender, email, dateOfBirth); | |
} | |
interface AddFirstName { | |
AddLastName withReturnFirstName(String firstName); | |
} | |
interface AddLastName { | |
AddGender withReturnLastName(String lastName); | |
} | |
interface AddGender { | |
AddEmail withReturnGender(Gender gender); | |
} | |
interface AddEmail { | |
AddDateOfBirth withReturnEmail(String email); | |
} | |
interface AddDateOfBirth { | |
Staff withReturnDateOfBirth(LocalDate dateOfBirth); | |
} | |
enum Gender { | |
Male, Female | |
} | |
} | |
// src: https://github.com/iluwatar/java-design-patterns/blob/367b870f5b5b63be2c0b27158c97911e015f1c93/currying/src/test/java/com/iluwatar/currying/StaffTest.java | |
import org.junit.Assert; | |
import org.junit.Test; | |
import java.time.LocalDate; | |
public class StaffTest { | |
private final String firstName = "Janus"; | |
private final String lastName = "Lin"; | |
private final Staff.Gender gender = Staff.Gender.Male; | |
private final String email = "example@gmail.com"; | |
private final LocalDate dateOfBirth = LocalDate.now(); | |
private final Staff expectedResult = new Staff(firstName, lastName, gender, email, dateOfBirth); | |
@Test | |
public void createStaffWithBasicCurrying() { | |
Staff actualResult = Staff.CREATOR | |
.apply(firstName) | |
.apply(lastName) | |
.apply(gender) | |
.apply(email) | |
.apply(dateOfBirth); | |
Assert.assertEquals(expectedResult, actualResult); | |
} | |
@Test | |
public void createStaffWithFunctionalInterface() { | |
Staff actualResult = Staff.builder() | |
.withReturnFirstName(firstName) | |
.withReturnLastName(lastName) | |
.withReturnGender(gender) | |
.withReturnEmail(email) | |
.withReturnDateOfBirth(dateOfBirth); | |
Assert.assertEquals(expectedResult, actualResult); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment