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

org.deephacks.tools4j.config.model.ThreadLocalManager Maven / Gradle / Ivy

/**
 * Licensed 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.deephacks.tools4j.config.model;

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

public class ThreadLocalManager {
    private static final ThreadLocal, Stack>> threadLocal = new ThreadLocal, Stack>>();

    public static  void push(Class cls, T value) {
        Map, Stack> map = threadLocal.get();
        if (map == null) {
            map = new HashMap, Stack>();
        }
        Stack stack = map.get(cls);
        if (stack == null) {
            stack = new Stack();
        }
        stack.push(value);
        map.put(cls, stack);
        threadLocal.set(map);
    }

    public static  T peek(Class cls) {
        Map, Stack> map = threadLocal.get();
        if (map == null) {
            return null;
        }
        Stack stack = map.get(cls);
        if (stack == null || stack.isEmpty()) {
            return null;
        }
        return cls.cast(stack.peek());
    }

    public static  T pop(Class cls) {
        Map, Stack> map = threadLocal.get();
        if (map == null) {
            return null;
        }
        Stack stack = map.get(cls);
        if (stack == null || stack.isEmpty()) {
            return null;
        }
        return cls.cast(stack.pop());
    }

    public static  void clear(Class cls) {
        Map, Stack> map = threadLocal.get();
        if (map == null) {
            return;
        }
        map.remove(cls);
    }

    public static void clear() {
        threadLocal.set(null);
    }
}