001    /**
002     * Copyright (c) 2000-present 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.cache.test;
016    
017    import com.liferay.portal.kernel.cache.AbstractPortalCache;
018    import com.liferay.portal.kernel.cache.PortalCacheManager;
019    
020    import java.io.Serializable;
021    
022    import java.util.ArrayList;
023    import java.util.List;
024    import java.util.concurrent.ConcurrentHashMap;
025    import java.util.concurrent.ConcurrentMap;
026    
027    /**
028     * @author Tina Tian
029     */
030    public class TestPortalCache <K extends Serializable, V>
031            extends AbstractPortalCache<K, V> {
032    
033            public TestPortalCache(
034                    PortalCacheManager<K, V> portalCacheManager, String portalCacheName) {
035    
036                    super(portalCacheManager);
037    
038                    _portalCacheName = portalCacheName;
039    
040                    _concurrentMap = new ConcurrentHashMap<>();
041            }
042    
043            public TestPortalCache(String portalCacheName) {
044                    super(null);
045    
046                    _portalCacheName = portalCacheName;
047    
048                    _concurrentMap = new ConcurrentHashMap<>();
049            }
050    
051            @Override
052            public List<K> getKeys() {
053                    List<K> keys = new ArrayList<>();
054    
055                    for (K key : _concurrentMap.keySet()) {
056                            keys.add(key);
057                    }
058    
059                    return keys;
060            }
061    
062            /**
063             * @deprecated As of 7.0.0, replaced by {@link #getPortalCacheName()}
064             */
065            @Deprecated
066            @Override
067            public String getName() {
068                    return getPortalCacheName();
069            }
070    
071            @Override
072            public String getPortalCacheName() {
073                    return _portalCacheName;
074            }
075    
076            @Override
077            public void removeAll() {
078                    _concurrentMap.clear();
079    
080                    aggregatedPortalCacheListener.notifyRemoveAll(this);
081            }
082    
083            @Override
084            protected V doGet(K key) {
085                    return _concurrentMap.get(key);
086            }
087    
088            @Override
089            protected void doPut(K key, V value, int timeToLive) {
090                    V oldValue = _concurrentMap.put(key, value);
091    
092                    if (oldValue != null) {
093                            aggregatedPortalCacheListener.notifyEntryUpdated(
094                                    this, key, value, timeToLive);
095                    }
096                    else {
097                            aggregatedPortalCacheListener.notifyEntryPut(
098                                    this, key, value, timeToLive);
099                    }
100            }
101    
102            @Override
103            protected V doPutIfAbsent(K key, V value, int timeToLive) {
104                    V oldValue = _concurrentMap.putIfAbsent(key, value);
105    
106                    if (oldValue == null) {
107                            aggregatedPortalCacheListener.notifyEntryPut(
108                                    this, key, value, timeToLive);
109                    }
110    
111                    return oldValue;
112            }
113    
114            @Override
115            protected void doRemove(K key) {
116                    V value = _concurrentMap.remove(key);
117    
118                    if (value != null) {
119                            aggregatedPortalCacheListener.notifyEntryRemoved(
120                                    this, key, value, DEFAULT_TIME_TO_LIVE);
121                    }
122            }
123    
124            @Override
125            protected boolean doRemove(K key, V value) {
126                    boolean removed = _concurrentMap.remove(key, value);
127    
128                    if (removed) {
129                            aggregatedPortalCacheListener.notifyEntryRemoved(
130                                    this, key, value, DEFAULT_TIME_TO_LIVE);
131                    }
132    
133                    return removed;
134            }
135    
136            @Override
137            protected V doReplace(K key, V value, int timeToLive) {
138                    V oldValue = _concurrentMap.replace(key, value);
139    
140                    if (oldValue != null) {
141                            aggregatedPortalCacheListener.notifyEntryUpdated(
142                                    this, key, value, timeToLive);
143                    }
144    
145                    return oldValue;
146            }
147    
148            @Override
149            protected boolean doReplace(K key, V oldValue, V newValue, int timeToLive) {
150                    boolean replaced = _concurrentMap.replace(key, oldValue, newValue);
151    
152                    if (replaced) {
153                            aggregatedPortalCacheListener.notifyEntryUpdated(
154                                    this, key, newValue, timeToLive);
155                    }
156    
157                    return replaced;
158            }
159    
160            private final ConcurrentMap<K, V> _concurrentMap;
161            private final String _portalCacheName;
162    
163    }