Android Open Source - SIC A Memorizer






From Project

Back to project page SIC.

License

The source code is released under:

MIT License

If you think the Android project SIC listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.sun.imageloader.memorizer.api;
// w w  w .j a v  a  2 s. co  m
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

import com.sun.imageloader.computable.impl.Computable;
import com.sun.imageloader.concurrent.ComputableCallable;
import com.sun.imageloader.core.ImageKey;
import com.sun.imageloader.core.ImageSettings;
import com.sun.imageloader.utils.L;
import com.sun.imageloader.utils.ViewUtils;

public abstract class AMemorizer<T extends ImageSettings,V> implements IMemorizer<T,V> {
  private static final String TAG = AMemorizer.class.getName();
  private ConcurrentHashMap<? super ImageKey, Future<V>> _bitmapFutureCache;
  private Computable<T, V> _computable;

  /**
   * AMemorizer is used to encapsulates a Computable so that Future tasks can be cached and computed when nessicary.
   * 
   * @param computable_ the Computable that will compute the value when given an input
   */
  protected AMemorizer(Computable<T, V> computable_,  ConcurrentHashMap<Integer, ImageKey> viewKeyMap_){
    _computable =  computable_;
    _bitmapFutureCache  = new ConcurrentHashMap<ImageKey, Future<V>>();
  }
  
  protected abstract Callable<V> getCallable(T computable_);
  
  
  @Override
  public V executeComputable(T computableKey_) throws InterruptedImageLoadException, ExecutionException {

    if(!ViewUtils.isViewStillValid(computableKey_)){
      L.v(TAG, "View is no longer valid with imageKey: " + computableKey_.getImageKey().toString());
      return null;
    }
    
    ImageKey key = computableKey_.getImageKey();
    Future<V> future = _bitmapFutureCache.get(key);
    V returnValue = null;

    if(future == null){
      Callable<V> callableToExecute = new ComputableCallable<T, V>(_computable, computableKey_);
      FutureTask<V> futueTask = new FutureTask<V>(callableToExecute);
      future = _bitmapFutureCache.putIfAbsent(key, futueTask);
      if (future == null) { 
        future = futueTask;
        futueTask.run();
      }
    }
    
    try {
       returnValue = future.get();
    }catch (CancellationException e) {
      L.v(TAG, "Future task was cancelled, so removing task from cache with key: " + computableKey_);
      _bitmapFutureCache.remove(key);
    }catch (InterruptedException e){
      _bitmapFutureCache.remove(key);
      throw new InterruptedImageLoadException("Image load was interrupted", e);
    }
    
    _bitmapFutureCache.remove(key);
    return returnValue;
  }
  
}




Java Source Code List

com.sun.imageloader.cache.api.MemoryCache.java
com.sun.imageloader.cache.impl.DiskCache.java
com.sun.imageloader.cache.impl.ImageFileFilter.java
com.sun.imageloader.cache.impl.LRUCache.java
com.sun.imageloader.cache.impl.SoftCache.java
com.sun.imageloader.computable.impl.ComputableImage.java
com.sun.imageloader.computable.impl.Computable.java
com.sun.imageloader.concurrent.ComputableCallable.java
com.sun.imageloader.concurrent.DisplayImageTask.java
com.sun.imageloader.concurrent.ImageLoaderTask.java
com.sun.imageloader.core.FlingLock.java
com.sun.imageloader.core.ImageKey.java
com.sun.imageloader.core.ImagePreferences.java
com.sun.imageloader.core.ImageSettings.java
com.sun.imageloader.core.ImageWriter.java
com.sun.imageloader.core.SimpleImageListenerImpl.java
com.sun.imageloader.core.UrlImageLoaderConfiguration.java
com.sun.imageloader.core.UrlImageLoader.java
com.sun.imageloader.core.UrlImageTaskExecutor.java
com.sun.imageloader.core.api.FailedTaskReason.java
com.sun.imageloader.core.api.ImageFailListenter.java
com.sun.imageloader.core.api.ImageTaskListener.java
com.sun.imageloader.core.api.Settings.java
com.sun.imageloader.downloader.api.ImageRetriever.java
com.sun.imageloader.downloader.impl.ImageDownloader.java
com.sun.imageloader.downloader.impl.ImageRetrieverFactory.java
com.sun.imageloader.downloader.impl.Scheme.java
com.sun.imageloader.imagedecoder.api.ImageDecoder.java
com.sun.imageloader.imagedecoder.impl.SimpleImageDecoder.java
com.sun.imageloader.memorizer.api.AMemorizer.java
com.sun.imageloader.memorizer.api.BitmapMemorizer.java
com.sun.imageloader.memorizer.api.IMemorizer.java
com.sun.imageloader.memorizer.api.InterruptedImageLoadException.java
com.sun.imageloader.utils.KeyUtils.java
com.sun.imageloader.utils.L.java
com.sun.imageloader.utils.ViewUtils.java