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