View Javadoc

1   package com.atlassian.cache.impl;
2   
3   import com.atlassian.cache.CachedReferenceEvent;
4   import com.atlassian.cache.CachedReferenceListener;
5   import com.atlassian.fugue.Effect;
6   import org.slf4j.Logger;
7   import org.slf4j.LoggerFactory;
8   
9   public class CachedReferenceNotificationSupport
10  {
11      private static final Logger log = LoggerFactory.getLogger(CachedReferenceNotificationSupport.class);
12  
13      private static final CachedReferenceNotificationSupport INSTANCE = new CachedReferenceNotificationSupport();
14  
15      public static CachedReferenceNotificationSupport getInstance()
16      {
17          return INSTANCE;
18      }
19  
20      public <V> void notifyEvict(Iterable<CachedReferenceListener<V>> listeners,
21                                  final CachedReferenceEvent<V> event)
22      {
23          notify(listeners, new Effect<CachedReferenceListener<V>>()
24          {
25              @Override
26              public void apply(CachedReferenceListener<V> listener)
27              {
28                  listener.onEvict(event);
29              }
30          });
31      }
32  
33      public <V> void notifySet(Iterable<CachedReferenceListener<V>> listeners,
34                                final CachedReferenceEvent<V> event)
35      {
36          notify(listeners, new Effect<CachedReferenceListener<V>>()
37          {
38              @Override
39              public void apply(CachedReferenceListener<V> listener)
40              {
41                  listener.onSet(event);
42              }
43          });
44      }
45  
46      public <V> void notifyReset(Iterable<CachedReferenceListener<V>> listeners,
47                                  final CachedReferenceEvent<V> event)
48      {
49          notify(listeners, new Effect<CachedReferenceListener<V>>()
50          {
51              @Override
52              public void apply(CachedReferenceListener<V> listener)
53              {
54                  listener.onReset(event);
55              }
56          });
57      }
58  
59      private <V> void notify(Iterable<CachedReferenceListener<V>> listeners,
60                              Effect<CachedReferenceListener<V>> effect)
61      {
62          for(CachedReferenceListener<V> listener : listeners)
63          {
64              try
65              {
66                  effect.apply(listener);
67              }
68              catch (RuntimeException exc)
69              {
70                  log.error("Exception while notifying listeners", exc);
71              }
72          }
73      }
74  }