View Javadoc

1   package com.atlassian.cache.ehcache;
2   
3   import com.atlassian.cache.Supplier;
4   import com.atlassian.cache.impl.ReferenceKey;
5   import net.sf.ehcache.Ehcache;
6   import net.sf.ehcache.Status;
7   import net.sf.ehcache.loader.CacheLoader;
8   
9   import java.util.Collection;
10  import java.util.HashMap;
11  import java.util.Map;
12  
13  /**
14  *  A delegating loader for EhCache.
15  *
16  * @since v2.0.8
17  */
18  class ReferenceCacheLoader<V> implements CacheLoader, Cloneable
19  {
20      private Supplier<V> supplier;
21  
22      public ReferenceCacheLoader(final Supplier<V> supplier)
23      {
24          this.supplier = supplier;
25      }
26  
27      @Override
28      public Object load(final Object key)
29      {
30          return supplier.get();
31      }
32  
33      @Override
34      public Map loadAll(final Collection keys)
35      {
36          Map<ReferenceKey, V> map = new HashMap<ReferenceKey, V>();
37          for (Object key : keys)
38          {
39              final V value = supplier.get();
40              if (value != null)
41              {
42                  map.put(ReferenceKey.KEY, value);
43              }
44          }
45          return map;
46      }
47  
48      @Override
49      public Object load(final Object key, final Object argument)
50      {
51          return load(key);
52      }
53  
54      @Override
55      public Map loadAll(final Collection keys, final Object argument)
56      {
57          return loadAll(keys);
58      }
59  
60      @Override
61      public String getName()
62      {
63          return supplier.getClass().getName();
64      }
65  
66      @Override
67      public CacheLoader clone(final Ehcache cache) throws CloneNotSupportedException
68      {
69          return (CacheLoader) super.clone();
70      }
71  
72      @Override
73      public void init()
74      {
75  
76      }
77  
78      @Override
79      public void dispose() throws net.sf.ehcache.CacheException
80      {
81          supplier = null;
82      }
83  
84      @Override
85      public Status getStatus()
86      {
87          return supplier == null ? Status.STATUS_ALIVE : Status.STATUS_UNINITIALISED;
88      }
89  
90  }