de.leanovate.pragmatic.ioc.AbstractScope Maven / Gradle / Ivy
The newest version!
package de.leanovate.pragmatic.ioc;
import java.util.LinkedList;
import java.util.Objects;
import java.util.function.Supplier;
public abstract class AbstractScope implements Scope {
protected ThreadLocal> createStack = ThreadLocal.withInitial(LinkedList::new);
protected T createInstance(final String key, final Supplier supplier) {
final LinkedList stack = createStack.get();
if (stack.contains(key)) {
throw new CyclicDependencyException(stack);
}
try {
stack.add(key);
return Objects.requireNonNull(supplier.get(), () -> "Instantiation of '" + key + "' failed");
} finally {
stack.removeLast();
if (stack.isEmpty()) {
createStack.remove();
}
}
}
}