Java8 Function函数式接口

1. 什么是函数式接口

1.只包含一个抽象方法的接口,称为函数式接口。
2.可以使用注解@FunctionalInterface注解,检查是否只包含一个接口。

2. Java内置四大核心函数是接口

在这里插入图片描述在这里插入图片描述

3. 自定义函数式接口

@FunctionalInterface
public interface CalcutionFunction {
    Integer getValue(Integer val);
}
@FunctionalInterface
public interface HandlerFunction<T, R> {

    R getValue(T t1, T t2);
}

4. 测试案例


/**
 * 支付对象
 *
 * @author zrj
 * @date 2021/1/26
 * @since 1.0
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Payment {
    public Integer id;
    public String name;
}

/**
 * 支付接口
 *
 * @author zrj
 * @date 2021/1/26
 * @since 1.0
 */
public interface PaymentService {

    int create(Payment payment);

    int delete(int id);

    int update(Payment payment);

    Payment get();

    boolean valid();

    void customer();

    void exception();

}

/**
 * 支付实现类
 *
 * @author zrj
 * @date 2021/1/26
 * @since 1.0
 */
public class PaymentServiceImpl implements PaymentService {

    @Override
    public int create(Payment payment) {
        return 1;
    }

    @Override
    public int delete(int id) {
        return 1;
    }

    @Override
    public int update(Payment payment) {
        return 1;
    }

    @Override
    public Payment get() {
        return new Payment();
    }

    @Override
    public boolean valid() {
        return false;
    }

    @Override
    public void customer() {
    }

    @Override
    public void exception() {
        throw new RuntimeException();
    }
}

/**
 * 自定义调用异常
 *
 * @author zrj
 * @date 2021/1/20
 * @since 1.0
 **/
public class InvokeServiceException extends RuntimeException {
    private String code;
    private String message;

    public InvokeServiceException() {
    }

    public InvokeServiceException(String code, String message) {
        this.code = code;
        this.message = message;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

/**
 * InvokeFunctionHandler
 *
 * @author zrj
 * @date 2021/1/26
 * @since 1.0
 */
@FunctionalInterface
public interface InvokeFunctionHandler<T> {
    int invoke(T t) throws InvokeServiceException;
}

/**
 * Represents a void of results.
 * no param and no result
 *
 * @author zrj
 * @date 2021/1/26
 * @since 1.0
 */
@FunctionalInterface
public interface InvokeConsumerHandler {
    void invoke() throws InvokeServiceException;
}

/**
 * 异常处理
 *
 * @author zrj
 * @date 2021/1/26
 * @since 1.0
 */
public class InvokeAdapterHandler {

    /**
     * 校验异常处理
     */
    public static boolean catchBooleanThrow(String msg, BooleanSupplier booleanSupplier) {
        try {
            return booleanSupplier.getAsBoolean();
        } catch (Exception ex) {
            throw new RuntimeException(msg + ":" + ex);
        }
    }

    /**
     * 无入参无返回异常处理
     */
    public static void catchConsumerThrow(String msg, InvokeConsumerHandler invokeConsumerHandler) {
        try {
            invokeConsumerHandler.invoke();
        } catch (Exception ex) {
            throw new RuntimeException(msg + ":" + ex);
        }
    }

    /**
     * 无入参有返回异常处理
     */
    public static <T> T catchSupplierThrow(String msg, Supplier<T> supplier) {
        try {
            return supplier.get();
        } catch (Exception ex) {
            throw new RuntimeException(msg + ":" + ex);
        }
    }

    /**
     * 有入参有返回值异常处理
     * 适合create,delete,update方法
     *
     * @param msg      异常信息
     * @param param    参数
     * @param function 方法
     * @return
     */
    public static Object catchFunctionThrow(String msg, Object param, Function<Object, Object> function) {
        try {
            return function.apply(param);
        } catch (Exception ex) {
            throw new RuntimeException(msg + ":" + ex);
        }
    }

    public static int catchInvokeFunctionThrow(String msg, Object param, InvokeFunctionHandler<Object> invokeFunctionHandler) {
        try {
            return invokeFunctionHandler.invoke(param);
        } catch (Exception ex) {
            throw new RuntimeException(msg + ":" + ex);
        }
    }
}

/**
 * AdapterHandler测试类
 *
 * @author zrj
 * @date 2021/1/26
 * @since 1.0
 */
public class InvokeAdapterHandlerTest {
    @Test
    public void invokeBooleanHandler() {
        PaymentService paymentService = new PaymentServiceImpl();
        boolean valid = InvokeAdapterHandler.catchBooleanThrow("invokeBooleanHandler调用异常", () -> paymentService.valid());
        System.out.println("invokeBooleanHandler处理成功:" + valid);
    }

    @Test
    public void invokeExceptionHandler() {
        PaymentService paymentService = new PaymentServiceImpl();
        InvokeAdapterHandler.catchConsumerThrow("invokeExceptionHandler调用异常", () -> paymentService.exception());
        System.out.println("invokeBooleanHandler处理成功");
    }

    @Test
    public void invokeCustomerHandler() {
        PaymentService paymentService = new PaymentServiceImpl();
        InvokeAdapterHandler.catchConsumerThrow("invokeCustomerHandler调用异常", () -> paymentService.customer());
        System.out.println("invokeCustomerHandler处理成功");
    }

    @Test
    public void invokeSupplierHandler() {
        PaymentService paymentService = new PaymentServiceImpl();
        Payment payment = InvokeAdapterHandler.catchSupplierThrow("invokeSupplierHandler调用异常", () -> paymentService.get());
        System.out.println("invokeCustomerHandler处理成功" + payment);
    }

    @Test
    public void invokeFunctionHandler() {
        PaymentService paymentService = new PaymentServiceImpl();
        Payment hello = new Payment();

        InvokeAdapterHandler.catchInvokeFunctionThrow("invokeSupplierHandler调用异常", hello, (catchInvokeFunctionThrow) -> paymentService.create(hello));
        System.out.println("invokeCustomerHandler处理成功");
    }

}

版权声明:本文为m0_37583655原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。