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

jalse.actions.BaseActionContext Maven / Gradle / Ivy

There is a newer version: 1.1.0
Show newest version
package jalse.actions;

import static jalse.actions.Actions.requireNotStopped;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;

abstract class BaseActionContext implements MutableActionContext {

    private final ActionEngine engine;
    private final Action action;
    private final MutableActionBindings bindings;
    private final AtomicReference actor;
    private final AtomicLong period;
    private final AtomicLong initialDelay;

    BaseActionContext(final ActionEngine engine, final Action action, final ActionBindings sourceBindings) {
	this.engine = requireNotStopped(engine);
	this.action = Objects.requireNonNull(action);
	bindings = new DefaultActionBindings(sourceBindings);
	actor = new AtomicReference<>();
	period = new AtomicLong();
	initialDelay = new AtomicLong();
    }

    @Override
    public  S get(final String key) {
	return bindings.get(key);
    }

    @Override
    public Action getAction() {
	return action;
    }

    @Override
    public T getActor() {
	return actor.get();
    }

    @Override
    public ActionEngine getEngine() {
	return engine;
    }

    @Override
    public long getInitialDelay(final TimeUnit unit) {
	return unit.convert(initialDelay.get(), TimeUnit.NANOSECONDS);
    }

    @Override
    public long getPeriod(final TimeUnit unit) {
	return unit.convert(period.get(), TimeUnit.NANOSECONDS);
    }

    @Override
    public  S put(final String key, final S value) {
	return bindings.put(key, value);
    }

    @Override
    public void putAll(final Map map) {
	bindings.putAll(map);
    }

    @Override
    public  S remove(final String key) {
	return bindings.remove(key);
    }

    @Override
    public void removeAll() {
	bindings.removeAll();
    }

    @Override
    public void setActor(final T actor) {
	this.actor.set(actor);
    }

    @Override
    public void setInitialDelay(final long initialDelay, final TimeUnit unit) {
	this.initialDelay.set(unit.toNanos(initialDelay));
    }

    @Override
    public void setPeriod(final long period, final TimeUnit unit) {
	this.period.set(unit.toNanos(period));
    }

    @Override
    public Map toMap() {
	return bindings.toMap();
    }
}