/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

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