Example usage for java.lang.ref SoftReference get

List of usage examples for java.lang.ref SoftReference get

Introduction

In this page you can find the example usage for java.lang.ref SoftReference get.

Prototype

public T get() 

Source Link

Document

Returns this reference object's referent.

Usage

From source file:com.cm.beer.util.DrawableManager.java

/**
 * //from  w w w  .ja v  a2 s. co  m
 * @param urlString
 * @param imageView
 */
public void fetchDrawableOnThread(final String urlString, final ImageView imageView) {
    if (mDrawableCache.containsKey(urlString)) {
        if (Logger.isLogEnabled())
            Logger.log("Returning Drawable from Cache:" + urlString);
        SoftReference<Drawable> softReference = mDrawableCache.get(urlString);
        if ((softReference == null) || (softReference.get() == null)) {
            mDrawableCache.remove(urlString);
            if (Logger.isLogEnabled())
                Logger.log("fetchDrawableOnThread():Soft Reference has been Garbage Collected:" + urlString);
        } else {
            imageView.setImageDrawable(softReference.get());
            return;
        }

    }

    final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message message) {
            imageView.setImageDrawable((Drawable) message.obj);
        }
    };

    Thread thread = new Thread() {
        @Override
        public void run() {
            while (mLockCache.containsKey(urlString)) {
                if (Logger.isLogEnabled())
                    Logger.log("URI download request in progress:" + urlString);
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    Log.e(this.getClass().getName(), e.getMessage());
                }
            }
            Drawable drawable = fetchDrawable(urlString);
            Message message = handler.obtainMessage(1, drawable);
            handler.sendMessage(message);
        }
    };
    thread.start();
}

From source file:com.taobao.common.tedis.cache.DefaultLocalCache.java

public V put(K key, V value, Date expiry) {
    SoftReference<V> result = getCache(key).put(key, new SoftReference<V>(value));
    expiryCache.put(key, expiry.getTime());

    return result == null ? null : result.get();
}

From source file:com.cm.beer.util.DrawableManager.java

/**
 * /*from ww w  . ja v a2 s .  co  m*/
 * @param urlString
 * @return
 */
public byte[] fetchDrawableAsByteArray(String urlString) {
    byte[] bitmapdata = null;

    if (mDrawableCache.containsKey(urlString)) {
        if (Logger.isLogEnabled())
            Logger.log("Returning Drawable from Cache:" + urlString);
        SoftReference<Drawable> softReference = mDrawableCache.get(urlString);
        if ((softReference == null) || (softReference.get() == null)) {
            mDrawableCache.remove(urlString);
            if (Logger.isLogEnabled())
                Logger.log("fetchDrawable():Soft Reference has been Garbage Collected:" + urlString);
        } else {
            Drawable drawable = softReference.get();
            Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
            bitmapdata = stream.toByteArray();
        }
    }

    if (Logger.isLogEnabled())
        Logger.log("image url:" + urlString);
    try {
        // prevents multithreaded fetches for the same image
        mLockCache.put(urlString, urlString);
        if (Logger.isLogEnabled())
            Logger.log("Begin Downloading:" + urlString);
        InputStream is = fetch(urlString);
        if (Logger.isLogEnabled())
            Logger.log("End Downloading:" + urlString);
        Drawable drawable = Drawable.createFromStream(is, "src");
        mDrawableCache.put(urlString, new SoftReference<Drawable>(drawable));
        mLockCache.remove(urlString);
        if (Logger.isLogEnabled())
            Logger.log("got a thumbnail drawable: " + drawable.getBounds() + ", "
                    + drawable.getIntrinsicHeight() + "," + drawable.getIntrinsicWidth() + ", "
                    + drawable.getMinimumHeight() + "," + drawable.getMinimumWidth());

        Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        bitmapdata = stream.toByteArray();

    } catch (Throwable e) {
        Log.e(this.getClass().getName(), "fetchDrawable failed", e);
        return null;
    }
    return bitmapdata;
}

From source file:com.taobao.common.tedis.cache.DefaultLocalCache.java

