Interface Option<T>
- Type Parameters:
T- value type - 值类型
- All Known Implementing Classes:
Option.None, Option.Some
Option Monad - Enhanced Optional with more functional operations
Option Monad - 增强的 Optional,提供更多函数式操作
A sealed type representing an optional value - either Option.Some containing
a value or Option.None representing absence.
一个密封类型表示可选值 - Option.Some 包含值或 Option.None 表示缺失。
Features | 主要功能:
- Sealed type with Some/None - 带 Some/None 的密封类型
- Monadic operations (map, flatMap, filter) - Monad 操作
- Interoperability with Optional - 与 Optional 互操作
- Pattern matching friendly - 模式匹配友好
Usage Examples | 使用示例:
// Creating Options
Option<String> some = Option.some("value");
Option<String> none = Option.none();
Option<String> fromNullable = Option.of(nullableValue);
// Chained operations
String result = Option.of(user)
.map(User::getAddress)
.flatMap(addr -> Option.of(addr.getCity()))
.map(String::toUpperCase)
.getOrElse("UNKNOWN");
// Pattern matching with JDK 25
String desc = switch (option) {
case Option.Some(var v) -> "Has: " + v;
case Option.None() -> "Empty";
};
vs java.util.Optional | 与 Optional 对比:
- Sealed type enables pattern matching - 密封类型支持模式匹配
- Some/None explicitly typed - Some/None 显式类型
- Richer API - 更丰富的 API
Security | 安全性:
- Thread-safe: Yes (immutable) - 线程安全: 是 (不可变)
- Null-safe: None for null - 空值安全: null 返回 None
- Since:
- JDK 25, opencode-base-functional V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic final classNone - Represents absence of value None - 表示值缺失static final recordSome - Contains a value Some - 包含值 -
Method Summary
Modifier and TypeMethodDescriptiondefault booleanCheck if this is a Some containing a value equal to the given value.default booleanCheck if this is a Some and the predicate matches the contained value.Filter the value with predicate 使用谓词过滤值<U> Option<U> Transform to another Option if Some 如果是 Some 则转换为另一个 Option<R> RFold this Option by applying one of two functions 通过应用两个函数之一来折叠此 Optiondefault booleanCheck if the predicate holds for all values.static <T> Option<T> fromOptional(Optional<T> optional) Create Option from Optional 从 Optional 创建 Optionget()Get the value, throws if None 获取值,如果是 None 则抛出异常Get value or compute default if None 获取值或计算默认值(如果是 None)Get value or default if None 获取值或默认值(如果是 None)booleanisNone()Check if this is None 检查是否为 NonebooleanisSome()Check if this is Some 检查是否为 Some<U> Option<U> Transform the value if Some 如果是 Some 则转换值static <T> Option<T> none()Create a None 创建 Nonestatic <T> Option<T> of(T value) Create Option from nullable value 从可空值创建 OptionExecute action if None 如果是 None 则执行操作Return this or other Option if None 返回本 Option 或其他 Option(如果是 None)Return this or computed Option if None 返回本 Option 或计算的 Option(如果是 None)Execute action if Some 如果是 Some 则执行操作static <T> Option<T> some(T value) Create a Some with value 创建包含值的 Somestream()Convert to Stream.toEither(L left) Convert to Either (None becomes Left) 转换为 Either(None 变为 Left)Convert to Optional 转换为 OptionalConvert to Try.default <E> Validation<E, T> toValidation(E error) Convert to Validation.static <T> Option<T> Create Option from supplier if condition is true 如果条件为真则从供应商创建 Optiondefault <U,R> Option <R> zip(Option<U> other, BiFunction<? super T, ? super U, ? extends R> zipper) Zip this Option with another using a combining function.
-
Method Details
-
some
Create a Some with value 创建包含值的 Some- Type Parameters:
T- value type - 值类型- Parameters:
value- the value (must not be null) - 值(不能为 null)- Returns:
- Some containing the value
- Throws:
NullPointerException- if value is null
-
none
Create a None 创建 None- Type Parameters:
T- value type - 值类型- Returns:
- None instance
-
of
Create Option from nullable value 从可空值创建 Option- Type Parameters:
T- value type - 值类型- Parameters:
value- nullable value - 可空值- Returns:
- Some if value not null, None otherwise
-
fromOptional
-
when
Create Option from supplier if condition is true 如果条件为真则从供应商创建 Option- Type Parameters:
T- value type - 值类型- Parameters:
condition- condition - 条件supplier- value supplier - 值供应商- Returns:
- Some if condition true and value not null, None otherwise
-
isSome
boolean isSome()Check if this is Some 检查是否为 Some- Returns:
- true if Some - 如果是 Some 返回 true
-
isNone
boolean isNone()Check if this is None 检查是否为 None- Returns:
- true if None - 如果是 None 返回 true
-
get
T get()Get the value, throws if None 获取值,如果是 None 则抛出异常- Returns:
- the value - 值
- Throws:
NoSuchElementException- if None
-
map
-
flatMap
-
filter
-
fold
Fold this Option by applying one of two functions 通过应用两个函数之一来折叠此 OptionThis is the catamorphism for Option - handles both cases symmetrically.
这是 Option 的态射 - 对称地处理两种情况。
Example | 示例:
String result = option.fold( () -> "No value", // None case value -> "Got: " + value // Some case );- Type Parameters:
R- result type - 结果类型- Parameters:
ifNone- function for None case - None 情况的函数ifSome- function for Some case - Some 情况的函数- Returns:
- result of applying the appropriate function
-
getOrElse
-
getOrElse
-
orElse
-
orElse
-
toOptional
-
toEither
-
peek
-
onNone
-
contains
Check if this is a Some containing a value equal to the given value. 检查是否为包含与给定值相等的值的 Some。- Parameters:
value- the value to compare - 要比较的值- Returns:
- true if Some and value equals contained value | 如果是 Some 且值相等返回 true
-
exists
-
forAll
-
toTry
Convert to Try. Some maps to Try.success(value), None maps to Try.failure with the supplied exception. 转换为 Try。Some 映射为 Try.success(value),None 映射为带有提供异常的 Try.failure。- Parameters:
exceptionSupplier- supplier for the exception when None - None 时异常的供应商- Returns:
- Try containing the value or failure | 包含值的 Try 或失败
-
stream
-
zip
Zip this Option with another using a combining function. Both must be Some for the result to be Some. 使用组合函数将此 Option 与另一个 Option 进行组合。 两者都必须是 Some 才能得到 Some 结果。- Type Parameters:
U- the other value type - 另一个值类型R- the result type - 结果类型- Parameters:
other- the other Option - 另一个 Optionzipper- the combining function - 组合函数- Returns:
- Some with combined value if both are Some, None otherwise | 如果两者都是 Some 返回组合值的 Some,否则返回 None
-
toValidation
Convert to Validation. Some maps to Validation.valid(value), None maps to Validation.invalid(error). 转换为 Validation。Some 映射为 Validation.valid(value),None 映射为 Validation.invalid(error)。- Type Parameters:
E- error type - 错误类型- Parameters:
error- the error value for None case - None 情况下的错误值- Returns:
- Validation containing the value or error | 包含值或错误的 Validation
-