8291954: Use Optional.isEmpty instead of !Optional.isPresent in java.base

Reviewed-by: jpai, alanb, lancea, rriggs, bpb
This commit is contained in:
Andrey Turbanov 2022-08-06 09:53:35 +00:00
parent 87cda21c5d
commit ae52053757
6 changed files with 21 additions and 23 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -212,7 +212,7 @@ public final class Optional<T> {
*/
public Optional<T> filter(Predicate<? super T> predicate) {
Objects.requireNonNull(predicate);
if (!isPresent()) {
if (isEmpty()) {
return this;
} else {
return predicate.test(value) ? this : empty();
@ -254,7 +254,7 @@ public final class Optional<T> {
*/
public <U> Optional<U> map(Function<? super T, ? extends U> mapper) {
Objects.requireNonNull(mapper);
if (!isPresent()) {
if (isEmpty()) {
return empty();
} else {
return Optional.ofNullable(mapper.apply(value));
@ -282,7 +282,7 @@ public final class Optional<T> {
*/
public <U> Optional<U> flatMap(Function<? super T, ? extends Optional<? extends U>> mapper) {
Objects.requireNonNull(mapper);
if (!isPresent()) {
if (isEmpty()) {
return empty();
} else {
@SuppressWarnings("unchecked")
@ -331,7 +331,7 @@ public final class Optional<T> {
* @since 9
*/
public Stream<T> stream() {
if (!isPresent()) {
if (isEmpty()) {
return Stream.empty();
} else {
return Stream.of(value);