Module bus.core

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 Summary

    Modifier and Type
    Method
    Description
    static <F> F
    build(Class<F> functionInterfaceType, Class<?> declaringClass, String methodName, Class<?>... paramTypes)
    构建Lambda
    static <F> F
    build(Class<F> functionInterfaceType, Executable executable)
    根据提供的方法或构造对象,构建对应的Lambda函数 调用函数相当于执行对应的方法或构造
    static <F> F
    build(Class<F> functionInterfaceType, Executable executable, Class<?> declaringClass)
    根据提供的方法或构造对象,构建对应的Lambda函数 调用函数相当于执行对应的方法或构造

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • build

      public static <F> F build(Class<F> functionInterfaceType, Class<?> declaringClass, String methodName, Class<?>... paramTypes)
      构建Lambda
      
       class Something {
           private Long id;
           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的函数式接口类型
      declaringClass - 声明方法的类的类型
      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的函数式接口对象
    • build

      public static <F> F build(Class<F> functionInterfaceType, Executable executable, Class<?> declaringClass)
      根据提供的方法或构造对象,构建对应的Lambda函数 调用函数相当于执行对应的方法或构造
      Type Parameters:
      F - Function类型
      Parameters:
      functionInterfaceType - 接受Lambda的函数式接口类型
      executable - 方法对象,支持构造器
      declaringClass - Executable声明的类,如果方法或构造定义在父类中,此处用于指定子类
      Returns:
      接受Lambda的函数式接口对象