Example usage for java.lang.ref SoftReference SoftReference

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

Introduction

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

Prototype

public SoftReference(T referent) 

Source Link

Document

Creates a new soft reference that refers to the given object.

Usage

From source file:com.andrewshu.android.reddit.threads.BitmapManager.java

public Bitmap fetchBitmap(String urlString) {
    SoftReference<Bitmap> ref = mCache.get(urlString);
    if (ref != null && ref.get() != null) {
        return ref.get();
    }// w  ww.j  ava 2 s . co m

    if (Constants.LOGGING)
        Log.d(TAG, "image url:" + urlString);

    try {
        Bitmap bitmap = readBitmapFromNetwork(urlString);
        mCache.put(urlString, new SoftReference<Bitmap>(bitmap));
        //          if (Constants.LOGGING) Log.d(this.getClass().getSimpleName(), "got a thumbnail drawable: " + drawable.getBounds() + ", "
        //                + drawable.getIntrinsicHeight() + "," + drawable.getIntrinsicWidth() + ", "
        //                + drawable.getMinimumHeight() + "," + drawable.getMinimumWidth());
        return bitmap;
    } catch (Exception e) {
        if (Constants.LOGGING)
            Log.e(TAG, "fetchBitmap failed", e);
        return null;
    }
}

From source file:org.hsweb.concureent.cache.monitor.SimpleMonitorCache.java

protected Object buildValue(Object value) {
    return new SoftReference(value);
}

From source file:com.binroot.fatpita.BitmapManager.java

public Bitmap fetchBitmap(String urlString, boolean saveToHistory) {
    SoftReference<Bitmap> ref = mCache.get(urlString);
    if (ref != null && ref.get() != null) {
        return ref.get();
    }// ww w .j  a v  a  2 s .  c  om

    Log.d(TAG, "image url:" + urlString);

    try {
        Bitmap bitmap = readBitmapFromNetwork(urlString, saveToHistory);
        mCache.put(urlString, new SoftReference<Bitmap>(bitmap));
        //          if (Constants.LOGGING) Log.d(this.getClass().getSimpleName(), "got a thumbnail drawable: " + drawable.getBounds() + ", "
        //                + drawable.getIntrinsicHeight() + "," + drawable.getIntrinsicWidth() + ", "
        //                + drawable.getMinimumHeight() + "," + drawable.getMinimumWidth());
        return bitmap;
    } catch (Exception e) {
        Log.e(TAG, "fetchBitmap failed", e);
        return null;
    }
}

From source file:de.handtwerk.android.IoHelper.java

public static JSONArray readAssetAsJsonArray(String pPath, Context pContext) throws IOException, JSONException {

    synchronized (JSON_ARRAY_CACHE) {

        SoftReference<JSONArray> ref = JSON_ARRAY_CACHE.get(pPath);
        if (ref != null && ref.get() != null) {

            return ref.get();
        }/*from ww  w.  j a v a 2 s  .co m*/

        String jsonStr = readAssetAsString(pPath, pContext);
        JSONArray json = new JSONArray(jsonStr);

        JSON_ARRAY_CACHE.put(pPath, new SoftReference<JSONArray>(json));
        return json;
    }
}

From source file:com.abelsky.idea.geekandpoke.entries.Entry.java

public Entry(EntryInfo onlineEntry, BufferedImage image) {
    entryInfo = onlineEntry;
    this.imageRef = new SoftReference<BufferedImage>(image);
}

From source file:edu.mayo.cts2.framework.plugin.service.bioportal.transform.AbstractBioportalOntologyVersionTransformTemplate.java

private static DateFormat getDateFormat() {
    SoftReference<DateFormat> ref = threadLocal.get();
    if (ref != null) {
        DateFormat result = ref.get();
        if (result != null) {
            return result;
        }//from ww w .  j a  v a 2 s .c o  m
    }
    DateFormat result = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S zzz");
    ref = new SoftReference<DateFormat>(result);
    threadLocal.set(ref);
    return result;
}

From source file:org.copperengine.core.persistent.hybrid.StorageCache.java

@Override
public WorkflowInstance readWorkflowInstance(String wfId) throws Exception {
    SoftReference<WorkflowInstance> entry = wfCache.get(wfId);
    if (entry != null) {
        WorkflowInstance wfi = entry.get();
        if (wfi != null) {
            cacheStatsWfCache.incNumberOfReads(true);
            return wfi;
        }/*from  w  w  w .  j  a  v a 2s .c  o  m*/
    }
    WorkflowInstance wfi = delegate.readWorkflowInstance(wfId);
    if (wfi != null) {
        wfCache.put(wfi.id, new SoftReference<WorkflowInstance>(wfi));
    }
    cacheStatsWfCache.incNumberOfReads(false);
    return wfi;
}

From source file:org.openvpms.web.echo.util.DoubleClickMonitor.java

/**
 * Determines if there has been a double click on an object.
 * <p/>//from  ww  w. jav  a  2 s  .c o  m
 * It is considered a double click if the method is called twice with the same (i.e using equals()) object,
 * within the specified interval. If a double click is detected, the monitor resets - two more clicks are required
 * within the interval to be considered another double click.
 *
 * @param object the clicked on object. May be <tt>null</tt>
 * @return true if the object has been clicked twice within the interval
 */
public boolean isDoubleClick(Object object) {
    boolean result;
    Date now = new Date();
    result = (lastClick != null && (interval == 0 || (lastClick.getTime() + interval) >= now.getTime()));
    Object old = (last != null) ? last.get() : null;
    result = result && ObjectUtils.equals(old, object);
    if (result) {
        reset();
    } else {
        lastClick = now;
        last = new SoftReference<Object>(object);
    }
    return result;
}

From source file:org.topazproject.xml.transform.MemoryCacheURLRetriever.java

/**
 * Lookup the <code>id</code> in the cache. If found, return results. Otherwise, call
 * delegate to fetch content./*from   ww  w  .  j a  v a2s.  c o m*/
 * 
 * @param url the url of the resource to retrieve
 * @param id  the id of the resource to retrieve
 * @return the contents, or null if not found
 * @throws IOException if an error occurred retrieving the contents (other than not-found)
 */
public synchronized byte[] retrieve(String url, String id) throws IOException {
    SoftReference ref = (SoftReference) cache.get(id);
    byte[] res = (ref != null) ? (byte[]) ref.get() : null;

    if (log.isDebugEnabled())
        log.debug("Memory cache('" + id + "'): "
                + (res != null ? "found" : ref != null ? "expired" : "not found"));

    if (res != null || delegate == null)
        return res;

    res = delegate.retrieve(url, id);
    if (res == null)
        return null;

    if (log.isDebugEnabled())
        log.debug("Caching '" + id + "'");

    cache.put(id, new SoftReference(res));
    return res;
}

From source file:edu.ku.brc.stats.StatsMgr.java

/**
 * Returns the Statistics DOM.//from   w  w w.  j  a v a 2  s.c  o  m
 * @return the Statistics DOM.
 */
public static Element getDOM() {
    Element dom = null;
    if (statDOM != null) {
        dom = statDOM.get();
    }

    if (dom == null) {

        statDOM = new SoftReference<Element>(loadDOM());
        dom = statDOM.get();
    }

    if (resourceName == null) {
        resourceName = XMLHelper.getAttr(dom, "resource", null);
        loadStatTooltips();
    }

    return dom;
}