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:org.apache.ojb.broker.cache.ObjectCacheLocalDefaultImpl.java

/**
 * Makes object persistent to the Objectcache.
 * I'm using soft-references to allow gc reclaim unused objects
 * even if they are still cached.//from  www. j  av a 2  s .  c  om
 */
public void cache(Identity oid, Object obj) {
    if ((obj != null)) {
        SoftReference ref = new SoftReference(new CacheEntry(obj));
        objectTable.put(oid, ref);
    }
}

From source file:myfightinglayoutbugs.ScreenshotCache.java

@Nonnull
public Screenshot getScreenshot(Condition condition) {
    SoftReference<Screenshot> softReference = _memoryCache.get(condition);
    Screenshot screenshot;//  www.  j  a  va 2 s. com
    if (softReference == null) {
        // Cache miss, take screenshot ...
        screenshot = takeScreenshot(condition);
        // ... and cache it ...
        _memoryCache.put(condition, new SoftReference<Screenshot>(screenshot));
        _diskCache.put(condition, saveToTempFile(screenshot));
    } else {
        screenshot = softReference.get();
        if (screenshot == null) {
            // screenshot in _memoryCache was garbage collected, read it from _diskCache ...
            File file = _diskCache.get(condition);
            screenshot = readFromFile(file);
            // ... and put it into _memoryCache again ...
            _memoryCache.put(condition, new SoftReference<Screenshot>(screenshot));
        }
    }
    return screenshot;
}

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  www . j  a v a2  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));
        }
    }
}

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:org.copperengine.core.persistent.hybrid.StorageCache.java

@Override
public ListenableFuture<Void> safeEarlyResponse(String correlationId, String serializedResponse)
        throws Exception {
    earCache.put(correlationId, new SoftReference<String>(serializedResponse));
    return delegate.safeEarlyResponse(correlationId, serializedResponse);
}

From source file:com.anrisoftware.prefdialog.miscswing.dialogsworker.AbstractCreateDialogWorker.java

/**
 * Returns the dialog. If not already created, the dialog is created on the
 * AWT event thread./*  www.ja  v a  2s  .c  om*/
 *
 * @return the dialog.
 *
 * @throws CreateDialogWorkerException
 *             if there was an error creating the dialog.
 *
 * @throws CreateDialogInterrupedException
 *             if the dialog creation on the AWT event thread was
 *             interrupted.
 */
public synchronized DialogType getDialog() throws CreateDialogWorkerException, CreateDialogInterrupedException {
    if (needRecreateDialog()) {
        this.dialog = new SoftReference<DialogType>(doCreateDialog());
    }
    return dialog.get();
}

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  ava 2  s.com*/
 */
@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:com.eviware.soapui.security.log.SecurityTestLogModel.java

/**
 * called before TestStep SecurityScans just to mark beginning of TestStep
 * SecurityLog part to be updated after step scans finish with proper values
 * // www  . j  a  v a  2s  .  c  o  m
 * @return true - if added, false - otherwise
 */
public synchronized boolean addSecurityTestStepResult(TestStep testStep) {
    stepCount++;
    checkCount = 0;

    currentStepEntriesCount = 1;
    int size = items.size();
    if (AbstractSecurityScan.isSecurable(testStep)) {
        SecurityTestStepResult result = new SecurityTestStepResult(testStep, null);
        SoftReference<SecurityResult> stepResultRef = new SoftReference<SecurityResult>(result);
        items.add("Step " + stepCount + " [" + result.getTestStep().getName() + "] ");
        results.add(stepResultRef);

        fireIntervalAdded(this, size, items.size() - 1);
        enforceMaxSize();
        return true;
    } else
        return false;
}

From source file:com.siahmsoft.soundroid.sdk7.util.ImageUtilities.java

/**
 * Retrieves a drawable from the book covers cache, identified by the specified id.
 * If the drawable does not exist in the cache, it is loaded and added to the cache.
 * If the drawable cannot be added to the cache, the specified default drwaable is
 * returned./*from  w  ww .  jav a2 s.com*/
 *
 * @param id The id of the drawable to retrieve
 * @param defaultCover The default drawable returned if no drawable can be found that
 *         matches the id
 *
 * @return The drawable identified by id or defaultCover
 */
public static FastBitmapDrawable getCachedCover(String id, FastBitmapDrawable defaultCover) {
    FastBitmapDrawable drawable = null;

    SoftReference<FastBitmapDrawable> reference = sArtCache.get(id);
    if (reference != null) {
        drawable = reference.get();
    }

    if (drawable == null || drawable == NULL_DRAWABLE) {
        final Bitmap bitmap = loadCover(id);
        if (bitmap != null) {
            drawable = new FastBitmapDrawable(bitmap);
        } else {
            drawable = NULL_DRAWABLE;
        }

        sArtCache.put(id, new SoftReference<FastBitmapDrawable>(drawable));
    }

    return drawable == NULL_DRAWABLE ? defaultCover : drawable;
}

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();
}