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