当前位置:   article > 正文

MoreSuppliers类_moresuppliers.lazy

moresuppliers.lazy

概述

MoreSuppliers类是一个Java工具类,它提供了一些增强的Supplier函数,使得Supplier执行的结果可以被缓存,真正的调用只执行一次。

public static <T> CloseableSupplier<T> lazy(Supplier<T> delegate):

这个方法返回一个懒加载的提供器,首次获取值时通过delegate加载值,并缓存这个值,在后续获取时直接返回这个缓存的值。这个方法的使用场景是当你有一个计算成本较高或者IO操作的Supplier,并且你希望只执行一次这个操作,然后缓存结果以供后续使用。

示例:

  1. Supplier<String> expensiveOperation = () -> {
  2.     // Some expensive operation...
  3.     return "result";
  4. };
  5. Supplier<String> lazySupplier = MoreSuppliers.lazy(expensiveOperation);
  6. String result = lazySupplier.get();  // The expensive operation is performed here.
  7. String cachedResult = lazySupplier.get();  // The cached result is returned here.

public static <T> CloseableSupplier<T> lazy(Supplier<T> delegate, boolean resetAfterClose):

这个方法和上一个方法类似,但是它允许在关闭提供器返回的资源后,是否释放缓存的对象。这个方法的使用场景是当你的Supplier返回的是一个需要关闭的资源,比如一个数据库连接,你希望在关闭这个资源后,下次调用get()方法时重新获取一个新的资源。

示例:

  1. Supplier<Connection> connectionSupplier = () -> {
  2.     // Get a connection from the database...
  3.     return connection;
  4. };
  5. CloseableSupplier<Connection> lazySupplier = MoreSuppliers.lazy(connectionSupplier, true);
  6. Connection connection = lazySupplier.get();  // The connection is obtained here.
  7. lazySupplier.tryClose(Connection::close);  // The connection is closed here.
  8. Connection newConnection = lazySupplier.get();  // A new connection is obtained here.

public static <T, X extends Throwable> CloseableThrowableSupplier<T, X> lazyEx(ThrowableSupplier<T, X> delegate):

这个方法返回一个懒加载的提供器,支持异常类型声明。这个方法的使用场景是当你的Supplier可能抛出一个异常,你希望这个异常能被正确地传播出去。

示例:

  1. ThrowableSupplier<String, IOException> ioOperation = () -> {
  2.     // Some IO operation...
  3.     return "result";
  4. };
  5. ThrowableSupplier<String, IOException> lazySupplier = MoreSuppliers.lazyEx(ioOperation);
  6. try {
  7.     String result = lazySupplier.get();  // The IO operation is performed here.
  8. } catch (IOException e) {
  9.     // Handle the exception...
  10. }

public static <T, X extends Throwable> CloseableThrowableSupplier<T, X> lazyEx(ThrowableSupplier<T, X> delegate, boolean resetAfterClose):

这个方法和上一个方法类似,但是它允许在关闭提供器返回的资源后,是否释放缓存的对象。这个方法的使用场景是当你的Supplier返回的是一个需要关闭的资源并且可能抛出一个异常,你希望在关闭这个资源后,下次调用get()方法时重新获取一个新的资源,并且异常能被正确地传播出去。

示例:

  1. ThrowableSupplier<Connection, SQLException> connectionSupplier = () -> {
  2.     // Get a connection from the database...
  3.     return connection;
  4. };
  5. CloseableThrowableSupplier<Connection, SQLException> lazySupplier = MoreSuppliers.lazyEx(connectionSupplier, true);
  6. try {
  7.     Connection connection = lazySupplier.get();  // The connection is obtained here.
  8.     lazySupplier.tryClose(Connection::close);  // The connection is closed here.
  9.     Connection newConnection = lazySupplier.get();  // A new connection is obtained here.
  10. } catch (SQLException e) {
  11.     // Handle the exception...
  12. }

public static <T> AsyncSupplier<T> asyncLazyEx(Supplier<T> delegate, Supplier<T> pendingSupplier, String threadName):

