Function.apply-方法在未被覆盖时在自定义界面中执行什么操作?
What does the Function.apply-Method do in a custom interface, when it's not overwritten?
我无法理解以下示例代码中“apply”方法的用法。 apply 方法到底在做什么,因为它没有被覆盖?
它显然返回结果 ValidationResult
,但没有代码支持它。
public class Main {
public static void main(String[] args) {
Customer customer = new Customer("Alice", "alice@gmail.com", "0879546451", LocalDate.of(2000, 1, 1));
ValidationResult result = CustomerRegistrationValidator
.isEmailValid()
.and(CustomerRegistrationValidator.isPhoneNumber())
.and(CustomerRegistrationValidator.isAdult())
.apply(customer);
System.out.println(result);
}
}
public interface CustomerRegistrationValidator extends Function<Customer, ValidationResult> {
static CustomerRegistrationValidator isEmailValid() {
return customer -> customer.getEmail().contains("@") ? ValidationResult.SUCCESS : ValidationResult.EMAIL_NOT_VALID;
}
static CustomerRegistrationValidator isPhoneNumber() {
return customer -> customer.getPhoneNumber().contains("+08") ? ValidationResult.SUCCESS : ValidationResult.PHONE_NUMER_NOT_VALID;
}
static CustomerRegistrationValidator isAdult() {
return customer -> Period.between(customer.getDob(), LocalDate.now()).getYears() > 16 ? ValidationResult.SUCCESS : ValidationResult.IS_NOT_AN_ADULT;
}
default CustomerRegistrationValidator and (CustomerRegistrationValidator other) {
return customer -> {
ValidationResult result = this.apply(customer);
return result.equals(ValidationResult.SUCCESS) ? other.apply(customer) : result;
};
}
enum ValidationResult {
SUCCESS, PHONE_NUMER_NOT_VALID, EMAIL_NOT_VALID, IS_NOT_AN_ADULT;
}
}
What is the apply method doing exactly, since it's not overwritten?
取决于您从接口中选择的静态方法。在 isEmailValid
的情况下, apply
方法的主体是
customer.getEmail().contains("@") ? ValidationResult.SUCCESS : ValidationResult.EMAIL_NOT_VALID;
It's obviously returning the result ValidationResult, but there is no code to back it.
在那里,CustomerRegistrationValidator
实例由 lambda 定义。
想象一下你必须重写这个
static CustomerRegistrationValidator isEmailValid() {
return new CustomerRegistrationValidator() {
@Override
public ValidationResult apply(Customer customer) {
return customer.getEmail().contains("@") ? ValidationResult.SUCCESS : ValidationResult.EMAIL_NOT_VALID;
}
};
}
到 lambda 语法。你最终会得到你现在所拥有的。
我无法理解以下示例代码中“apply”方法的用法。 apply 方法到底在做什么,因为它没有被覆盖?
它显然返回结果 ValidationResult
,但没有代码支持它。
public class Main {
public static void main(String[] args) {
Customer customer = new Customer("Alice", "alice@gmail.com", "0879546451", LocalDate.of(2000, 1, 1));
ValidationResult result = CustomerRegistrationValidator
.isEmailValid()
.and(CustomerRegistrationValidator.isPhoneNumber())
.and(CustomerRegistrationValidator.isAdult())
.apply(customer);
System.out.println(result);
}
}
public interface CustomerRegistrationValidator extends Function<Customer, ValidationResult> {
static CustomerRegistrationValidator isEmailValid() {
return customer -> customer.getEmail().contains("@") ? ValidationResult.SUCCESS : ValidationResult.EMAIL_NOT_VALID;
}
static CustomerRegistrationValidator isPhoneNumber() {
return customer -> customer.getPhoneNumber().contains("+08") ? ValidationResult.SUCCESS : ValidationResult.PHONE_NUMER_NOT_VALID;
}
static CustomerRegistrationValidator isAdult() {
return customer -> Period.between(customer.getDob(), LocalDate.now()).getYears() > 16 ? ValidationResult.SUCCESS : ValidationResult.IS_NOT_AN_ADULT;
}
default CustomerRegistrationValidator and (CustomerRegistrationValidator other) {
return customer -> {
ValidationResult result = this.apply(customer);
return result.equals(ValidationResult.SUCCESS) ? other.apply(customer) : result;
};
}
enum ValidationResult {
SUCCESS, PHONE_NUMER_NOT_VALID, EMAIL_NOT_VALID, IS_NOT_AN_ADULT;
}
}
What is the apply method doing exactly, since it's not overwritten?
取决于您从接口中选择的静态方法。在 isEmailValid
的情况下, apply
方法的主体是
customer.getEmail().contains("@") ? ValidationResult.SUCCESS : ValidationResult.EMAIL_NOT_VALID;
It's obviously returning the result ValidationResult, but there is no code to back it.
在那里,CustomerRegistrationValidator
实例由 lambda 定义。
想象一下你必须重写这个
static CustomerRegistrationValidator isEmailValid() {
return new CustomerRegistrationValidator() {
@Override
public ValidationResult apply(Customer customer) {
return customer.getEmail().contains("@") ? ValidationResult.SUCCESS : ValidationResult.EMAIL_NOT_VALID;
}
};
}
到 lambda 语法。你最终会得到你现在所拥有的。