kikaha.core.cdi.helpers.ProducerFactoryMap Maven / Gradle / Ivy
package kikaha.core.cdi.helpers;
import java.lang.reflect.*;
import java.util.*;
import kikaha.core.cdi.*;
import kikaha.core.cdi.helpers.filter.AnyObject;
@SuppressWarnings( "rawtypes" )
public class ProducerFactoryMap {
final Map, List>> producerImplementationClasses = new HashMap<>();
final Map, List>> map = new HashMap<>();
public static ProducerFactoryMap from( final Iterable> iterable ) {
final ProducerFactoryMap providers = new ProducerFactoryMap();
for ( final Class provider : iterable ) {
final Class> clazz = getGenericClassFrom( provider );
providers.memorizeProviderForClazz(provider, clazz);
}
return providers;
}
private static Class> getGenericClassFrom( Class extends ProducerFactory> clazz ) {
final Type[] types = clazz.getGenericInterfaces();
for ( final Type type : types )
if ( ( (ParameterizedType)type ).getRawType().equals( ProducerFactory.class ) )
return (Class>)( (ParameterizedType)type ).getActualTypeArguments()[0];
return null;
}
private void memorizeProviderForClazz( final Class provider, final Class> clazz ) {
List> iterable = producerImplementationClasses.get( clazz );
if ( iterable == null ) {
iterable = new TinyList<>();
producerImplementationClasses.put( clazz, iterable );
}
iterable.add( provider );
}
public ProducerFactory> get( final Class> clazz, final DefaultServiceProvider.DependencyInjector injector ) {
final List> list = getAll( clazz, injector );
if ( list == null || list.isEmpty() )
return null;
return list.get(0);
}
private List> getAll( final Class> clazz, DefaultServiceProvider.DependencyInjector injector ) {
List> list = map.get( clazz );
if ( list == null )
synchronized ( map ) {
list = map.get( clazz );
if ( list == null ) {
list = loadAll( clazz, injector );
map.put( clazz, list );
}
}
return list;
}
private List> loadAll( Class> clazz, DefaultServiceProvider.DependencyInjector injector ) {
final List> list = new TinyList<>();
final List> factories = producerImplementationClasses.get( clazz );
if ( factories != null )
for ( final Class factoryClass : factories )
list.add( injector.load( factoryClass, AnyObject.instance(), EmptyProviderContext.INSTANCE ) );
return list;
}
public void memorizeProviderForClazz( final ProducerFactory> provider, final Class> clazz ) {
synchronized ( map ) {
List> list = map.get( clazz );
if ( list == null ) {
list = new TinyList<>();
map.put( clazz, list );
}
list.add( provider );
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy