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