forked from junit-team/junit5
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Java 9+ variant for optimized ServiceLoader filtering
Issue: junit-team#3717 Signed-off-by: yongjunhong <[email protected]>
- Loading branch information
Showing
4 changed files
with
119 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
junit-platform-commons/src/main/java/org/junit/platform/commons/util/ServiceLoaderUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright 2015-2024 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package org.junit.platform.commons.util; | ||
|
||
import java.util.ServiceLoader; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Stream; | ||
import java.util.stream.StreamSupport; | ||
|
||
import org.apiguardian.api.API; | ||
|
||
/** | ||
* Collection of utilities for working with {@link ServiceLoader}. | ||
* | ||
* <h2>DISCLAIMER</h2> | ||
* | ||
* <p>These utilities are intended solely for usage within the JUnit framework | ||
* itself. <strong>Any usage by external parties is not supported.</strong> | ||
* Use at your own risk! | ||
* | ||
* @since 5.11 | ||
*/ | ||
@API(status = API.Status.INTERNAL, since = "5.11") | ||
public final class ServiceLoaderUtils { | ||
|
||
private ServiceLoaderUtils() { | ||
/* no-op */ | ||
} | ||
|
||
/** | ||
* Loads services of the given type using the specified class loader and filters them using the provided predicate. | ||
* | ||
* @param <T> the type of the service | ||
* @param service the class of the service to be loaded | ||
* @param providerPredicate the predicate to filter the loaded services | ||
* @param loader the class loader to be used to load the services | ||
* @return a stream of loaded services that match the predicate | ||
*/ | ||
public static <T> Stream<T> load(Class<T> service, Predicate<? super Class<? extends T>> providerPredicate, | ||
ClassLoader loader) { | ||
|
||
return StreamSupport.stream(ServiceLoader.load(service, loader).spliterator(), false) | ||
.filter(it -> { | ||
@SuppressWarnings("unchecked") Class<? extends T> type = (Class<? extends T>) it.getClass(); | ||
return providerPredicate.test(type); | ||
}); | ||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
...t-platform-commons/src/main/java9/org/junit/platform/commons/util/ServiceLoaderUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright 2015-2024 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package org.junit.platform.commons.util; | ||
|
||
import java.util.ServiceLoader; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Stream; | ||
|
||
import org.apiguardian.api.API; | ||
import org.apiguardian.api.API.Status; | ||
|
||
/** | ||
* Collection of utilities for working with {@link ServiceLoader}. | ||
* | ||
* <h2>DISCLAIMER</h2> | ||
* | ||
* <p>These utilities are intended solely for usage within the JUnit framework | ||
* itself. <strong>Any usage by external parties is not supported.</strong> | ||
* Use at your own risk! | ||
* | ||
* @since 5.11 | ||
*/ | ||
@API(status = Status.INTERNAL, since = "5.11") | ||
public class ServiceLoaderUtils { | ||
|
||
/** | ||
* Loads services of the given type using the specified class loader and filters them using the provided predicate. | ||
* | ||
* @param <T> the type of the service | ||
* @param service the class of the service to be loaded | ||
* @param providerPredicate the predicate to filter the loaded services | ||
* @param loader the class loader to be used to load the services | ||
* @return a stream of loaded services that match the predicate | ||
*/ | ||
public static <T> Stream<T> load(Class<T> service, Predicate<? super Class<? extends T>> providerPredicate, | ||
ClassLoader loader) { | ||
return ServiceLoader.load(service, loader).stream() | ||
.filter(provider -> providerPredicate.test(provider.type())) | ||
.map(ServiceLoader.Provider::get); | ||
} | ||
|
||
} |