【源码 JDK 1.8】Optional的源码解读

本文介绍了Java中的Optional类,一个可以包含或不包含非空值的对象容器,提供isPresent(),get(),ifPresent(),filter(),map(),flatMap()等方法处理值的存在性,并演示了如何使用这些方法进行值的处理和默认值的选择。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



/**
 * A container object which may or may not contain a non-null value.
 * 一个对象的容器,可以包含一个非空值,也可以不包含.
 * 
 * If a value is present, {@code isPresent()} will return {@code true} and
 * {@code get()} will return the value.
 * 如果对象存在(即不为null),则isPresent()方法返回true,并且调用get()方法将返回该对象.
 *
 * <p>Additional methods that depend on the presence or absence of a contained
 * value are provided, such as {@link #orElse(java.lang.Object) orElse()}
 * (return a default value if value not present) and
 * {@link #ifPresent(java.util.function.Consumer) ifPresent()} (execute a block
 * of code if the value is present).
 * 并且提供了一系列的依赖对象是否存在的方法,例如orElse()方法(如果对象不存在,则返回一个默认值)和ifPresent()方法(如果存在则执行
 * 一段代码)
 *
 * <p>This is a <a href="../lang/doc-files/ValueBased.html">value-based</a>
 * class; use of identity-sensitive operations (including reference equality
 * ({@code ==}), identity hash code, or synchronization) on instances of
 * {@code Optional} may have unpredictable results and should be avoided.
 * 这是一个值基类,在Optional实例上使用标识敏感操作(包括引用相等(==)、哈希值或者synchronization)可能会产生不可预测的结果,
 * 应避免.
 * Java中的值基类(Value-Based Classes)是用于封装值的类,它们的主要目的是提供一种安全、不可变的方式来存储和操作值。关于值基
 * 的详细后面再讨论
 */
public final class Optional<T> {
    /**
     * empty实例
     */
    private static final Optional<?> EMPTY = new Optional<>();

    /**
     * 实际的对象值
     */
    private final T value;

    /**
     * 构建一个empty实例,为了保证单一实例,定义为private
     */
    private Optional() {
        this.value = null;
    }

    /**
     * 返回一个empty的Optional实例
     * 虽然使用了单例,但是不要使用==进行空值的比较,如果需要判断为空,使用isPresent()方法
     */
    public static<T> Optional<T> empty() {
        @SuppressWarnings("unchecked")
        Optional<T> t = (Optional<T>) EMPTY;
        return t;
    }

    /**
     * 构造方法。创建一个指定值对象的实例,此处只能传入一个non-null的值,如果传入null则会报错
     * NollPointerException
     * 这里使用private将构造方法私有化,主要出于一下几个目的:
     * 1、设计原则:单一职责,Optional作为一个值容器,其构造函数应该只有Optional类本身使用,不应给其他类使用;
     * 2、防止滥用:如果将Optional的构造方法公开,那么任何类都可以实例化Optional,这可能导致不必要的滥用。将构造函数设置成私有,确保Optional只能通过静态工厂方法(Optional.empty()、Optional.of())进行实例化,能更好的使用Optional;
     * 3、简化API:限制构造函数的访问,可以简化API使其更易于理解。外部代码只需要知道如何使用静态工厂方法,而不是直接使用构造函数实例化.
     */
    private Optional(T value) {
        this.value = Objects.requireNonNull(value);
    }

    /**
     * 静态工厂方法,返回一个包装non-null值的Optional实例
     * 
     * @param <T> the class of the value
     * @param value the value to be present, which must be non-null
     * @return an {@code Optional} with the value present
     * @throws NullPointerException if value is null
     */
    public static <T> Optional<T> of(T value) {
        return new Optional<>(value);
    }

    /**
     * 静态工厂方法,返回一个封装值的Optional。传入的值可以为空,为空则返回empty;
     * 当你传入的参数可能为null时,就使用这个构造方法
     *
     * @param <T> the class of the value
     * @param value the possibly-null value to describe
     * @return an {@code Optional} with a present value if the specified value
     * is non-null, otherwise an empty {@code Optional}
     */
    public static <T> Optional<T> ofNullable(T value) {
        return value == null ? empty() : of(value);
    }

