1 package com.atlassian.cache.impl;
2
3 import com.atlassian.cache.CacheEntryListener;
4 import com.atlassian.util.concurrent.LazyReference;
5
6 public class ValueCacheEntryListenerSupport<K, V> implements CacheEntryListenerSupport<K, V>
7 {
8 private final LazyReference<Void> initReference = new LazyReference<Void>()
9 {
10 @Override
11 protected Void create() throws Exception
12 {
13 init(ValueCacheEntryListenerSupport.this);
14 return null;
15 }
16 };
17
18 private final CacheEntryListenerSupport<K, V> valueListenerSupport =
19 new LazyCacheEntryListenerSupport<K, V>()
20 {
21 @Override
22 protected void init()
23 {
24 initReference.get();
25 initValue(this);
26 }
27 };
28
29 private final CacheEntryListenerSupport<K, V> valuelessListenerSupport =
30 new LazyCacheEntryListenerSupport<K, V>()
31 {
32 @Override
33 protected void init()
34 {
35 initReference.get();
36 initValueless(this);
37 }
38 };
39
40 protected void init(CacheEntryListenerSupport<K, V> actualListenerSupport)
41 {
42 }
43
44 protected void initValue(CacheEntryListenerSupport<K, V> actualListenerSupport)
45 {
46 }
47
48 protected void initValueless(CacheEntryListenerSupport<K, V> actualListenerSupport)
49 {
50 }
51
52 @Override
53 public void add(final CacheEntryListener<K, V> listener, final boolean includeValues)
54 {
55 if (includeValues)
56 {
57 valueListenerSupport.add(listener, true);
58 } else {
59 valuelessListenerSupport.add(listener, false);
60 }
61 }
62
63 @Override
64 public void remove(final CacheEntryListener<K, V> listener)
65 {
66 valueListenerSupport.remove(listener);
67 valuelessListenerSupport.remove(listener);
68 }
69
70 @Override
71 public void notifyAdd(final K key, final V value)
72 {
73 valueListenerSupport.notifyAdd(key, value);
74 valuelessListenerSupport.notifyAdd(key, null);
75 }
76
77 @Override
78 public void notifyEvict(final K key, final V oldValue)
79 {
80 valueListenerSupport.notifyEvict(key, oldValue);
81 valuelessListenerSupport.notifyEvict(key, null);
82 }
83
84 @Override
85 public void notifyRemove(final K key, final V oldValue)
86 {
87 valueListenerSupport.notifyRemove(key, oldValue);
88 valuelessListenerSupport.notifyRemove(key, null);
89 }
90
91 @Override
92 public void notifyUpdate(final K key, final V value, final V oldValue)
93 {
94 valueListenerSupport.notifyUpdate(key, value, oldValue);
95 valuelessListenerSupport.notifyUpdate(key, null, null);
96 }
97 }