View Javadoc
1   package com.atlassian.cache.impl;
2   
3   import java.util.Set;
4   import java.util.concurrent.CopyOnWriteArraySet;
5   
6   import com.atlassian.cache.CachedReferenceListener;
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 CachedReferenceListener}s</li>
14   *     <li>Provides a way to notify these listeners about different events</li>
15   * </ul>
16   */
17  public class DefaultCachedReferenceListenerSupport<V> implements CachedReferenceListenerSupport<V>
18  {
19      private final Set<CachedReferenceListener<V>> listeners = new CopyOnWriteArraySet<CachedReferenceListener<V>>();
20      private final CachedReferenceNotificationSupport notificationSupport =
21              CachedReferenceNotificationSupport.getInstance();
22  
23      @Override
24      public void add(CachedReferenceListener<V> listener, boolean includeValues)
25      {
26          listeners.add(checkNotNull(listener));
27      }
28  
29      @Override
30      public void remove(CachedReferenceListener<V> listener)
31      {
32          listeners.remove(checkNotNull(listener));
33      }
34  
35      @Override
36      public void notifyEvict(V oldValue)
37      {
38          notificationSupport.notifyEvict(listeners, new DefaultCachedReferenceEvent<V>(oldValue));
39      }
40  
41      @Override
42      public void notifySet(V value)
43      {
44          notificationSupport.notifySet(listeners, new DefaultCachedReferenceEvent<V>(value));
45      }
46  
47      @Override
48      public void notifyReset(V oldValue)
49      {
50          notificationSupport.notifyReset(listeners, new DefaultCachedReferenceEvent<V>(oldValue));
51      }
52  }