    /**
     * 如果值存在,则返回该值。否则抛出NoSuchElementException异常;
     * 所以get()方法通常和isPresent()方法一起使用,但是这不是一个Optional推荐的使用方法,
     * 因为这样和直接使用if(value != null) 判断差不多
     *
     * @return the non-null value held by this {@code Optional}
     * @throws NoSuchElementException if there is no value present
     *
     * @see Optional#isPresent()
     */
    public T get() {
        if (value == null) {
            throw new NoSuchElementException("No value present");
        }
        return value;
    }

    /**
     * Return {@code true} if there is a value present, otherwise {@code false}.
     * 判断值是否为null的方法,如果值不为null,则返回true,否则返回false
     *
     * @return {@code true} if there is a value present, otherwise {@code false}
     */
    public boolean isPresent() {
        return value != null;
    }

    /**
     * If a value is present, invoke the specified consumer with the value,
     * otherwise do nothing.
     * 如果值不为null,则通过传入的Consumer对值进行处理。否则将不执行任何事情
     * Optional.ofNullable(obj).ifPresent(() -> {
     *     // Do SomeThing
     * })
     * 
     * Consumer是Java8的新内容:lambda表达式
     *
     * @param consumer block to be executed if a value is present
     * @throws NullPointerException if value is present and {@code consumer} is
     * null
     */
    public void ifPresent(Consumer<? super T> consumer) {
        if (value != null)
            consumer.accept(value);
    }

    /**
     * If a value is present, and the value matches the given predicate,
     * return an {@code Optional} describing the value, otherwise return an
     * empty {@code Optional}.
     * 这里的filter和stream中的filter差不多。如果值不为null,并且使用Predicate对值匹配成功,则返回该值
     * 否则返回Optional.empty();
     * 
     * Predicate是Java8的新内容:lambda表达式,通常用于测试或匹配某个条件
     *
     * @param predicate a predicate to apply to the value, if present
     * @return an {@code Optional} describing the value of this {@code Optional}
     * if a value is present and the value matches the given predicate,
     * otherwise an empty {@code Optional}
     * @throws NullPointerException if the predicate is null
     */
    public Optional<T> filter(Predicate<? super T> predicate) {
        // 判断传入的Predicate是否为null
        Objects.requireNonNull(predicate);
        if (!isPresent())
        // 如果value == null,则返回自身相当于返回一个empty();
            return this;
        else
        // value != null,使用Predicate进行匹配,成功则返回自身,不成功返回empty
            return predicate.test(value) ? this : empty();
    }

    /**
     * If a value is present, apply the provided mapping function to it,
     * and if the result is non-null, return an {@code Optional} describing the
     * result.  Otherwise return an empty {@code Optional}.
     * 加工方法,如果值不为null,并且使用Function对值进行加工,并将加工后的值封装到Optional中
     * 否则返回Optional.empty();
     * 这个方法支持对值的后处理,即不用显示地检查值是否为null;简单来说就是值存在才进行处理,
     * 值不存在直接返回empty;使用者无需再手动的判断(可以参考下面官方给出的例子)。
     * 要注意的是,这里返回的是一个Optional类型,所以当你定义Functions时如果返回的是一个Optional类型,会被封装两次,如果不想被再度封装使用下一个方法Optional.flatMap();
     * 
     * 
     * 
     * @apiNote This method supports post-processing on optional values, without
     * the need to explicitly check for a return status.  For example, the
     * following code traverses a stream of file names, selects one that has
     * not yet been processed, and then opens that file, returning an
     * {@code Optional<FileInputStream>}:
     *
     * <pre>{@code
     *     Optional<FileInputStream> fis =
     *         names.stream().filter(name -> !isProcessedYet(name))
     *                       .findFirst()
     *                       .map(name -> new FileInputStream(name));
     * }</pre>
     *
     * Here, {@code findFirst} returns an {@code Optional<String>}, and then
     * {@code map} returns an {@code Optional<FileInputStream>} for the desired
     * file if one exists.
     *
     * @param <U> The type of the result of the mapping function
     * @param mapper a mapping function to apply to the value, if present
     * @return an {@code Optional} describing the result of applying a mapping
     * function to the value of this {@code Optional}, if a value is present,
     * otherwise an empty {@code Optional}
     * @throws NullPointerException if the mapping function is null
     */
    public<U> Optional<U> map(Function<? super T, ? extends U> mapper) {
        Objects.requireNonNull(mapper);
        if (!isPresent())
            return empty();
        else {
            return Optional.ofNullable(mapper.apply(value));
        }
    }

