All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cucumber.runtime.java.spring.GlueCodeContext Maven / Gradle / Ivy

The newest version!
package cucumber.runtime.java.spring;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;

class GlueCodeContext {
    public static final GlueCodeContext INSTANCE = new GlueCodeContext();
    private static final AtomicInteger COUNTER = new AtomicInteger();
    private final ThreadLocal> objects = ThreadLocal.withInitial(new Supplier>() {
        public Map get() {
            return new HashMap();
        }
    });
    private final ThreadLocal> callbacks = ThreadLocal.withInitial(new Supplier>() {
        public Map get() {
            return new HashMap();
        }
    });

    private GlueCodeContext() {
    }

    public void start() {
        cleanUp();
        COUNTER.incrementAndGet();
    }

    public String getId() {
        return "cucumber_glue_" + COUNTER.get();
    }

    public void stop() {
        for (Runnable callback : callbacks.get().values()) {
            callback.run();
        }
        cleanUp();
    }

    public Object get(String name) {
        return objects.get().get(name);
    }

    public void put(String name, Object object) {
        objects.get().put(name, object);
    }

    public Object remove(String name) {
        callbacks.get().remove(name);
        return objects.get().remove(name);
    }

    private void cleanUp() {
        objects.get().clear();
        callbacks.get().clear();
    }

    public void registerDestructionCallback(String name, Runnable callback) {
        callbacks.get().put(name, callback);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy