Issue #4340 - loadFirst can take a predicate to match a service provider

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2020-02-26 10:35:31 +11:00
parent a277c30b81
commit 67e88abbe9
1 changed files with 21 additions and 2 deletions

View File

@ -44,6 +44,7 @@ import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.ServiceConfigurationError; import java.util.ServiceConfigurationError;
import java.util.ServiceLoader; import java.util.ServiceLoader;
import java.util.function.Predicate;
import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger; import org.eclipse.jetty.util.log.Logger;
@ -790,7 +791,7 @@ public class TypeUtil
} }
/** /**
* Uses the {@link ServiceLoader} to get the first availible service provider. * Uses the {@link ServiceLoader} to get the first available service provider.
* If loading a service type throws {@link ServiceConfigurationError}, * If loading a service type throws {@link ServiceConfigurationError},
* it warns and continues iterating through the service loader until one is found. * it warns and continues iterating through the service loader until one is found.
* @param <T> The class of the service type. * @param <T> The class of the service type.
@ -799,6 +800,21 @@ public class TypeUtil
* @throws ServiceConfigurationError If the number of errors exceeds {@link #MAX_ERRORS} * @throws ServiceConfigurationError If the number of errors exceeds {@link #MAX_ERRORS}
*/ */
public static <T> T loadFirst(ServiceLoader<T> serviceLoader) public static <T> T loadFirst(ServiceLoader<T> serviceLoader)
{
return loadFirst(serviceLoader, t -> true);
}
/**
* Uses the {@link ServiceLoader} to get the first service provider to match the provided predicate.
* If loading a service type throws {@link ServiceConfigurationError},
* it warns and continues iterating through the service loader until one is found.
* @param <T> The class of the service type.
* @param serviceLoader The service loader to use.
* @param predicate The predicate used to match a service provider.
* @return an instance of a service provider, or null if none could be found.
* @throws ServiceConfigurationError If the number of errors exceeds {@link #MAX_ERRORS}
*/
public static <T> T loadFirst(ServiceLoader<T> serviceLoader, Predicate<T> predicate)
{ {
Iterator<T> iterator = serviceLoader.iterator(); Iterator<T> iterator = serviceLoader.iterator();
@ -809,7 +825,10 @@ public class TypeUtil
{ {
if (!iterator.hasNext()) if (!iterator.hasNext())
break; break;
return iterator.next();
T t = iterator.next();
if (predicate.test(t))
return t;
} }
catch (ServiceConfigurationError e) catch (ServiceConfigurationError e)
{ {