    /**
     * If a value is present, apply the provided {@code Optional}-bearing
     * mapping function to it, return that result, otherwise return an empty
     * {@code Optional}.  This method is similar to {@link #map(Function)},
     * but the provided mapper is one whose result is already an {@code Optional},
     * and if invoked, {@code flatMap} does not wrap it with an additional
     * {@code Optional}.
     * 
     * 这个方法和map()方法类似,区别就是这个方法Funciton已经返回了一个Optional对象
     * 所以不会再将值封装到一个新的Optional中了
     *
     * @param <U> The type parameter to the {@code Optional} returned by
     * @param mapper a mapping function to apply to the value, if present
     *           the mapping function
     * @return the result of applying an {@code Optional}-bearing mapping
     * function to the value of this {@code Optional}, if a value is present,
     * otherwise an empty {@code Optional}
     * @throws NullPointerException if the mapping function is null or returns
     * a null result
     */
    public<U> Optional<U> flatMap(Function<? super T, Optional<U>> mapper) {
        Objects.requireNonNull(mapper);
        if (!isPresent())
            return empty();
        else {
            return Objects.requireNonNull(mapper.apply(value));
        }
    }

    /**
     * Return the value if present, otherwise return {@code other}.
     * value != null,则返回value,否则返回other
     * 这个方法和orElseGet区别是:这个不管是不是null都会获取other;
     * 因此如果获取other的方法计算量较大,最好使用orElseGet()
     *
     * @param other the value to be returned if there is no value present, may
     * be null
     * @return the value, if present, otherwise {@code other}
     */
    public T orElse(T other) {
        return value != null ? value : other;
    }

    /**
     * Return the value if present, otherwise invoke {@code other} and return
     * the result of that invocation.
     * value != null,则返回value,否则返回other函数获取该方法的结果
     *
     * @param other a {@code Supplier} whose result is returned if no value
     * is present
     * @return the value if present otherwise the result of {@code other.get()}
     * @throws NullPointerException if value is not present and {@code other} is
     * null
     */
    public T orElseGet(Supplier<? extends T> other) {
        return value != null ? value : other.get();
    }

    /**
     * Return the contained value, if present, otherwise throw an exception
     * to be created by the provided supplier.
     * value != null,则返回value,否则执行exceptionSupplier函数,生成并抛出一个异常
     *
     * @apiNote A method reference to the exception constructor with an empty
     * argument list can be used as the supplier. For example,
     * {@code IllegalStateException::new}
     *
     * @param <X> Type of the exception to be thrown
     * @param exceptionSupplier The supplier which will return the exception to
     * be thrown
     * @return the present value
     * @throws X if there is no value present
     * @throws NullPointerException if no value is present and
     * {@code exceptionSupplier} is null
     */
    public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
        if (value != null) {
            return value;
        } else {
            throw exceptionSupplier.get();
        }
    }

    /**
     * Indicates whether some other object is "equal to" this Optional. The
     * other object is considered equal if:
     * <ul>
     * <li>it is also an {@code Optional} and;
     * <li>both instances have no value present or;
     * <li>the present values are "equal to" each other via {@code equals()}.
     * </ul>
     *
     * @param obj an object to be tested for equality
     * @return {code true} if the other object is "equal to" this object
     * otherwise {@code false}
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }

        if (!(obj instanceof Optional)) {
            return false;
        }

        Optional<?> other = (Optional<?>) obj;
        return Objects.equals(value, other.value);
    }

    /**
     * Returns the hash code value of the present value, if any, or 0 (zero) if
     * no value is present.
     *
     * @return hash code value of the present value or 0 if no value is present
     */
    @Override
    public int hashCode() {
        return Objects.hashCode(value);
    }

    /**
     * Returns a non-empty string representation of this Optional suitable for
     * debugging. The exact presentation format is unspecified and may vary
     * between implementations and versions.
     *
     * @implSpec If a value is present the result must include its string
     * representation in the result. Empty and present Optionals must be
     * unambiguously differentiable.
     *
     * @return the string representation of this instance
     */
    @Override
    public String toString() {
        return value != null
            ? String.format("Optional[%s]", value)
            : "Optional.empty";
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值