In my understanding the way lambdas work is similar to creating instance of an anonymous class and providing the implementation of an abstract method - please feel free to comment on this
I have also been looking into combinator pattern in Java and getting confused by "this" in the and method of the below interface, in my understanding, this refers to a current instance of a class. When I am doing System.out.println(this) in the and method, it prints combinatorpatttern.CustomerRegistrationValidator$$Lambda$4/0x0000000800067440@12f40c25, which is not making sense as to what class this is referring to.
public interface CustomerRegistrationValidator
extends Function<Customer, ValidationResult> {
static CustomerRegistrationValidator isEmailValid() {
return customer -> {
System.out.println("running email validation");
return customer.getEmail().contains("@") ?
SUCCESS : EMAIL_NOT_VALID;
};
}
static CustomerRegistrationValidator isPhoneNumberValid() {
return customer -> customer.getPhoneNumber().startsWith("+0") ?
SUCCESS : PHONE_NUMBER_NOT_VALID;
}
static CustomerRegistrationValidator isAnAdult() {
return customer ->
Period.between(customer.getDob(), LocalDate.now()).getYears() > 16 ?
SUCCESS : IS_NOT_AN_ADULT;
}
default CustomerRegistrationValidator and (CustomerRegistrationValidator other) {
return customer -> {
ValidationResult result = this.apply(customer);
return result.equals(SUCCESS) ? other.apply(customer) : result;
};
}
enum ValidationResult {
SUCCESS,
PHONE_NUMBER_NOT_VALID,
EMAIL_NOT_VALID,
IS_NOT_AN_ADULT
}
}
public class Main {
public static void main(String[] args) {
Customer customer = new Customer(
"Alice",
"alice@gmail.com",
"+0898787879878",
LocalDate.of(2015, 1,1)
);
ValidationResult result = isEmailValid()
.and(isPhoneNumberValid())
.and(isAnAdult())
.apply(customer);
System.out.println(result);
}
}