de.leanovate.pragmatic.ioc.ScopedInjector Maven / Gradle / Ivy
The newest version!
package de.leanovate.pragmatic.ioc;
import java.util.Optional;
import java.util.function.Supplier;
/**
* Generic scope based injector.
*
* @param the type to be injected
*/
public class ScopedInjector implements Injector {
private final Class injectedClass;
private final Optional name;
private final Supplier scopeSupplier;
private final Supplier extends T> supplier;
/**
* Standard constructor.
*
* @param injectedClass class of {@code T}
* @param name the name inside the scope
* @param scopeSupplier supplier for the {@link de.leanovate.pragmatic.ioc.Scope}
* @param supplier supplier for the instance
*/
public ScopedInjector(final Class injectedClass, final Optional name, final Supplier scopeSupplier,
final Supplier extends T> supplier) {
this.injectedClass = injectedClass;
this.name = name;
this.scopeSupplier = scopeSupplier;
this.supplier = supplier;
}
@Override
public T inject() {
return scopeSupplier.get().getInstance(injectedClass, name, supplier);
}
@Override
public Scope getScope() {
return scopeSupplier.get();
}
}