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

org.zodiac.sdk.async.promises.PromiseContext Maven / Gradle / Ivy

package org.zodiac.sdk.async.promises;

import java.util.IdentityHashMap;
import java.util.Map;

/**
 * Allows individual Logics to pass data to other Logics later in the sequence,
 * in the case of compound promises.
 *
 */
public class PromiseContext {

    private final Map, Object> map = new IdentityHashMap<>();

    public  PromiseContext put(Key key, T obj) {
        map.put(key, key.cast(obj));
        return this;
    }

    public  T get(Key key) {
        Object o = map.get(key);
        return key.cast(o);
    }

    /**
     * Create a key that can be used to store and retrieve context contents. The
     * equals contract is identity, so multiple keys with the same type may be
     * used - the reference to the key you want to use must be known to all the
     * logic instances that want to use it.
     *
     * @param  The type of object.
     * @param key The key type
     * @return A key
     */
    public static  Key newKey(Class key) {
        return new Key<>(key);
    }

    public static final class Key {

        private final Class type;

        Key(Class type) {
            this.type = type;
        }

        public T cast(Object o) {
            return type.cast(o);
        }

        public Class type() {
            return type;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy