View Javadoc

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