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

top.fullj.chain.Context Maven / Gradle / Ivy

package top.fullj.chain;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author bruce.wu
 * @since 2022/5/30 14:50
 */
public class Context {

    private final Map items;

    public Context() {
        this(true);
    }

    public Context(boolean threadSafe) {
        this.items = threadSafe ? new ConcurrentHashMap<>() : new HashMap<>();
    }

    public boolean hasAttr(@Nonnull Object key) {
        return items.containsKey(key);
    }

    @SuppressWarnings("unchecked")
    @Nullable
    public  T getAttr(@Nonnull Object key) {
        return (T) items.get(key);
    }

    @SuppressWarnings("unchecked")
    @Nullable
    public  T getAttr(@Nonnull Object key, T defaultValue) {
        return items.containsKey(key) ? (T)items.get(key) : defaultValue;
    }

    public void setAttr(@Nonnull Object key, @Nonnull Object value) {
        items.put(key, value);
    }

    public void setAttr(@Nonnull Map attrs) {
        items.putAll(attrs);
    }

    @SuppressWarnings("unchecked")
    @Nullable
    public  T remove(@Nonnull Object key) {
        return (T) items.remove(key);
    }

    @Nonnull
    public Map snapshot() {
        return new HashMap<>(items);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy