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.eviware.soapui.impl.wsdl.testcase.TestCaseLogModel.java

public synchronized TestStepResult getResultAt(int index) {
    if (index >= results.size())
        return null;

    SoftReference<TestStepResult> result = results.get(index);
    return result == null ? null : result.get();
}

From source file:SoftHashMap.java

public V get(Object key) {
    processQueue();/*from ww  w .jav  a  2 s. c o  m*/
    SoftReference<V> o = map.get(key);
    if (o == null) {
        return null;
    }
    return o.get();
}

From source file:net.sf.ehcache.distribution.EventMessageTest.java

/**
 * SoftReference behaviour testing.// w w w.ja va  2s . c om
 */
public void testSoftReferences() {
    AbstractCacheTest.forceVMGrowth();
    Map map = new HashMap();
    for (int i = 0; i < 100; i++) {
        map.put(new Integer(i), new SoftReference(new byte[1000000]));
    }

    int counter = 0;
    for (int i = 0; i < 100; i++) {
        SoftReference softReference = (SoftReference) map.get(new Integer(i));
        byte[] payload = (byte[]) softReference.get();
        if (payload != null) {
            LOG.info("Value found for " + i);
            counter++;
        }
    }
    //This one varies by operating system and architecture. 
    assertTrue("You should get more than " + counter + " out of SoftReferences", counter >= 13);

}

From source file:SoftCache.java

public Object put(Object key, Object value) {
    SoftReference softRef = (SoftReference) map.put(key, new SoftReference(value));

    if (softRef == null)
        return null;

    Object oldValue = softRef.get();
    softRef.clear();//from w  w  w  .  jav  a 2s . co m

    return oldValue;
}

From source file:net.sf.maltcms.chromaui.charts.tooltips.RTIXYTooltipGenerator.java

/**
 *
 * @param xyd//  w ww  .j  ava  2s . c o m
 * @param i
 * @param i1
 * @return
 */
@Override
public String generateToolTip(XYDataset xyd, int i, int i1) {
    Point p = new Point(i, i1);
    if (hm.containsKey(p)) {
        SoftReference<String> sr = hm.get(p);
        if (sr.get() != null) {
            return sr.get();
        }
    } else {
        StringBuilder sb = new StringBuilder();
        int x = (int) xyd.getXValue(i, i1);
        int y = (int) xyd.getYValue(i, i1);
        double z = Double.NaN;
        if (xyd instanceof XYZDataset) {
            z = ((XYZDataset) xyd).getZValue(i, i1);
        }
        if (x >= 0 && x < lookup.length && y >= 0 && y < scansPerModulation) {
            sb.append("[ SCAN1: ");
            sb.append(x);
            sb.append(", SCAN2: " + y + " ]");
            sb.append(" at [ RT1: ");
            sb.append(lookup[x]);
            sb.append(" s, RT2: ");
            float off = (this.modulationTime * ((float) y / (float) (this.scansPerModulation)));
            sb.append(lookup[x] + off);
            sb.append("s ]");
            if (xyd instanceof XYZDataset) {
                sb.append(" = ");
                sb.append(z);
            }
            String s = sb.toString();
            SoftReference<String> sr = new SoftReference<>(s);
            hm.put(p, sr);
            return s;
        }
        return "";
    }
    return null;

}

From source file:org.codehaus.groovy.grails.commons.metaclass.WeakGenericDynamicProperty.java

public Object get(Object object) {
    String propertyKey = System.identityHashCode(object) + getPropertyName();

    SoftReference<Object> ref = propertyToInstanceMap.get(propertyKey);
    Object val = (ref != null) ? ref.get() : null;
    if (val != null) {
        return val;
    } else if (this.initialValueGenerator != null) {
        final Object value = this.initialValueGenerator.execute(object);
        propertyToInstanceMap.put(propertyKey, new SoftReference<Object>(value));
        return value;
    } else if (this.initialValue != null) {
        propertyToInstanceMap.put(propertyKey, new SoftReference<Object>(this.initialValue));
        return this.initialValue;
    }//from   ww  w  . ja v a 2  s.com
    return null;
}

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

@Override
public String readEarlyResponse(String correlationId) throws Exception {
    final SoftReference<String> entry = earCache.get(correlationId);
    if (entry != null) {
        String resp = entry.get();
        if (resp != null) {
            cacheStatsEarCache.incNumberOfReads(true);
            return resp;
        }/*ww w  . ja  v  a2s .com*/
    }
    cacheStatsEarCache.incNumberOfReads(false);
    return delegate.readEarlyResponse(correlationId);
}

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;
        }//  w w  w  .  ja va  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.knime.knip.base.data.ObjectRepository.java

/**
 * Caches an object. Can be retrieved by {@link #getCachedObject(MemoryReleasable)}, if not deleted.
 *
 * @param obj/*from   w ww  .  j  av  a2 s  .  c  o  m*/
 */
@SuppressWarnings("javadoc")
public final void cacheObject(final Object obj) {
    synchronized (CACHED_OBJECTS) {
        final SoftReference<Object> ref = new SoftReference<Object>(obj);
        CACHED_OBJECTS.put(obj.hashCode(), ref);
        ref.get();
        LOGGER.debug("Cached another object!");
    }

}

From source file:org.appcelerator.titanium.util.TiDownloadManager.java

protected void startDownload(URI uri, TiDownloadListener listener) {
    String hash = DigestUtils.shaHex(uri.toString());
    ArrayList<SoftReference<TiDownloadListener>> listenerList = null;
    synchronized (listeners) {
        if (!listeners.containsKey(hash)) {
            listenerList = new ArrayList<SoftReference<TiDownloadListener>>();
            listeners.put(hash, listenerList);
        } else {/*from w  w  w . j  ava  2 s.  c  o m*/
            listenerList = listeners.get(hash);
        }
        // We only allow a listener once per URI
        for (SoftReference<TiDownloadListener> l : listenerList) {
            if (l.get() == listener) {
                return;
            }
        }
        listenerList.add(new SoftReference<TiDownloadListener>(listener));
    }
    synchronized (downloadingURIs) {
        if (!downloadingURIs.contains(hash)) {
            downloadingURIs.add(hash);
            threadPool.execute(new DownloadJob(uri));
        }
    }
}