generic refactor.

pull/1036/head
hongdan.qin 3 years ago
parent 97409b9f42
commit c242c9159b

@ -17,6 +17,8 @@
package cn.hippo4j.common.spi; package cn.hippo4j.common.spi;
import cn.hippo4j.common.spi.annotation.SingletonSPI;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
@ -26,14 +28,20 @@ import java.util.ServiceLoader;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import cn.hippo4j.common.spi.annotation.SingletonSPI;
/** /**
* Dynamic thread-pool service loader. * Dynamic thread-pool service loader.
*/ */
public class DynamicThreadPoolServiceLoader { public class DynamicThreadPoolServiceLoader {
private static final Map<Class<?>, Collection<Object>> SERVICES = new ConcurrentHashMap<>(); /**
* safe container
* key : SPI interface class type.
* value : collection whose elements are object of key's class type.
*/
private static final Map<Class<?>, Collection<?>> SERVICES = new ConcurrentHashMap<>();
private DynamicThreadPoolServiceLoader() {
}
/** /**
* Register. * Register.
@ -53,8 +61,8 @@ public class DynamicThreadPoolServiceLoader {
* @param <T> * @param <T>
* @return * @return
*/ */
private static <T> Collection<Object> load(final Class<T> serviceInterface) { private static <T> Collection<T> load(final Class<T> serviceInterface) {
Collection<Object> result = new LinkedList<>(); Collection<T> result = new LinkedList<>();
for (T each : ServiceLoader.load(serviceInterface)) { for (T each : ServiceLoader.load(serviceInterface)) {
result.add(each); result.add(each);
} }
@ -63,7 +71,7 @@ public class DynamicThreadPoolServiceLoader {
/** /**
* Get Service instances * Get Service instances
* *
* @param serviceClass serviceClass * @param serviceClass serviceClass
* @param <T> * @param <T>
* @return * @return

Loading…
Cancel
Save