public V get(K key) {
    checkValidate(key);//  w  ww .  ja  v a  2s. c o  m
    SoftReference<V> sr = getCache(key).get(key);
    if (sr == null) {
        expiryCache.remove(key);
        return null;
    } else {
        return sr.get();
    }
}

From source file:com.guang.eunormia.common.cache.DefaultLocalCache.java

@Override
public V put(K key, V value, Date expiry) {
    SoftReference<V> result = getCache(key).put(key, new SoftReference<V>(value));
    expiryCache.put(key, expiry.getTime());

    return result == null ? null : result.get();
}

From source file:com.guang.eunormia.common.cache.DefaultLocalCache.java

@Override
public V get(K key) {
    checkValidate(key);/* ww w  . j  av  a 2s  .c om*/
    SoftReference<V> sr = getCache(key).get(key);
    if (sr == null) {
        expiryCache.remove(key);
        return null;
    } else {
        return sr.get();
    }
}

From source file:FastCache.java

Record<K, V> getFirstRecord(int bucketId) {
    SoftReference<Record<K, V>> ref = mBuckets[bucketId];
    return ref == null ? null : ref.get();
}

From source file:org.jinzora.util.DrawableManager.java

public void fetchDrawableOnThread(final String urlString, final ImageView imageView) {
    if (urlString == null) {
        if (DBG)/*from w w  w. j  ava  2 s .com*/
            Log.d(TAG, "Null image url.");
        synchronized (pendingViews) {
            pendingViews.remove(imageView);
        }
        return;
    }

    synchronized (pendingViews) {
        pendingViews.put(imageView, urlString);
    }

    if (drawableMap.containsKey(urlString)) {
        SoftReference<Drawable> reference = drawableMap.get(urlString);
        Drawable drawable = reference.get();
        if (drawable != null) {
            if (DBG)
                Log.d(TAG, "Image cached in memory.");
            imageView.setImageDrawable(drawable);
            return;
        } else {
            if (DBG)
                Log.d(TAG, "Soft reference cleared.");
            drawableMap.remove(urlString);
        }
    }

    File localFile = getLocalFile(urlString);
    if (localFile != null && localFile.exists()) {
        if (DBG)
            Log.d(TAG, "Image from disk.");
        imageView.setImageDrawable(fetchDrawable(urlString));
        return;
    }

    final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message message) {
            synchronized (pendingViews) {
                String latest = pendingViews.get(imageView);
                if (urlString.equals(latest)) {
                    imageView.setImageDrawable((Drawable) message.obj);
                }
            }
        }
    };

    new Thread() {
        @Override
        public void run() {
            Drawable drawable = fetchDrawable(urlString);
            Message message = handler.obtainMessage(1, drawable);
            handler.sendMessage(message);
        }
    }.start();
}

From source file:net.sf.janos.util.ui.softcache.SoftCache.java

/**
 * @param key The key//from ww w  .ja  v  a 2 s .  c  o  m
 * @return the value corresponding to <code>key</code> (may be null)
 * @throws NoSuchKeyException if the key is not present in the cache
 */
public final V get(K key) throws NoSuchKeyException {
    expungeStaleEntries();
    SoftReference<V> ref = map.get(key);
    if (ref != null) {
        /*
         * we have to maintain a reference to val so it doesn't get removed after
         * we test for enqueued-ness (indicating a garbage collect since the start
         * of the method)
         */
        V val = ref.get();
        if (!ref.isEnqueued()) {
            return val;
        }
    }
    throw new NoSuchKeyException(
            "There is no entry for this key in the map (perhaps it's been garbage collected?)");
}

From source file:com.eviware.soapui.security.log.SecurityTestLogModel.java

public synchronized int getIndexOfSecurityScan(SecurityScan check) {
    for (int i = 0; i < results.size(); i++) {
        SoftReference<SecurityResult> result = results.get(i);
        if (result != null) {
            SecurityResult referent = result.get();
            if (referent instanceof SecurityScanResult) {
                if (((SecurityScanResult) referent).getSecurityScan() == check) {
                    return i;
                }//  w w w. ja v  a2 s  .c om
            }
        }
    }
    return -1;
}