Class LambdaFactory

java.lang.Object
org.miaixz.bus.core.center.function.LambdaFactory

public class LambdaFactory extends Object
以类似反射的方式动态创建Lambda,在性能上有一定优势,同时避免每次调用Lambda时创建匿名内部类
Since:
Java 17+
Author:
Kimi Liu
  • Method Details

    • build

      public static <F> F build(Class<F> functionInterfaceType, Class<?> methodClass, String methodName, Class<?>... paramTypes)
      构建Lambda
      
       class Something {
           private Long data;
           private String name;
           // ... 省略GetterSetter方法
       }
       Function<Something, Long> getIdFunction = LambdaFactory.buildLambda(Function.class, Something.class, "getId");
       BiConsumer<Something, String> setNameConsumer = LambdaFactory.buildLambda(BiConsumer.class, Something.class, "setName", String.class);
       
       
      Type Parameters:
      F - Function类型
      Parameters:
      functionInterfaceType - 接受Lambda的函数式接口类型
      methodClass - 声明方法的类的类型
      methodName - 方法名称
      paramTypes - 方法参数数组
      Returns:
      接受Lambda的函数式接口对象
    • build

      public static <F> F build(Class<F> functionInterfaceType, Executable executable)
      根据提供的方法或构造对象,构建对应的Lambda函数 调用函数相当于执行对应的方法或构造
      Type Parameters:
      F - Function类型
      Parameters:
      functionInterfaceType - 接受Lambda的函数式接口类型
      executable - 方法对象,支持构造器
      Returns:
      接受Lambda的函数式接口对象