trip.spi.SingletonContext Maven / Gradle / Ivy
The newest version!
package trip.spi;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.extern.java.Log;
import trip.spi.helpers.ProvidableClass;
import trip.spi.helpers.QualifierExtractor;
@Log
@RequiredArgsConstructor
public class SingletonContext {
final Map, Object> cache = new HashMap<>();
final Map, ProvidableClass>> providableClassCache = new HashMap<>();
@Setter
QualifierExtractor qualifierExtractor;
@SuppressWarnings("unchecked")
public Iterable instantiate( Iterable> classes ){
final List list = new ArrayList<>();
synchronized ( cache ) {
for ( final Class clazz : classes ) {
T object = (T)cache.get( clazz );
if ( object == null )
cache.put( clazz, object = instantiate( clazz ) );
list.add( object );
}
}
return list;
}
public T instantiate( Class clazz ) {
try {
return clazz.newInstance();
} catch ( final IllegalAccessException | InstantiationException cause ) {
log.finest("Can't instantiate " + clazz + ": " + cause.getMessage());
return null;
}
}
public ProvidableClass> retrieveProvidableClass( final Class> targetClazz ) {
ProvidableClass> providableClass = providableClassCache.get( targetClazz );
if ( providableClass == null )
synchronized ( providableClassCache ) {
providableClass = providableClassCache.get( targetClazz );
if ( providableClass == null ) {
providableClass = ProvidableClass.wrap( qualifierExtractor, targetClazz );
providableClassCache.put( targetClazz, providableClass );
}
}
return providableClass;
}
}