Skip to content

Commit ebced83

Browse files
committed
Replacing custom loop by ServiceLoader.findFirst()
1 parent e50c16d commit ebced83

File tree

3 files changed

+6
-15
lines changed

3 files changed

+6
-15
lines changed

jaxrs-api/src/main/java/jakarta/ws/rs/client/FactoryFinder.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2019 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -174,10 +174,7 @@ private static ClassLoader getClassLoader() {
174174
private static <T> T findFirstService(final String factoryId, final ClassLoader cl, final Class<T> service) {
175175
final PrivilegedAction<T> action = () -> {
176176
try {
177-
final ServiceLoader<T> loader = ServiceLoader.load(service, cl);
178-
if (loader.iterator().hasNext()) {
179-
return loader.iterator().next();
180-
}
177+
return ServiceLoader.load(service, cl).findFirst().orElse(null);
181178
} catch (Exception e) {
182179
LOGGER.log(Level.FINER, "Failed to load service " + factoryId + ".", e);
183180
}

jaxrs-api/src/main/java/jakarta/ws/rs/ext/FactoryFinder.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2019 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -174,10 +174,7 @@ private static ClassLoader getClassLoader() {
174174
private static <T> T findFirstService(final String factoryId, final ClassLoader cl, final Class<T> service) {
175175
final PrivilegedAction<T> action = () -> {
176176
try {
177-
final ServiceLoader<T> loader = ServiceLoader.load(service, cl);
178-
if (loader.iterator().hasNext()) {
179-
return loader.iterator().next();
180-
}
177+
return ServiceLoader.load(service, cl).findFirst().orElse(null);
181178
} catch (Exception e) {
182179
LOGGER.log(Level.FINER, "Failed to load service " + factoryId + ".", e);
183180
}

jaxrs-api/src/main/java/jakarta/ws/rs/sse/FactoryFinder.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2019 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -174,10 +174,7 @@ private static ClassLoader getClassLoader() {
174174
private static <T> T findFirstService(final String factoryId, final ClassLoader cl, final Class<T> service) {
175175
final PrivilegedAction<T> action = () -> {
176176
try {
177-
final ServiceLoader<T> loader = ServiceLoader.load(service, cl);
178-
if (loader.iterator().hasNext()) {
179-
return loader.iterator().next();
180-
}
177+
return ServiceLoader.load(service, cl).findFirst().orElse(null);
181178
} catch (Exception e) {
182179
LOGGER.log(Level.FINER, "Failed to load service " + factoryId + ".", e);
183180
}

0 commit comments

Comments
 (0)