Skip to content

Commit ae52053

Browse files
author
Andrey Turbanov
committed
8291954: Use Optional.isEmpty instead of !Optional.isPresent in java.base
Reviewed-by: jpai, alanb, lancea, rriggs, bpb
1 parent 87cda21 commit ae52053

File tree

6 files changed

+21
-23
lines changed

6 files changed

+21
-23
lines changed

src/java.base/share/classes/java/lang/Runtime.java

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1995, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1995, 2022, Oracle and/or its affiliates. All rights reserved.
33
* Copyright (c) 2019, Azul Systems, Inc. All rights reserved.
44
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
55
*
@@ -36,7 +36,6 @@
3636
import java.util.StringTokenizer;
3737

3838
import jdk.internal.access.SharedSecrets;
39-
import jdk.internal.loader.NativeLibrary;
4039
import jdk.internal.reflect.CallerSensitive;
4140
import jdk.internal.reflect.Reflection;
4241

@@ -1075,7 +1074,7 @@ public static Version parse(String s) {
10751074
m.group(VersionPattern.OPT_GROUP));
10761075

10771076
// empty '+'
1078-
if (!build.isPresent()) {
1077+
if (build.isEmpty()) {
10791078
if (m.group(VersionPattern.PLUS_GROUP) != null) {
10801079
if (optional.isPresent()) {
10811080
if (pre.isPresent())
@@ -1087,7 +1086,7 @@ public static Version parse(String s) {
10871086
+ " build or optional components: '" + s + "'");
10881087
}
10891088
} else {
1090-
if (optional.isPresent() && !pre.isPresent()) {
1089+
if (optional.isPresent() && pre.isEmpty()) {
10911090
throw new IllegalArgumentException("optional component"
10921091
+ " must be preceded by a pre-release component"
10931092
+ " or '+': '" + s + "'");
@@ -1353,11 +1352,11 @@ private int compareVersion(Version obj) {
13531352

13541353
private int comparePre(Version obj) {
13551354
Optional<String> oPre = obj.pre();
1356-
if (!pre.isPresent()) {
1355+
if (pre.isEmpty()) {
13571356
if (oPre.isPresent())
13581357
return 1;
13591358
} else {
1360-
if (!oPre.isPresent())
1359+
if (oPre.isEmpty())
13611360
return -1;
13621361
String val = pre.get();
13631362
String oVal = oPre.get();
@@ -1388,11 +1387,11 @@ private int compareBuild(Version obj) {
13881387

13891388
private int compareOptional(Version obj) {
13901389
Optional<String> oOpt = obj.optional();
1391-
if (!optional.isPresent()) {
1390+
if (optional.isEmpty()) {
13921391
if (oOpt.isPresent())
13931392
return -1;
13941393
} else {
1395-
if (!oOpt.isPresent())
1394+
if (oOpt.isEmpty())
13961395
return 1;
13971396
return optional.get().compareTo(oOpt.get());
13981397
}

src/java.base/share/classes/java/lang/invoke/MethodHandles.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -6780,7 +6780,7 @@ private static void loopChecks1cd(List<MethodHandle> pred, List<MethodHandle> fi
67806780
loopReturnType + ")");
67816781
}
67826782

6783-
if (!pred.stream().filter(Objects::nonNull).findFirst().isPresent()) {
6783+
if (pred.stream().noneMatch(Objects::nonNull)) {
67846784
throw newIllegalArgumentException("no predicate found", pred);
67856785
}
67866786
if (pred.stream().filter(Objects::nonNull).map(MethodHandle::type).map(MethodType::returnType).

src/java.base/share/classes/java/lang/module/Resolver.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,7 @@ private Set<ModuleReference> findAll() {
867867
Set<ModuleReference> result = new HashSet<>(beforeModules);
868868
for (ModuleReference mref : afterModules) {
869869
String name = mref.descriptor().name();
870-
if (!beforeFinder.find(name).isPresent()
870+
if (beforeFinder.find(name).isEmpty()
871871
&& findInParent(name) == null) {
872872
result.add(mref);
873873
}

src/java.base/share/classes/java/util/Optional.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -212,7 +212,7 @@ public void ifPresentOrElse(Consumer<? super T> action, Runnable emptyAction) {
212212
*/
213213
public Optional<T> filter(Predicate<? super T> predicate) {
214214
Objects.requireNonNull(predicate);
215-
if (!isPresent()) {
215+
if (isEmpty()) {
216216
return this;
217217
} else {
218218
return predicate.test(value) ? this : empty();
@@ -254,7 +254,7 @@ public Optional<T> filter(Predicate<? super T> predicate) {
254254
*/
255255
public <U> Optional<U> map(Function<? super T, ? extends U> mapper) {
256256
Objects.requireNonNull(mapper);
257-
if (!isPresent()) {
257+
if (isEmpty()) {
258258
return empty();
259259
} else {
260260
return Optional.ofNullable(mapper.apply(value));
@@ -282,7 +282,7 @@ public <U> Optional<U> map(Function<? super T, ? extends U> mapper) {
282282
*/
283283
public <U> Optional<U> flatMap(Function<? super T, ? extends Optional<? extends U>> mapper) {
284284
Objects.requireNonNull(mapper);
285-
if (!isPresent()) {
285+
if (isEmpty()) {
286286
return empty();
287287
} else {
288288
@SuppressWarnings("unchecked")
@@ -331,7 +331,7 @@ public Optional<T> or(Supplier<? extends Optional<? extends T>> supplier) {
331331
* @since 9
332332
*/
333333
public Stream<T> stream() {
334-
if (!isPresent()) {
334+
if (isEmpty()) {
335335
return Stream.empty();
336336
} else {
337337
return Stream.of(value);

src/java.base/share/classes/jdk/internal/module/ModuleBootstrap.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import java.util.Collections;
3939
import java.util.HashMap;
4040
import java.util.HashSet;
41-
import java.util.Iterator;
4241
import java.util.LinkedHashMap;
4342
import java.util.List;
4443
import java.util.Map;
@@ -395,7 +394,7 @@ private static ModuleLayer boot2() {
395394
if (isPatched) {
396395
patcher.patchedModules()
397396
.stream()
398-
.filter(mn -> !cf.findModule(mn).isPresent())
397+
.filter(mn -> cf.findModule(mn).isEmpty())
399398
.forEach(mn -> warnUnknownModule(PATCH_MODULE, mn));
400399
}
401400

@@ -427,7 +426,7 @@ private static ModuleLayer boot2() {
427426
if (upgradeModulePath != null
428427
&& upgradeModulePath.find(name).isPresent())
429428
fail(name + ": cannot be loaded from upgrade module path");
430-
if (!systemModuleFinder.find(name).isPresent())
429+
if (systemModuleFinder.find(name).isEmpty())
431430
fail(name + ": cannot be loaded from application module path");
432431
}
433432
}
@@ -658,7 +657,7 @@ private static void addExtraReads(ModuleLayer bootLayer) {
658657
// the key is $MODULE
659658
String mn = e.getKey();
660659
Optional<Module> om = bootLayer.findModule(mn);
661-
if (!om.isPresent()) {
660+
if (om.isEmpty()) {
662661
warnUnknownModule(ADD_READS, mn);
663662
continue;
664663
}
@@ -728,7 +727,7 @@ private static void addExtraExportsOrOpens(ModuleLayer bootLayer,
728727
// The exporting module is in the boot layer
729728
Module m;
730729
Optional<Module> om = bootLayer.findModule(mn);
731-
if (!om.isPresent()) {
730+
if (om.isEmpty()) {
732731
warnUnknownModule(option, mn);
733732
continue;
734733
}

src/java.base/share/classes/sun/launcher/LauncherHelper.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ private static Class<?> loadModuleMainClass(String what) {
726726
// main module is in the boot layer
727727
ModuleLayer layer = ModuleLayer.boot();
728728
Optional<Module> om = layer.findModule(mainModule);
729-
if (!om.isPresent()) {
729+
if (om.isEmpty()) {
730730
// should not happen
731731
throw new InternalError("Module " + mainModule + " not in boot Layer");
732732
}
@@ -735,7 +735,7 @@ private static Class<?> loadModuleMainClass(String what) {
735735
// get main class
736736
if (mainClass == null) {
737737
Optional<String> omc = m.getDescriptor().mainClass();
738-
if (!omc.isPresent()) {
738+
if (omc.isEmpty()) {
739739
abort(null, "java.launcher.module.error1", mainModule);
740740
}
741741
mainClass = omc.get();
@@ -1023,7 +1023,7 @@ private static void setFXLaunchParameters(String what, int mode) {
10231023

10241024
// find the module with the FX launcher
10251025
Optional<Module> om = ModuleLayer.boot().findModule(JAVAFX_GRAPHICS_MODULE_NAME);
1026-
if (!om.isPresent()) {
1026+
if (om.isEmpty()) {
10271027
abort(null, "java.launcher.cls.error5");
10281028
}
10291029

0 commit comments

Comments
 (0)