package org.xbib.logging.log4j;

import java.util.Map;

import org.apache.logging.log4j.spi.ThreadContextMap;
import org.xbib.logging.MDC;

/**
 * A {@link ThreadContextMap} implementation which delegates to {@link MDC}.
 */
public class ThreadContextMDCMap implements ThreadContextMap {

    public ThreadContextMDCMap() {
    }

    @Override
    public void clear() {
        MDC.clear();
    }

    @Override
    public boolean containsKey(final String key) {
        return MDC.get(key) != null;
    }

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

    @Override
    public Map<String, String> getCopy() {
        return MDC.copy();
    }

    @Override
    public Map<String, String> getImmutableMapOrNull() {
        final Map<String, String> copy = MDC.copy();
        return copy.isEmpty() ? null : Map.copyOf(copy);
    }

    @Override
    public boolean isEmpty() {
        return MDC.isEmpty();
    }

    @Override
    public void put(final String key, final String value) {
        if (value == null) {
            MDC.remove(key);
        } else {
            MDC.put(key, value);
        }
    }

    @Override
    public void remove(final String key) {
        MDC.remove(key);
    }
}
