PageRenderTime 11ms CodeModel.GetById 1ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/UniversalImageLoader/src/com/nostra13/universalimageloader/cache/memory/BaseMemoryCache.java

https://github.com/rashedulkabir/Android-Universal-Image-Loader
Java | 53 lines | 36 code | 9 blank | 8 comment | 2 complexity | 1ac606f60882580b82615b7a85ae3ba6 MD5 | raw file
 1package com.nostra13.universalimageloader.cache.memory;
 2
 3import java.lang.ref.Reference;
 4import java.util.Collection;
 5import java.util.Collections;
 6import java.util.HashMap;
 7import java.util.Map;
 8
 9/**
10 * Base memory cache. Implements common functionality for memory cache. Provides object references (
11 * {@linkplain Reference not strong}) storing.
12 * 
13 * @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
14 */
15public abstract class BaseMemoryCache<K, V> implements MemoryCacheAware<K, V> {
16
17	/** Stores not strong references to objects */
18	private final Map<K, Reference<V>> softMap = Collections.synchronizedMap(new HashMap<K, Reference<V>>());
19
20	@Override
21	public V get(K key) {
22		V result = null;
23		Reference<V> reference = softMap.get(key);
24		if (reference != null) {
25			result = reference.get();
26		}
27		return result;
28	}
29
30	@Override
31	public boolean put(K key, V value) {
32		softMap.put(key, createReference(value));
33		return true;
34	}
35
36	@Override
37	public void remove(K key) {
38		softMap.remove(key);
39	}
40
41	@Override
42	public Collection<K> keys() {
43		return softMap.keySet();
44	}
45
46	@Override
47	public void clear() {
48		softMap.clear();
49	}
50
51	/** Creates {@linkplain Reference not strong} reference of value */
52	protected abstract Reference<V> createReference(V value);
53}