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.kernel.cache;
016    
017    import java.io.Serializable;
018    
019    /**
020     * @author Tina Tian
021     */
022    public abstract class AbstractPortalCache<K extends Serializable, V>
023            implements LowLevelCache<K, V> {
024    
025            public AbstractPortalCache(PortalCacheManager<K, V> portalCacheManager) {
026                    _portalCacheManager = portalCacheManager;
027            }
028    
029            @Override
030            public V get(K key) {
031                    if (key == null) {
032                            throw new NullPointerException("Key is null");
033                    }
034    
035                    return doGet(key);
036            }
037    
038            @Override
039            public PortalCacheManager<K, V> getPortalCacheManager() {
040                    return _portalCacheManager;
041            }
042    
043            @Override
044            public void put(K key, V value) {
045                    put(key, value, DEFAULT_TIME_TO_LIVE);
046            }
047    
048            @Override
049            public void put(K key, V value, int timeToLive) {
050                    if (key == null) {
051                            throw new NullPointerException("Key is null");
052                    }
053    
054                    if (value == null) {
055                            throw new NullPointerException("Value is null");
056                    }
057    
058                    if (timeToLive < 0) {
059                            throw new IllegalArgumentException("Time to live is negative");
060                    }
061    
062                    doPut(key, value, timeToLive);
063            }
064    
065            @Override
066            public V putIfAbsent(K key, V value) {
067                    return putIfAbsent(key, value, DEFAULT_TIME_TO_LIVE);
068            }
069    
070            @Override
071            public V putIfAbsent(K key, V value, int timeToLive) {
072                    if (key == null) {
073                            throw new NullPointerException("Key is null");
074                    }
075    
076                    if (value == null) {
077                            throw new NullPointerException("Value is null");
078                    }
079    
080                    if (timeToLive < 0) {
081                            throw new IllegalArgumentException("Time to live is negative");
082                    }
083    
084                    return doPutIfAbsent(key, value, timeToLive);
085            }
086    
087            @Override
088            public void registerCacheListener(CacheListener<K, V> cacheListener) {
089                    aggregatedCacheListener.addCacheListener(cacheListener);
090            }
091    
092            @Override
093            public void registerCacheListener(
094                    CacheListener<K, V> cacheListener,
095                    CacheListenerScope cacheListenerScope) {
096    
097                    aggregatedCacheListener.addCacheListener(
098                            cacheListener, cacheListenerScope);
099            }
100    
101            @Override
102            public void remove(K key) {
103                    if (key == null) {
104                            throw new NullPointerException("Key is null");
105                    }
106    
107                    doRemove(key);
108            }
109    
110            @Override
111            public boolean remove(K key, V value) {
112                    if (key == null) {
113                            throw new NullPointerException("Key is null");
114                    }
115    
116                    if (value == null) {
117                            throw new NullPointerException("Value is null");
118                    }
119    
120                    return doRemove(key, value);
121            }
122    
123            @Override
124            public V replace(K key, V value) {
125                    return replace(key, value, DEFAULT_TIME_TO_LIVE);
126            }
127    
128            @Override
129            public V replace(K key, V value, int timeToLive) {
130                    if (key == null) {
131                            throw new NullPointerException("Key is null");
132                    }
133    
134                    if (value == null) {
135                            throw new NullPointerException("Value is null");
136                    }
137    
138                    if (timeToLive < 0) {
139                            throw new IllegalArgumentException("Time to live is negative");
140                    }
141    
142                    return doReplace(key, value, timeToLive);
143            }
144    
145            @Override
146            public boolean replace(K key, V oldValue, V newValue) {
147                    return replace(key, oldValue, newValue, DEFAULT_TIME_TO_LIVE);
148            }
149    
150            @Override
151            public boolean replace(K key, V oldValue, V newValue, int timeToLive) {
152                    if (key == null) {
153                            throw new NullPointerException("Key is null");
154                    }
155    
156                    if (oldValue == null) {
157                            throw new NullPointerException("Old value is null");
158                    }
159    
160                    if (newValue == null) {
161                            throw new NullPointerException("New value is null");
162                    }
163    
164                    if (timeToLive < 0) {
165                            throw new IllegalArgumentException("Time to live is negative");
166                    }
167    
168                    return doReplace(key, oldValue, newValue, timeToLive);
169            }
170    
171            @Override
172            public void unregisterCacheListener(CacheListener<K, V> cacheListener) {
173                    aggregatedCacheListener.removeCacheListener(cacheListener);
174            }
175    
176            @Override
177            public void unregisterCacheListeners() {
178                    aggregatedCacheListener.clearAll();
179            }
180    
181            protected abstract V doGet(K key);
182    
183            protected abstract void doPut(K key, V value, int timeToLive);
184    
185            protected abstract V doPutIfAbsent(K key, V value, int timeToLive);
186    
187            protected abstract void doRemove(K key);
188    
189            protected abstract boolean doRemove(K key, V value);
190    
191            protected abstract V doReplace(K key, V value, int timeToLive);
192    
193            protected abstract boolean doReplace(
194                    K key, V oldValue, V newValue, int timeToLive);
195    
196            protected final AggregatedCacheListener<K, V> aggregatedCacheListener =
197                    new AggregatedCacheListener<>();
198    
199            private final PortalCacheManager<K, V> _portalCacheManager;
200    
201    }