/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.shenyu.admin.utils;

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

/**
 * The type Thread local util.
 */
public class ThreadLocalUtils {
    
    private static final ThreadLocal<Map<String, Object>> THREAD_CONTEXT = ThreadLocal.withInitial(HashMap::new);
    
    /**
     * save thread variable.
     *
     * @param key   put key
     * @param value put value
     */
    public static void put(final String key, final Object value) {
        THREAD_CONTEXT.get().put(key, value);
    }
    
    /**
     * remove thread variable.
     *
     * @param key remove key
     */
    public static void remove(final String key) {
        THREAD_CONTEXT.get().remove(key);
    }
    
    /**
     * get thread variables.
     *
     * @param key get key
     * @return the Object
     */
    public static Object get(final String key) {
        return THREAD_CONTEXT.get().get(key);
    }
    
    /**
     * remove all variables.
     */
    public static void clear() {
        THREAD_CONTEXT.remove();
    }
}
