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