All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.cosycode.ext.hub.AbstractClosureProxy Maven / Gradle / Ivy

Go to download

扩展模块, 用于存放一些非常用的工具或模块的扩展类, 例如在poi基础上扩展的excel的导入模块, 模拟按键模块

The newest version!
package com.github.cosycode.ext.hub;

import java.util.Objects;
import java.util.function.*;

/**
 * Description :  抽象闭包代理类
 * 

* created in 2021/4/5 *

* * @param 代理方法的函数式接口实例 * @param

代理方法可能传入的参数类型 * @param 代理方法可能的返回值类型 * @author CPF */ public abstract class AbstractClosureProxy { /** * 代理方法的函数式接口实例 */ protected final T functional; /** * 函数式接口 then 对传入参数的操作函数, 如果该项为 null, 则默认 */ protected final BiFunction biFunction; protected AbstractClosureProxy(T functional) { this.functional = functional; this.biFunction = geneDefaultBiFunction(); } protected AbstractClosureProxy(T functional, BiConsumer biConsumer) { this(functional, (t, p) -> { biConsumer.accept(t, p); return null; }); } protected AbstractClosureProxy(T functional, BiFunction biFunction) { Objects.requireNonNull(functional, "functional cannot be null"); this.functional = functional; if (biFunction == null) { this.biFunction = geneDefaultBiFunction(); } else { this.biFunction = biFunction; } } /** * 闭包代理方法: Function */ public abstract R closureFunction(P params); /** * 闭包代理方法: Consumer */ public void closureConsumer(P params) { closureFunction(params); } /** * 闭包代理方法: Supplier */ public R closureSupplier() { return closureFunction(null); } /** * 闭包代理方法: Runnable */ public void closureRunnable() { closureFunction(null); } @SuppressWarnings("unchecked") public P closureUnaryOperator(P params) { return (P) closureFunction(params); } /** * 根据 then 返回默认的闭包代理方法 * * @return 默认的闭包代理方法 */ @SuppressWarnings("unchecked") public T proxy() { if (functional instanceof Consumer) { final Consumer

proxy = this::closureConsumer; return (T) proxy; } else if (functional instanceof UnaryOperator) { final UnaryOperator

proxy = this::closureUnaryOperator; return (T) proxy; } else if (functional instanceof Function) { final Function proxy = this::closureFunction; return (T) proxy; } else if (functional instanceof Supplier) { final Supplier proxy = this::closureSupplier; return (T) proxy; } else if (functional instanceof Runnable) { final Runnable proxy = this::closureRunnable; return (T) proxy; } throw new IllegalArgumentException("the parameter functional" + functional + " must be a supported functional interface"); } /** * 返回自定义的闭包代理函数式接口实例 * * @param function 自定义闭包代理函数式接口实例返回函数 * @param 自定义的返回函数式接口类型 * @return 自定义的闭包代理函数式接口实例 */ public V proxy(Function, V> function) { return function.apply(this::closureFunction); } /** * 根据函数式接口实例, 生成默认的针对(函数接口then调用的处理方式)的处理函数 * * @return 函数接口then调用的处理方式 的处理函数 */ @SuppressWarnings("unchecked") private BiFunction geneDefaultBiFunction() { if (functional instanceof Consumer) { return (t, p) -> { Consumer

consumer = (Consumer

) t; consumer.accept(p); return null; }; } else if (functional instanceof UnaryOperator) { return (t, p) -> { UnaryOperator

consumer = (UnaryOperator

) t; return (R) consumer.apply(p); }; } else if (functional instanceof Function) { return (t, p) -> { Function consumer = (Function) t; return consumer.apply(p); }; } else if (functional instanceof Supplier) { return (t, p) -> { Supplier supplier = ((Supplier) t); return supplier.get(); }; } else if (functional instanceof Runnable) { return (t, p) -> { ((Runnable) t).run(); return null; }; } throw new IllegalArgumentException("the parameter functional" + functional + " must be a supported functional interface"); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy