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