From ebced83761f9ffa59bdfc34c6ad2c4fddd71bbe7 Mon Sep 17 00:00:00 2001 From: Markus KARG Date: Sat, 21 Dec 2024 23:17:28 +0000 Subject: [PATCH] Replacing custom loop by ServiceLoader.findFirst() --- .../src/main/java/jakarta/ws/rs/client/FactoryFinder.java | 7 ++----- .../src/main/java/jakarta/ws/rs/ext/FactoryFinder.java | 7 ++----- .../src/main/java/jakarta/ws/rs/sse/FactoryFinder.java | 7 ++----- 3 files changed, 6 insertions(+), 15 deletions(-) diff --git a/jaxrs-api/src/main/java/jakarta/ws/rs/client/FactoryFinder.java b/jaxrs-api/src/main/java/jakarta/ws/rs/client/FactoryFinder.java index 74deb0599..237e3f793 100644 --- a/jaxrs-api/src/main/java/jakarta/ws/rs/client/FactoryFinder.java +++ b/jaxrs-api/src/main/java/jakarta/ws/rs/client/FactoryFinder.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010, 2019 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2010 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0, which is available at @@ -174,10 +174,7 @@ private static ClassLoader getClassLoader() { private static T findFirstService(final String factoryId, final ClassLoader cl, final Class service) { final PrivilegedAction action = () -> { try { - final ServiceLoader loader = ServiceLoader.load(service, cl); - if (loader.iterator().hasNext()) { - return loader.iterator().next(); - } + return ServiceLoader.load(service, cl).findFirst().orElse(null); } catch (Exception e) { LOGGER.log(Level.FINER, "Failed to load service " + factoryId + ".", e); } diff --git a/jaxrs-api/src/main/java/jakarta/ws/rs/ext/FactoryFinder.java b/jaxrs-api/src/main/java/jakarta/ws/rs/ext/FactoryFinder.java index bbd5afd96..bd46f79e3 100644 --- a/jaxrs-api/src/main/java/jakarta/ws/rs/ext/FactoryFinder.java +++ b/jaxrs-api/src/main/java/jakarta/ws/rs/ext/FactoryFinder.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010, 2019 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2010 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0, which is available at @@ -174,10 +174,7 @@ private static ClassLoader getClassLoader() { private static T findFirstService(final String factoryId, final ClassLoader cl, final Class service) { final PrivilegedAction action = () -> { try { - final ServiceLoader loader = ServiceLoader.load(service, cl); - if (loader.iterator().hasNext()) { - return loader.iterator().next(); - } + return ServiceLoader.load(service, cl).findFirst().orElse(null); } catch (Exception e) { LOGGER.log(Level.FINER, "Failed to load service " + factoryId + ".", e); } diff --git a/jaxrs-api/src/main/java/jakarta/ws/rs/sse/FactoryFinder.java b/jaxrs-api/src/main/java/jakarta/ws/rs/sse/FactoryFinder.java index 40440ae87..a9d2415c4 100644 --- a/jaxrs-api/src/main/java/jakarta/ws/rs/sse/FactoryFinder.java +++ b/jaxrs-api/src/main/java/jakarta/ws/rs/sse/FactoryFinder.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010, 2019 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2010 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0, which is available at @@ -174,10 +174,7 @@ private static ClassLoader getClassLoader() { private static T findFirstService(final String factoryId, final ClassLoader cl, final Class service) { final PrivilegedAction action = () -> { try { - final ServiceLoader loader = ServiceLoader.load(service, cl); - if (loader.iterator().hasNext()) { - return loader.iterator().next(); - } + return ServiceLoader.load(service, cl).findFirst().orElse(null); } catch (Exception e) { LOGGER.log(Level.FINER, "Failed to load service " + factoryId + ".", e); }