View Javadoc

1   package com.atlassian.cache.impl;
2   
3   import com.atlassian.cache.CacheEntryListener;
4   import com.atlassian.util.concurrent.LazyReference;
5   
6   public class LazyCacheEntryListenerSupport<K, V> implements CacheEntryListenerSupport<K, V>
7   {
8       private final LazyReference<CacheEntryListenerSupport<K, V>> lazyListenerSupport =
9               new LazyReference<CacheEntryListenerSupport<K, V>>()
10              {
11                  @Override
12                  protected CacheEntryListenerSupport<K, V> create() throws Exception {
13                      init();
14                      return createDelegate();
15                  }
16              };
17  
18      protected void init()
19      {
20      }
21  
22      protected CacheEntryListenerSupport<K, V> createDelegate()
23      {
24          return new DefaultCacheEntryListenerSupport<K, V>();
25      }
26  
27      private CacheEntryListenerSupport<K, V> loadDelegate()
28      {
29          return lazyListenerSupport.get();
30      }
31  
32      private CacheEntryListenerSupport<K, V> getDelegate()
33      {
34          if (lazyListenerSupport.isInitialized())
35          {
36              return lazyListenerSupport.get();
37          }
38          else
39          {
40              return CacheEntryListenerSupport.EMPTY;
41          }
42      }
43  
44      @Override
45      public void add(CacheEntryListener<K, V> listener, boolean includeValues)
46      {
47          loadDelegate().add(listener, includeValues);
48      }
49  
50      @Override
51      public void remove(CacheEntryListener<K, V> listener)
52      {
53          getDelegate().remove(listener);
54      }
55  
56      @Override
57      public void notifyAdd(K key, V value)
58      {
59          getDelegate().notifyAdd(key, value);
60      }
61  
62      @Override
63      public void notifyEvict(K key, V oldValue)
64      {
65          getDelegate().notifyEvict(key, oldValue);
66      }
67  
68      @Override
69      public void notifyRemove(K key, V oldValue)
70      {
71          getDelegate().notifyRemove(key, oldValue);
72      }
73  
74      @Override
75      public void notifyUpdate(K key, V value, V oldValue)
76      {
77          getDelegate().notifyUpdate(key, value, oldValue);
78      }
79  }