package com.nepxion.discovery.plugin.strategy.extension.service.context;

/**
 * <p>Title: Nepxion Discovery</p>
 * <p>Description: Nepxion Discovery</p>
 * <p>Copyright: Copyright (c) 2017-2050</p>
 * <p>Company: Nepxion</p>
 * @author Haojun Ren
 * @version 1.0
 */

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

public class ServiceStrategyContext {
    private static final ThreadLocal<ServiceStrategyContext> THREAD_LOCAL = new InheritableThreadLocal<ServiceStrategyContext>() {
        @Override
        protected ServiceStrategyContext initialValue() {
            return new ServiceStrategyContext();
        }
    };

    public static ServiceStrategyContext getCurrentContext() {
        return THREAD_LOCAL.get();
    }

    public static void clearCurrentContext() {
        THREAD_LOCAL.remove();
    }

    private final Map<String, Object> attributes = new LinkedHashMap<String, Object>();

    public ServiceStrategyContext add(String key, Object value) {
        attributes.put(key, value);

        return this;
    }

    public Object get(String key) {
        return attributes.get(key);
    }

    public ServiceStrategyContext remove(String key) {
        attributes.remove(key);

        return this;
    }

    public ServiceStrategyContext clear() {
        attributes.clear();

        return this;
    }

    public Map<String, Object> getAttributes() {
        return Collections.unmodifiableMap(attributes);
    }

    @Override
    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this);
    }

    @Override
    public boolean equals(Object object) {
        return EqualsBuilder.reflectionEquals(this, object);
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
    }
}