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 net.sf.ehcache.Ehcache;
8 import net.sf.ehcache.Status;
9 import net.sf.ehcache.loader.CacheLoader;
10
11
12
13
14
15
16 class DelegatingCacheLoader<K, V> implements CacheLoader, Cloneable
17 {
18 private com.atlassian.cache.CacheLoader<K, V> loader;
19
20 public DelegatingCacheLoader(final com.atlassian.cache.CacheLoader<K, V> loader)
21 {
22 this.loader = loader;
23 }
24
25 @SuppressWarnings("unchecked")
26 @Override
27 public Object load(final Object key) throws net.sf.ehcache.CacheException
28 {
29 return loader.load((K) key);
30 }
31
32 @SuppressWarnings("unchecked")
33 @Override
34 public Map loadAll(final Collection keys)
35 {
36 Map<K, V> map = new HashMap<K, V>();
37 for (Object key : keys)
38 {
39 final V value = loader.load((K) key);
40 if (value != null)
41 {
42 map.put((K) 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 loader.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 loader = null;
82 }
83
84 @Override
85 public Status getStatus()
86 {
87 return loader == null ? Status.STATUS_ALIVE : Status.STATUS_UNINITIALISED;
88 }
89 }