View Javadoc

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