Back to project page SIC.
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.
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; } }