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

org.apache.jetspeed.util.ThreadLocalHashMap Maven / Gradle / Ivy

There is a newer version: 2.3.1
Show newest version
/*
 * 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.jetspeed.util;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * ThreadLocalHashMap
 * @param 
 * @param 
 * @version $Id$
 */
public class ThreadLocalHashMap implements Map
{
    private static ThreadLocal> tlMap = new ThreadLocal>();
    
    public ThreadLocalHashMap()
    {
        this(true);
    }
    
    public ThreadLocalHashMap(boolean clear)
    {
        clear();
    }
    
    public int size()
    {
        Map map = getThreadLocalMap();
        return (map == null ? 0 : map.size());
    }
    
    public boolean isEmpty()
    {
        Map map = getThreadLocalMap();
        return (map == null ? true : map.isEmpty());
    }

    public boolean containsKey(Object key)
    {
        Map map = getThreadLocalMap();
        return (map == null ? false : map.containsKey(key));
    }
    
    public boolean containsValue(Object value)
    {
        Map map = getThreadLocalMap();
        return (map == null ? false : map.containsValue(value));
    }

    public V get(Object key)
    {
        Map map = getThreadLocalMap();
        return (map == null ? null : map.get(key));
    }

    public V put(K key, V value)
    {
        Map map = getThreadLocalMap();
        
        if (map == null)
        {
            map = new HashMap();
            setThreadLocalMap(map);
        }
        
        return map.put(key, value);
    }

    public V remove(Object key)
    {
        Map map = getThreadLocalMap();
        
        if (map != null)
        {
            return map.remove(key);
        }
        
        return null;
    }

    public void putAll(Map m)
    {
        Map map = getThreadLocalMap();
        
        if (map == null)
        {
            map = new HashMap();
            setThreadLocalMap(map);
        }
        
        map.putAll(m);
    }

    public void clear()
    {
        Map map = getThreadLocalMap();
        
        if (map != null)
        {
            map.clear();
        }
    }
    
    public Set keySet()
    {
        Map map = getThreadLocalMap();
        
        if (map != null)
        {
            return map.keySet();
        }
        
        return Collections.emptySet();
    }

    public Collection values()
    {
        Map map = getThreadLocalMap();
        
        if (map != null)
        {
            return map.values();
        }
        
        return Collections.emptyList();
    }

    public Set> entrySet()
    {
        Map map = getThreadLocalMap();
        
        if (map != null)
        {
            return map.entrySet();
        }
        
        return Collections.emptySet();
    }
    
    @SuppressWarnings("unchecked")
    private Map getThreadLocalMap()
    {
        return (Map) tlMap.get();
    }
    
    private void setThreadLocalMap(Map map)
    {
        tlMap.set(map);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy