View Javadoc

1   package com.atlassian.cache.impl;
2   
3   import com.atlassian.cache.CacheEntryListener;
4   
5   import java.util.Set;
6   import java.util.concurrent.CopyOnWriteArraySet;
7   
8   import static com.google.common.base.Preconditions.checkNotNull;
9   
10  /**
11   * A utility class which:
12   * <ul>
13   *     <li>Manages a set of {@code CacheEntryListener}s</li>
14   *     <li>Provides a way to notify these listeners about different events</li>
15   * </ul>
16   */
17  public class DefaultCacheEntryListenerSupport<K, V> implements CacheEntryListenerSupport<K, V>
18  {
19      private final Set<CacheEntryListener<K, V>> listeners = new CopyOnWriteArraySet<CacheEntryListener<K, V>>();
20      private final CacheEntryNotificationSupport notificationSupport = CacheEntryNotificationSupport.getInstance();
21  
22      public void add(CacheEntryListener<K, V> listener, boolean includeValues)
23      {
24          listeners.add(checkNotNull(listener));
25      }
26  
27      public void remove(CacheEntryListener<K, V> listener)
28      {
29          listeners.remove(checkNotNull(listener));
30      }
31  
32      public void notifyAdd(K key, V value)
33      {
34          notificationSupport.notifyAdd(listeners, new DefaultCacheEntryEvent<K, V>(key, value, null));
35      }
36  
37      public void notifyEvict(K key, V oldValue)
38      {
39          notificationSupport.notifyEvict(listeners, new DefaultCacheEntryEvent<K, V>(key, null, oldValue));
40      }
41  
42      public void notifyRemove(K key, V oldValue)
43      {
44          notificationSupport.notifyRemove(listeners, new DefaultCacheEntryEvent<K, V>(key, null, oldValue));
45      }
46  
47      public void notifyUpdate(K key, V value, V oldValue)
48      {
49          notificationSupport.notifyUpdate(listeners, new DefaultCacheEntryEvent<K, V>(key, value, oldValue));
50      }
51  }