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

uk.org.lidalia.slf4jtest.TestMDCAdapter Maven / Gradle / Ivy

package uk.org.lidalia.slf4jtest;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.spi.MDCAdapter;

import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;

import uk.org.lidalia.lang.ThreadLocal;

import static com.google.common.base.Optional.fromNullable;

public class TestMDCAdapter implements MDCAdapter {

    private final ThreadLocal> value = new ThreadLocal>(
            Suppliers.makeEmptyMutableMap());

    public void put(final String key, final String val) {
        value.get().put(key, fromNullable(val).or("null"));
    }

    public String get(final String key) {
        return value.get().get(key);
    }

    public void remove(final String key) {
        value.get().remove(key);
    }

    public void clear() {
        value.get().clear();
    }

    public ImmutableMap getCopyOfContextMap() {
        return ImmutableMap.copyOf(value.get());
    }

    @SuppressWarnings("unchecked")
    public void setContextMap(final Map contextMap) {
        value.set(new HashMap(contextMap));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy