public final class Optional<T> extends Object
isPresent() will return true and
get() will return the value.
此处简化 jdk8 Optional,提供一个简化版的 Optional提供给 jdk7 使用。便于过渡使用。| 限定符和类型 | 方法和说明 |
|---|---|
static <T> Optional<T> |
empty()
Returns an empty
Optional instance. |
boolean |
equals(Object obj)
Indicates whether some other object is "equal to" this Optional.
|
T |
get()
If a value is present in this
Optional, returns the value,
otherwise throws NoSuchElementException. |
<R> R |
getCastOrNull(Class<R> rClass)
获取转换后的类型。
|
int |
hashCode()
Returns the hash code value of the present value, if any, or 0 (zero) if
no value is present.
|
boolean |
isNotPresent()
Return
true if there is a value not present, otherwise false. |
boolean |
isPresent()
Return
true if there is a value present, otherwise false. |
static <T> Optional<T> |
of(T value)
Returns an
Optional with the specified present non-null value. |
static <T> Optional<T> |
ofNullable(T value)
Returns an
Optional describing the specified value, if non-null,
otherwise returns an empty Optional. |
T |
orDefault(T defaultVal)
Return the contained value, if present, otherwise return default value.
|
T |
orElseNull()
Return the contained value, if present, otherwise return null.
|
<X extends Throwable> |
orElseThrow(X throwable)
Return the contained value, if present, otherwise throw an exception
to be created by the provided supplier.
|
String |
toString()
Returns a non-empty string representation of this Optional suitable for
debugging.
|
public static <T> Optional<T> empty()
Optional instance. No value is present for this
Optional.
Though it may be tempting to do so, avoid testing if an object
is empty by comparing with == against instances returned by
Option.empty(). There is no guarantee that it is a singleton.
Instead, use isPresent().T - Type of the non-existent valueOptionalpublic static <T> Optional<T> of(T value)
Optional with the specified present non-null value.T - the class of the valuevalue - the value to be present, which must be non-nullOptional with the value presentNullPointerException - if value is nullpublic static <T> Optional<T> ofNullable(T value)
Optional describing the specified value, if non-null,
otherwise returns an empty Optional.T - the class of the valuevalue - the possibly-null value to describeOptional with a present value if the specified value
is non-null, otherwise an empty Optionalpublic T get()
Optional, returns the value,
otherwise throws NoSuchElementException.OptionalNoSuchElementException - if there is no value presentisPresent()public <R> R getCastOrNull(Class<R> rClass)
R - 泛型rClass - 类型public boolean isPresent()
true if there is a value present, otherwise false.true if there is a value present, otherwise falsepublic boolean isNotPresent()
true if there is a value not present, otherwise false.true if there is a value not present, otherwise falsepublic <X extends Throwable> T orElseThrow(X throwable) throws X extends Throwable
IllegalStateException::newX - 泛型throwable - Type of the exception to be thrown be thrownX - if there is no value presentNullPointerException - if no value is present and
exceptionSupplier is nullX extends Throwablepublic T orElseNull()
public T orDefault(T defaultVal)
defaultVal - default valuepublic boolean equals(Object obj)
Optional and;
equals().
public int hashCode()
public String toString()
Copyright © 2024. All rights reserved.