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 java.io.Serializable;
018    
019    import java.util.Map;
020    
021    /**
022     * @author Shuyang Zhou
023     */
024    public class PortalCacheMapSynchronizeUtil {
025    
026            public static <K extends Serializable, V> void synchronize(
027                    PortalCache<K, V> portalCache, Map<? extends K, ? extends V> map,
028                    Synchronizer<K, V> synchronizer) {
029    
030                    portalCache.registerCacheListener(
031                            new SynchronizeCacheListener<K, V>(map, synchronizer));
032            }
033    
034            public static interface Synchronizer<K extends Serializable, V> {
035    
036                    public void onSynchronize(
037                            Map<? extends K, ? extends V> map, K key, V value);
038    
039            }
040    
041            private static class SynchronizeCacheListener<K extends Serializable, V>
042                    implements CacheListener<K, V> {
043    
044                    public SynchronizeCacheListener(
045                            Map<? extends K, ? extends V> map,
046                            Synchronizer<K, V> synchronizer) {
047    
048                            _map = map;
049                            _synchronizer = synchronizer;
050                    }
051    
052                    @Override
053                    public void notifyEntryEvicted(
054                            PortalCache<K, V> portalCache, K key, V value) {
055    
056                            _synchronizer.onSynchronize(_map, key, value);
057                    }
058    
059                    @Override
060                    public void notifyEntryExpired(
061                            PortalCache<K, V> portalCache, K key, V value) {
062    
063                            _synchronizer.onSynchronize(_map, key, value);
064                    }
065    
066                    @Override
067                    public void notifyEntryPut(
068                            PortalCache<K, V> portalCache, K key, V value) {
069    
070                            _synchronizer.onSynchronize(_map, key, value);
071                    }
072    
073                    @Override
074                    public void notifyEntryRemoved(
075                            PortalCache<K, V> portalCache, K key, V value) {
076    
077                            _synchronizer.onSynchronize(_map, key, value);
078                    }
079    
080                    @Override
081                    public void notifyEntryUpdated(
082                            PortalCache<K, V> portalCache, K key, V value) {
083    
084                            _synchronizer.onSynchronize(_map, key, value);
085                    }
086    
087                    @Override
088                    public void notifyRemoveAll(PortalCache<K, V> portalCache) {
089                            _map.clear();
090                    }
091    
092                    private Map<? extends K, ? extends V> _map;
093                    private Synchronizer<K, V> _synchronizer;
094    
095            }
096    
097    }