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            /**
039             * @deprecated As of 7.0.0, replaced by {@link #getPortalCacheName()}
040             */
041            @Deprecated
042            @Override
043            public String getName() {
044                    return getPortalCacheName();
045            }
046    
047            @Override
048            public PortalCacheManager<K, V> getPortalCacheManager() {
049                    return _portalCacheManager;
050            }
051    
052            @Override
053            public void put(K key, V value) {
054                    put(key, value, DEFAULT_TIME_TO_LIVE);
055            }
056    
057            @Override
058            public void put(K key, V value, int timeToLive) {
059                    if (key == null) {
060                            throw new NullPointerException("Key is null");
061                    }
062    
063                    if (value == null) {
064                            throw new NullPointerException("Value is null");
065                    }
066    
067                    if (timeToLive < 0) {
068                            throw new IllegalArgumentException("Time to live is negative");
069                    }
070    
071                    doPut(key, value, timeToLive);
072            }
073    
074            @Override
075            public V putIfAbsent(K key, V value) {
076                    return putIfAbsent(key, value, DEFAULT_TIME_TO_LIVE);
077            }
078    
079            @Override
080            public V putIfAbsent(K key, V value, int timeToLive) {
081                    if (key == null) {
082                            throw new NullPointerException("Key is null");
083                    }
084    
085                    if (value == null) {
086                            throw new NullPointerException("Value is null");
087                    }
088    
089                    if (timeToLive < 0) {
090                            throw new IllegalArgumentException("Time to live is negative");
091                    }
092    
093                    return doPutIfAbsent(key, value, timeToLive);
094            }
095    
096            @Override
097            public void registerPortalCacheListener(
098                    PortalCacheListener<K, V> portalCacheListener) {
099    
100                    aggregatedPortalCacheListener.addPortalCacheListener(
101                            portalCacheListener);
102            }
103    
104            @Override
105            public void registerPortalCacheListener(
106                    PortalCacheListener<K, V> portalCacheListener,
107                    PortalCacheListenerScope portalCacheListenerScope) {
108    
109                    aggregatedPortalCacheListener.addPortalCacheListener(
110                            portalCacheListener, portalCacheListenerScope);
111            }
112    
113            @Override
114            public void remove(K key) {
115                    if (key == null) {
116                            throw new NullPointerException("Key is null");
117                    }
118    
119                    doRemove(key);
120            }
121    
122            @Override
123            public boolean remove(K key, V value) {
124                    if (key == null) {
125                            throw new NullPointerException("Key is null");
126                    }
127    
128                    if (value == null) {
129                            throw new NullPointerException("Value is null");
130                    }
131    
132                    return doRemove(key, value);
133            }
134    
135            @Override
136            public V replace(K key, V value) {
137                    return replace(key, value, DEFAULT_TIME_TO_LIVE);
138            }
139    
140            @Override
141            public V replace(K key, V value, int timeToLive) {
142                    if (key == null) {
143                            throw new NullPointerException("Key is null");
144                    }
145    
146                    if (value == null) {
147                            throw new NullPointerException("Value is null");
148                    }
149    
150                    if (timeToLive < 0) {
151                            throw new IllegalArgumentException("Time to live is negative");
152                    }
153    
154                    return doReplace(key, value, timeToLive);
155            }
156    
157            @Override
158            public boolean replace(K key, V oldValue, V newValue) {
159                    return replace(key, oldValue, newValue, DEFAULT_TIME_TO_LIVE);
160            }
161    
162            @Override
163            public boolean replace(K key, V oldValue, V newValue, int timeToLive) {
164                    if (key == null) {
165                            throw new NullPointerException("Key is null");
166                    }
167    
168                    if (oldValue == null) {
169                            throw new NullPointerException("Old value is null");
170                    }
171    
172                    if (newValue == null) {
173                            throw new NullPointerException("New value is null");
174                    }
175    
176                    if (timeToLive < 0) {
177                            throw new IllegalArgumentException("Time to live is negative");
178                    }
179    
180                    return doReplace(key, oldValue, newValue, timeToLive);
181            }
182    
183            @Override
184            public void unregisterPortalCacheListener(
185                    PortalCacheListener<K, V> portalCacheListener) {
186    
187                    aggregatedPortalCacheListener.removePortalCacheListener(
188                            portalCacheListener);
189            }
190    
191            @Override
192            public void unregisterPortalCacheListeners() {
193                    aggregatedPortalCacheListener.clearAll();
194            }
195    
196            protected abstract V doGet(K key);
197    
198            protected abstract void doPut(K key, V value, int timeToLive);
199    
200            protected abstract V doPutIfAbsent(K key, V value, int timeToLive);
201    
202            protected abstract void doRemove(K key);
203    
204            protected abstract boolean doRemove(K key, V value);
205    
206            protected abstract V doReplace(K key, V value, int timeToLive);
207    
208            protected abstract boolean doReplace(
209                    K key, V oldValue, V newValue, int timeToLive);
210    
211            protected final AggregatedPortalCacheListener<K, V>
212                    aggregatedPortalCacheListener = new AggregatedPortalCacheListener<>();
213    
214            private final PortalCacheManager<K, V> _portalCacheManager;
215    
216    }