这个方法返回一个异步加载的提供器,通过异步线程来完成初始化操作,支持超时。当超过指定的时间没有获取初始值成功时,使用pendingSupplier提供的值作为托底。这个方法的使用场景是当你的Supplier需要花费较长的时间来获取值,你希望这个操作能在一个单独的线程中进行,而主线程可以继续执行其他任务。

示例:

  1. Supplier<String> slowOperation = () -> {
  2.     // Some slow operation...
  3.     return "result";
  4. };
  5. Supplier<String> fallback = () -> "fallback";
  6. AsyncSupplier<String> asyncSupplier = MoreSuppliers.asyncLazyEx(slowOperation, fallback, "InitThread");
  7. String result = asyncSupplier.get(Duration.ofSeconds(5));  // The slow operation is performed in a separate thread. If it takes more than 5 seconds, the fallback value is returned.

public static <T> AsyncSupplier<T> asyncLazyEx(Supplier<T> delegate, String threadName):

这个方法和上一个方法类似,但是它没有提供托底的Supplier,如果异步初始化值超时,它将返回null。

示例:

  1. Supplier<String> slowOperation = () -> {
  2.     // Some slow operation...
  3.     return "result";
  4. };
  5. AsyncSupplier<String> asyncSupplier = MoreSuppliers.asyncLazyEx(slowOperation, "InitThread");
  6. String result = asyncSupplier.get(Duration.ofSeconds(5));  // The slow operation is performed in a separate thread. If it takes more than 5 seconds, null is returned.

public static <T> AsyncSupplier<T> asyncLazyEx(Supplier<T> delegate):

这个方法和上一个方法类似,但是它没有指定执行初始化操作的线程名称。

示例:

  1. Supplier<String> slowOperation = () -> {
  2.     // Some slow operation...
  3.     return "result";
  4. };
  5. AsyncSupplier<String> asyncSupplier = MoreSuppliers.asyncLazyEx(slowOperation);
  6. String result = asyncSupplier.get(Duration.ofSeconds(5));  // The slow operation is performed in a separate thread. If it takes more than 5 seconds, null is returned.

CloseableSupplier<T>:

这是一个可关闭的Supplier实现,支持通过tryClose(ThrowableConsumer<T, X>closer)方法关闭提供器返回的资源。

示例:

  1. CloseableSupplier<Connection> connectionSupplier = MoreSuppliers.lazy(() -> {
  2.     // Get a connection from the database...
  3.     return connection;
  4. }, true);
  5. Connection connection = connectionSupplier.get();  // The connection is obtained here.
  6. connectionSupplier.tryClose(Connection::close);  // The connection is closed here.

CloseableThrowableSupplier<T, X>:

这是一个可关闭的Supplier实现,支持异常类型声明,通过tryClose(ThrowableConsumer<T, X> closer)方法关闭提供器返回的资源。

示例:

  1. CloseableThrowableSupplier<Connection, SQLException> connectionSupplier = MoreSuppliers.lazyEx(() -> {
  2.     // Get a connection from the database...
  3.     return connection;
  4. }, true);
  5. try {
  6.     Connection connection = connectionSupplier.get();  // The connection is obtained here.
  7.     connectionSupplier.tryClose(Connection::close);  // The connection is closed here.
  8. } catch (SQLException e) {
  9.     // Handle the exception...
  10. }

AsyncSupplier<T>:

这是一个异步加载的Supplier实现,通过异步线程来完成初始化操作,支持超时。当超过指定的时间没有获取初始值成功时,使用pendingSupplier提供的值作为托底。

示例:

  1. Supplier<String> slowOperation = () -> {
  2.     // Some slow operation...
  3.     return "result";
  4. };
  5. Supplier<String> fallback = () -> "fallback";
  6. AsyncSupplier<String> asyncSupplier = MoreSuppliers.asyncLazyEx(slowOperation, fallback, "InitThread");
  7. String result = asyncSupplier.get(Duration.ofSeconds(5));  // The slow operation is performed in a separate thread. If it takes more than 5 seconds, the fallback value is returned.

参考:

https://github.com/PhantomThief/more-lambdas-java/blob/master/core/src/main/java/com/github/phantomthief/util/MoreSuppliers.java

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/949044
推荐阅读
相关标签
  

闽ICP备14008679号