Java的MoreSuppliers工具类方法解析

 更新时间:2024年01月16日 08:50:27   作者:加油当当  
这篇文章主要介绍了Java的MoreSuppliers工具类方法解析,MoreSuppliers类是一个Java工具类,它提供了一些增强的Supplier函数,使得Supplier执行的结果可以被缓存,真正的调用只执行一次,需要的朋友可以参考下

MoreSuppliers工具类

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

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

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

示例:

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

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

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

示例:

Supplier<Connection> connectionSupplier = () -> {
    // Get a connection from the database...
    return connection;
};
CloseableSupplier<Connection> lazySupplier = MoreSuppliers.lazy(connectionSupplier, true);
Connection connection = lazySupplier.get();  // The connection is obtained here.
lazySupplier.tryClose(Connection::close);  // The connection is closed here.
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可能抛出一个异常,你希望这个异常能被正确地传播出去。

示例:

ThrowableSupplier<String, IOException> ioOperation = () -> {
    // Some IO operation...
    return "result";
};
ThrowableSupplier<String, IOException> lazySupplier = MoreSuppliers.lazyEx(ioOperation);
try {
    String result = lazySupplier.get();  // The IO operation is performed here.
} catch (IOException e) {
    // Handle the exception...
}

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

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

示例:

ThrowableSupplier<Connection, SQLException> connectionSupplier = () -> {
    // Get a connection from the database...
    return connection;
};
CloseableThrowableSupplier<Connection, SQLException> lazySupplier = MoreSuppliers.lazyEx(connectionSupplier, true);
try {
    Connection connection = lazySupplier.get();  // The connection is obtained here.
    lazySupplier.tryClose(Connection::close);  // The connection is closed here.
    Connection newConnection = lazySupplier.get();  // A new connection is obtained here.
} catch (SQLException e) {
    // Handle the exception...
}

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

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

示例:

Supplier<String> slowOperation = () -> {
    // Some slow operation...
    return "result";
};
Supplier<String> fallback = () -> "fallback";
AsyncSupplier<String> asyncSupplier = MoreSuppliers.asyncLazyEx(slowOperation, fallback, "InitThread");
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。

示例:

Supplier<String> slowOperation = () -> {
    // Some slow operation...
    return "result";
};
AsyncSupplier<String> asyncSupplier = MoreSuppliers.asyncLazyEx(slowOperation, "InitThread");
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):

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

示例:

Supplier<String> slowOperation = () -> {
    // Some slow operation...
    return "result";
};
AsyncSupplier<String> asyncSupplier = MoreSuppliers.asyncLazyEx(slowOperation);
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)方法关闭提供器返回的资源。

示例:

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

CloseableThrowableSupplier<T, X>:

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

示例:

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

AsyncSupplier<T>:

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

示例:

Supplier<String> slowOperation = () -> {
    // Some slow operation...
    return "result";
};
Supplier<String> fallback = () -> "fallback";
AsyncSupplier<String> asyncSupplier = MoreSuppliers.asyncLazyEx(slowOperation, fallback, "InitThread");
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.

到此这篇关于Java的MoreSuppliers工具类方法解析的文章就介绍到这了,更多相关MoreSuppliers工具类内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

最新评论