Class OptionalUtil
java.lang.Object
cloud.opencode.base.core.stream.OptionalUtil
Optional Utility - Enhanced Optional operations
Optional 工具类 - 增强的 Optional 操作
Provides extended operations for Java Optional beyond the standard API.
提供超越标准 API 的 Java Optional 扩展操作。
Features | 主要功能:
- First present selection (firstPresent, firstPresentLazy) - 第一个存在值选择
- Multiple Optional combination (combine, combine3) - 多个 Optional 组合
- Presence checks (allPresent, anyPresent) - 存在性检查
- Flatten nested Optional - 扁平化嵌套 Optional
Usage Examples | 使用示例:
// First present - 第一个存在值
Optional<String> first = OptionalUtil.firstPresent(opt1, opt2, opt3);
// Combine two - 组合两个
Optional<String> combined = OptionalUtil.combine(opt1, opt2, (a, b) -> a + b);
// Check all present - 检查全部存在
boolean allOk = OptionalUtil.allPresent(opt1, opt2, opt3);
Security | 安全性:
- Thread-safe: Yes (stateless) - 线程安全: 是 (无状态)
- Null-safe: Yes - 空值安全: 是
- Since:
- JDK 25, opencode-base-core V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceThree-argument function interface. -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanallPresent(Optional<?>... optionals) Checks if all Optionals have a value present 检查所有 Optional 是否都存在值static booleananyPresent(Optional<?>... optionals) Checks if any Optional has a value present 检查是否有任意 Optional 存在值static <T,U, R> Optional <R> combine(Optional<T> opt1, Optional<U> opt2, BiFunction<T, U, R> combiner) Combines two Optionals 组合两个 Optionalstatic <T,U, V, R> Optional <R> combine3(Optional<T> opt1, Optional<U> opt2, Optional<V> opt3, OptionalUtil.TriFunction<T, U, V, R> combiner) Combines three Optionals 组合三个 Optionalstatic <T,R> Optional <R> filterAndMap(Optional<T> optional, Predicate<T> predicate, Function<T, R> mapper) Filters and maps 过滤并映射static <T> Optional<T> firstPresent(Optional<T>... optionals) Returns the first present Optional 返回第一个存在值的 Optionalstatic <T> Optional<T> firstPresentLazy(Supplier<Optional<T>>... suppliers) Returns the first present Optional (using Supplier for lazy evaluation) 返回第一个存在值的 Optional(使用 Supplier 延迟计算)static <T,R> Optional <R> flatMapNullable(Optional<T> optional, Function<T, R> mapper) Transforms the value inside Optional, handling null 转换 Optional 内的值,处理 nullstatic <T> Optional<T> FlattensOptional<Optional<T>>toOptional<T>.static <T> voidifPresentOrElse(Optional<T> optional, Consumer<T> action, Runnable emptyAction) Executes an action if the value is present 执行操作,如果存在值static <T,R> Optional <R> mapIfPresent(Optional<T> optional, Function<T, R> mapper) Maps if present 存在时映射static <T> TGets the value or computes a default 获取值或计算默认值static <T, X extends Throwable>
TorElseThrow(Optional<T> optional, Supplier<X> exceptionSupplier) Throws an exception if empty 如果为空则抛出异常static <T> TGets the value or returns null 获取值或返回 nullstatic <T> Stream<T> Converts to Stream 转为 Stream
-
Method Details
-
firstPresent
Returns the first present Optional 返回第一个存在值的 Optional -
firstPresentLazy
Returns the first present Optional (using Supplier for lazy evaluation) 返回第一个存在值的 Optional(使用 Supplier 延迟计算) -
mapIfPresent
-
stream
-
combine
public static <T,U, Optional<R> combineR> (Optional<T> opt1, Optional<U> opt2, BiFunction<T, U, R> combiner) Combines two Optionals 组合两个 Optional -
combine3
public static <T,U, Optional<R> combine3V, R> (Optional<T> opt1, Optional<U> opt2, Optional<V> opt3, OptionalUtil.TriFunction<T, U, V, R> combiner) Combines three Optionals 组合三个 Optional -
orElseGet
-
orNull
Gets the value or returns null 获取值或返回 null -
orElseThrow
-
flatMapNullable
-
filterAndMap
-
ifPresentOrElse
-
flatten
FlattensOptional<Optional<T>>toOptional<T>. 将Optional<Optional<T>>扁平化为Optional<T>。- Type Parameters:
T- the value type | 值类型- Parameters:
optional- the nested optional to flatten | 要扁平化的嵌套 Optional- Returns:
- the flattened optional | 扁平化后的 Optional
-
allPresent
Checks if all Optionals have a value present 检查所有 Optional 是否都存在值 -
anyPresent
Checks if any Optional has a value present 检查是否有任意 Optional 存在值
-