cucumber.runtime.java.hk2.impl.ScenarioContext Maven / Gradle / Ivy
The newest version!
package cucumber.runtime.java.hk2.impl;
import cucumber.runtime.java.hk2.ScenarioScoped;
import org.glassfish.hk2.api.ActiveDescriptor;
import org.glassfish.hk2.api.Context;
import org.glassfish.hk2.api.ServiceHandle;
import javax.inject.Singleton;
import java.lang.annotation.Annotation;
import java.util.HashMap;
import java.util.HashSet;
@Singleton
public class ScenarioContext implements Context {
private boolean isActive = true;
private HashMap, Object> map = new HashMap<>();
@Override
public Class extends Annotation> getScope() {
return ScenarioScoped.class;
}
@Override
@SuppressWarnings("unchecked")
public U findOrCreate(ActiveDescriptor activeDescriptor, ServiceHandle> root) {
Object val = map.get(activeDescriptor);
if (val == null) {
val = activeDescriptor.create(root);
map.put(activeDescriptor, val);
}
return (U) val;
}
@Override
public boolean containsKey(ActiveDescriptor> descriptor) {
return map.containsKey(descriptor);
}
@Override
public void destroyOne(ActiveDescriptor> descriptor) {
remove(descriptor);
}
@SuppressWarnings("unchecked")
private void remove(ActiveDescriptor descriptor) {
final T removed = (T) map.remove(descriptor);
if (removed != null) {
descriptor.dispose(removed);
}
}
@Override
public boolean supportsNullCreation() {
return false;
}
@Override
public boolean isActive() {
return isActive;
}
@Override
public void shutdown() {
isActive = false;
clear();
}
public void clear() {
HashSet> copyOfKeys = new HashSet<>(map.keySet());
for (ActiveDescriptor> descriptor : copyOfKeys) {
remove(descriptor);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy