001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.cache;
016    
017    import com.liferay.portal.kernel.util.StringBundler;
018    
019    import java.io.Serializable;
020    
021    import java.util.HashMap;
022    import java.util.Map;
023    
024    /**
025     * @author Shuyang Zhou
026     */
027    public class ThreadLocalCache<T> {
028    
029            public ThreadLocalCache(Serializable name, Lifecycle lifecycle) {
030                    _name = name;
031                    _lifecycle = lifecycle;
032            }
033    
034            public T get(String key) {
035                    if (_cache == null) {
036                            return null;
037                    }
038                    else {
039                            return _cache.get(key);
040                    }
041            }
042    
043            public Lifecycle getLifecycle() {
044                    return _lifecycle;
045            }
046    
047            public Serializable getName() {
048                    return _name;
049            }
050    
051            public void put(String key, T obj) {
052                    if (_cache == null) {
053                            _cache = new HashMap<String, T>();
054                    }
055    
056                    _cache.put(key, obj);
057            }
058    
059            public void remove(String key) {
060                    if (_cache != null) {
061                            _cache.remove(key);
062                    }
063            }
064    
065            public void removeAll() {
066                    if (_cache != null) {
067                            _cache.clear();
068                    }
069            }
070    
071            @Override
072            public String toString() {
073                    StringBundler sb = new StringBundler(7);
074    
075                    sb.append("{cache=");
076                    sb.append(_cache.toString());
077                    sb.append(", lifecycle=");
078                    sb.append(_lifecycle);
079                    sb.append(", name=");
080                    sb.append(_name);
081                    sb.append("}");
082    
083                    return sb.toString();
084            }
085    
086            private Map<String, T> _cache;
087            private Lifecycle _lifecycle;
088            private Serializable _name;
089    
090    }