com.cudrania.core.functions.Fn Maven / Gradle / Ivy
Show all versions of cudrania Show documentation
package com.cudrania.core.functions;
import lombok.SneakyThrows;
import java.io.Serializable;
import java.lang.invoke.SerializedLambda;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.Callable;
/**
* 多个参数的函数定义
*
* bi = 2
* tri = 3
* quad = 4
* quin = 5
* hexa = 6
* hept = 7
* octa = 8
* nona = 9
* deca = 10
*
*/
public class Fn {
public static Supplier of(Supplier lambda) {
return lambda;
}
public static Consumer
of(Consumer
lambda) {
return lambda;
}
public static BiConsumer of(BiConsumer lambda) {
return lambda;
}
public static TriConsumer of(TriConsumer lambda) {
return lambda;
}
public static QuaConsumer of(QuaConsumer lambda) {
return lambda;
}
public static QuiConsumer of(QuiConsumer lambda) {
return lambda;
}
public static HexConsumer of(HexConsumer lambda) {
return lambda;
}
public static HepConsumer of(HepConsumer lambda) {
return lambda;
}
public static OctConsumer of(OctConsumer lambda) {
return lambda;
}
public static NonConsumer of(NonConsumer lambda) {
return lambda;
}
public static DecConsumer of(DecConsumer lambda) {
return lambda;
}
public static Function
of(Function
lambda) {
return lambda;
}
public static BiFunction of(BiFunction lambda) {
return lambda;
}
/**
* 指定lambda实例,示例: Fn.of(Map<String, String>::put);
*/
public static TriFunction of(TriFunction lambda) {
return lambda;
}
public static QuaFunction of(QuaFunction lambda) {
return lambda;
}
public static QuiFunction of(QuiFunction lambda) {
return lambda;
}
public static HexFunction of(HexFunction lambda) {
return lambda;
}
public static HepFunction of(HepFunction lambda) {
return lambda;
}
public static OctFunction of(OctFunction lambda) {
return lambda;
}
public static NonFunction of(NonFunction lambda) {
return lambda;
}
public static DecFunction of(DecFunction lambda) {
return lambda;
}
@FunctionalInterface
public interface Supplier extends Lambda {
T get();
}
@FunctionalInterface
public interface Consumer extends Lambda, Binder
, java.util.function.Consumer
{
void accept(P p1);
default Runnable bind(P p1) {
return () -> this.accept(p1);
}
}
@FunctionalInterface
public interface BiConsumer extends Lambda, Binder>, java.util.function.BiConsumer {
void accept(P1 p1, P2 p2);
default Consumer bind(P1 p1) {
return (p2) -> this.accept(p1, p2);
}
}
@FunctionalInterface
public interface TriConsumer extends Lambda, Binder> {
void accept(P1 p1, P2 p2, P3 p3);
default BiConsumer bind(P1 p1) {
return (p2, p3) -> this.accept(p1, p2, p3);
}
}
@FunctionalInterface
public interface QuaConsumer extends Lambda, Binder> {
void accept(P1 p1, P2 p2, P3 p3, P4 p4);
default TriConsumer bind(P1 p1) {
return (p2, p3, p4) -> this.accept(p1, p2, p3, p4);
}
}
@FunctionalInterface
public interface QuiConsumer extends Lambda, Binder> {
void accept(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5);
default QuaConsumer bind(P1 p1) {
return (p2, p3, p4, p5) -> this.accept(p1, p2, p3, p4, p5);
}
}
@FunctionalInterface
public interface HexConsumer extends Lambda, Binder> {
void accept(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6);
default QuiConsumer bind(P1 p1) {
return (p2, p3, p4, p5, p6) -> this.accept(p1, p2, p3, p4, p5, p6);
}
}
@FunctionalInterface
public interface HepConsumer extends Lambda, Binder> {
void accept(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7);
default HexConsumer bind(P1 p1) {
return (p2, p3, p4, p5, p6, p7) -> this.accept(p1, p2, p3, p4, p5, p6, p7);
}
}
@FunctionalInterface
public interface OctConsumer extends Lambda, Binder> {
void accept(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8);
default HepConsumer bind(P1 p1) {
return (p2, p3, p4, p5, p6, p7, p8) -> this.accept(p1, p2, p3, p4, p5, p6, p7, p8);
}
}
@FunctionalInterface
public interface NonConsumer extends Lambda, Binder> {
void accept(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8, P9 p9);
default OctConsumer bind(P1 p1) {
return (p2, p3, p4, p5, p6, p7, p8, p9) -> this.accept(p1, p2, p3, p4, p5, p6, p7, p8, p9);
}
}
@FunctionalInterface
public interface DecConsumer extends Lambda, Binder> {
void accept(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8, P9 p9, P10 p10);
default NonConsumer bind(P1 p1) {
return (p2, p3, p4, p5, p6, p7, p8, p9, p10) -> this.accept(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10);
}
}
@FunctionalInterface
public interface Function extends Lambda, Binder
>, java.util.function.Function
{
R apply(P p1);
default Callable bind(P p) {
return () -> this.apply(p);
}
}
@FunctionalInterface
public interface BiFunction extends Lambda, Binder>, java.util.function.BiFunction {
R apply(P1 p1, P2 p2);
default Function bind(P1 p1) {
return (p2) -> this.apply(p1, p2);
}
}
@FunctionalInterface
public interface TriFunction extends Lambda, Binder> {
R apply(P1 p1, P2 p2, P3 p3);
default BiFunction bind(P1 p1) {
return (p2, p3) -> this.apply(p1, p2, p3);
}
}
@FunctionalInterface
public interface QuaFunction extends Lambda, Binder> {
R apply(P1 p1, P2 p2, P3 p3, P4 p4);
default TriFunction bind(P1 p1) {
return (p2, p3, p4) -> this.apply(p1, p2, p3, p4);
}
}
@FunctionalInterface
public interface QuiFunction extends Lambda, Binder> {
R apply(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5);
default QuaFunction bind(P1 p1) {
return (p2, p3, p4, p5) -> this.apply(p1, p2, p3, p4, p5);
}
}
@FunctionalInterface
public interface HexFunction extends Lambda, Binder> {
R apply(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6);
default QuiFunction bind(P1 p1) {
return (p2, p3, p4, p5, p6) -> this.apply(p1, p2, p3, p4, p5, p6);
}
}
@FunctionalInterface
public interface HepFunction extends Lambda, Binder> {
R apply(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7);
default HexFunction bind(P1 p1) {
return (p2, p3, p4, p5, p6, p7) -> this.apply(p1, p2, p3, p4, p5, p6, p7);
}
}
@FunctionalInterface
public interface OctFunction extends Lambda, Binder> {
R apply(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8);
default HepFunction bind(P1 p1) {
return (p2, p3, p4, p5, p6, p7, p8) -> this.apply(p1, p2, p3, p4, p5, p6, p7, p8);
}
}
@FunctionalInterface
public interface NonFunction extends Lambda, Binder> {
R apply(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8, P9 p9);
default OctFunction bind(P1 p1) {
return (p2, p3, p4, p5, p6, p7, p8, p9) -> this.apply(p1, p2, p3, p4, p5, p6, p7, p8, p9);
}
}
@FunctionalInterface
public interface DecFunction extends Lambda, Binder> {
R apply(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8, P9 p9, P10 p10);
default NonFunction bind(P1 p1) {
return (p2, p3, p4, p5, p6, p7, p8, p9, p10) -> this.apply(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10);
}
}
/**
* 用于提取Lambda对象
*/
public interface Binder {
/**
* 绑定lambda第一个参数
*
* @param p
* @return
*/
FUN bind(P p);
}
/**
* 用于提取Lambda对象
*/
public interface Lambda extends Serializable {
/**
* 获取lambda引用方法名称
*
* @return
*/
default String name() {
return lambda().getImplMethodName();
}
/**
* 获取SerializedLambda对象
*
* @return
*/
default SerializedLambda lambda() {
return Fn.lambda(this);
}
}
/**
* 缓存
*/
private static Map cache = new WeakHashMap();
/**
* 获取SerializedLambda对象, 入参必须是方法引用
*
* @param lambda
* @return
*/
@SneakyThrows
public static SerializedLambda lambda(Serializable lambda) {
return cache.computeIfAbsent(lambda, k -> lambda0(k));
}
/**
* 获取SerializedLambda对象, 入参必须是方法引用
*
* @param lambda
* @return
*/
@SneakyThrows
private static SerializedLambda lambda0(Serializable lambda) {
Method method = lambda.getClass().getDeclaredMethod("writeReplace");
method.setAccessible(Boolean.TRUE);
//调用writeReplace()方法,返回一个SerializedLambda对象
return (SerializedLambda) method.invoke(lambda);
}
}