001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.spi;
018
019import java.util.Map;
020
021import org.apache.logging.log4j.ThreadContext;
022
023/**
024 * Service provider interface to implement custom MDC behavior for {@link org.apache.logging.log4j.ThreadContext}.
025 * <p>
026 * Since 2.8, {@code ThreadContextMap} implementations that implement the {@link ReadOnlyThreadContextMap} interface
027 * are accessible to applications via the {@link ThreadContext#getThreadContextMap()} method.
028 * </p>
029 */
030public interface ThreadContextMap {
031
032    /**
033     * Clears the context.
034     */
035    void clear();
036
037    /**
038     * Determines if the key is in the context.
039     * @param key The key to locate.
040     * @return True if the key is in the context, false otherwise.
041     */
042    boolean containsKey(final String key);
043
044    /**
045     * Gets the context identified by the <code>key</code> parameter.
046     *
047     * <p>This method has no side effects.</p>
048     * @param key The key to locate.
049     * @return The value associated with the key or null.
050     */
051    String get(final String key);
052
053    /**
054     * Gets a non-{@code null} mutable copy of current thread's context Map.
055     * @return a mutable copy of the context.
056     */
057    Map<String, String> getCopy();
058
059    /**
060     * Returns an immutable view on the context Map or {@code null} if the context map is empty.
061     * @return an immutable context Map or {@code null}.
062     */
063    Map<String, String> getImmutableMapOrNull();
064
065    /**
066     * Returns true if the Map is empty.
067     * @return true if the Map is empty, false otherwise.
068     */
069    boolean isEmpty();
070
071    /**
072     * Puts a context value (the <code>o</code> parameter) as identified
073     * with the <code>key</code> parameter into the current thread's
074     * context map.
075     *
076     * <p>If the current thread does not have a context map it is
077     * created as a side effect.</p>
078     * @param key The key name.
079     * @param value The key value.
080     */
081    void put(final String key, final String value);
082
083    /**
084     * Removes the the context identified by the <code>key</code>
085     * parameter.
086     * @param key The key to remove.
087     */
088    void remove(final String key);
089}