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:net.sf.nmedit.jtheme.JTCursor.java

public static Cursor getJackCursor(int id) {
    if (!(0 <= id && id < names.length))
        return null;

    SoftReference<?> ref = cursors[id];

    Cursor cursor = null;/*w w w .  j  a v  a2s  . c o  m*/
    if (ref == null) {
        cursor = createCursor(id);
        cursors[id] = new SoftReference<Cursor>(cursor);
        return cursor;
    }
    cursor = (Cursor) ref.get();
    if (cursor == null) {
        cursor = createCursor(id);
        cursors[id] = new SoftReference<Cursor>(cursor);
    }

    return cursor;
}

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

/**
 * Removes all the callbacks from the drawables stored in the memory cache. This
 * method must be called from the onDestroy() method of any activity using the
 * cached drawables. Failure to do so will result in the entire activity being
 * leaked.// ww w.  j  av a2  s.com
 */
public static void cleanupCache() {
    for (SoftReference<FastBitmapDrawable> reference : sArtCache.values()) {
        final FastBitmapDrawable drawable = reference.get();
        if (drawable != null) {
            drawable.setCallback(null);
        }
    }
}

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  .c  o m
 *
 * @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:mobisocial.musubi.service.WebRenderService.java

public static ImageView newLazyImageWeb(Context context, String html, int targetWidth, int targetHeight) {
    ImageView iv = new ImageView(context);
    //try the short cache
    if (sService != null) {
        SoftReference<Bitmap> srb = sService.mWebViewCache
                .get(new TByteArrayList(Util.sha256(html.getBytes())));
        if (srb != null) {
            Bitmap b = srb.get();
            if (b != null) {
                sService.setImageViewBitmapAndLayout(iv, b);
                return iv;
            }/*  ww  w.ja va  2  s.  c  o  m*/
        }
    }
    //set a default?
    iv.setImageBitmap(Bitmap.createBitmap(1, 1, Config.RGB_565));
    WebRenderRequest req = new WebRenderRequest();
    req.targetHeight = targetHeight;
    req.targetWidth = targetWidth;
    req.mDestionationView = iv;
    req.mHtml = html;
    if (sService != null) {
        sService.mWebViewClient.addItem(req);
    } else {
        sToProcess.add(req);
    }
    return iv;
}

From source file:org.wso2.carbon.feature.mgt.core.util.IUPropertyUtils.java

/**
 * Collects the installable unit fragments that contain locale data for the given locales.
 *///  ww w  .ja  v a2s . c  om
private static synchronized Collection getLocalizationFragments(Locale locale, List localeVariants) {
    SoftReference collectorRef = (SoftReference) LocaleCollectionCache.get(locale);
    if (collectorRef != null) {
        Collection cached = (Collection) collectorRef.get();
        if (cached != null) {
            return cached;
        }
    }

    final List locales = localeVariants;

    Collector localeFragmentCollector = new Collector() {
        public boolean accept(Object object) {
            boolean haveLocale = false;
            if (object instanceof IInstallableUnitFragment) {
                IInstallableUnitFragment fragment = (IInstallableUnitFragment) object;
                Collection<IProvidedCapability> providedCapabilities = fragment.getProvidedCapabilities();
                for (IProvidedCapability providedCapability : providedCapabilities) {
                    IProvidedCapability nextProvide = providedCapability;
                    if (NAMESPACE_IU_LOCALIZATION.equals(nextProvide.getNamespace())) {
                        String providedLocale = nextProvide.getName();
                        if (providedLocale != null) {
                            for (Iterator iter = locales.iterator(); iter.hasNext();) {
                                if (providedLocale.equals(iter.next())) {
                                    haveLocale = true;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            return (haveLocale ? super.accept(object) : true);
        }
    };

    //Due to performance problems we restrict locale lookup to the current profile (see bug 233958)
    IProfileRegistry profileRegistry = null;
    try {
        profileRegistry = (IProfileRegistry) ServiceHolder.getProfileRegistry();
    } catch (ProvisioningException e) {
        log.warn("Profile registry unavailable. Default language will be used.");
        return Collections.emptySet();
    }

    IProfile profile = profileRegistry.getProfile(IProfileRegistry.SELF);
    if (profile == null) {
        log.warn("Profile unavailable. Default language will be used.");
        return Collections.emptySet();
    }
    Collection collection = profile
            .query(QueryUtil.createIUPropertyQuery(
                    MetadataFactory.InstallableUnitDescription.PROP_TYPE_FRAGMENT, "true"), null)
            .toUnmodifiableSet();
    LocaleCollectionCache.put(locale, new SoftReference(collection));
    return collection;
}

From source file:com.image.oom.ImageUtil.java

public static Bitmap getBitmapFromCache(String url) {
    // First try the hard reference cache
    synchronized (sHardBitmapCache) {
        final Bitmap bitmap = sHardBitmapCache.get(url);
        if (bitmap != null) {
            // Bitmap found in hard cache
            // Move element to first position, so that it is removed last
            sHardBitmapCache.remove(url);
            sHardBitmapCache.put(url, bitmap);
            return bitmap;
        }//from  ww  w  .j  a va  2s  . c o m
    }

    // Then try the soft reference cache
    SoftReference<Bitmap> bitmapReference = sSoftBitmapCache.get(url);
    if (bitmapReference != null) {
        final Bitmap bitmap = bitmapReference.get();
        if (bitmap != null) {
            // Bitmap found in soft cache
            return bitmap;
        } else {
            // Soft reference has been Garbage Collected
            sSoftBitmapCache.remove(url);
            Log.e("imageDowlnloader", "sSoftBitmapCache.remove");
        }
    }

    return null;
}

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. ja  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:edu.stanford.mobisocial.dungbeetle.Helpers.java

public static Contact getContact(Context context, long contactId) {
    SoftReference<Contact> entry = g_contacts.get(contactId);
    if (entry != null) {
        Contact c = entry.get();
        if (c != null)
            return c;
    }//w w w  .  java  2 s.c  om
    Contact c = forceGetContact(context, contactId);
    g_contacts.put(contactId, new SoftReference<Contact>(c));
    return c;
}

From source file:biz.varkon.shelvesom.util.ImageUtilities.java

/**
 * Removes all the callbacks from the drawables stored in the memory cache.
 * This method must be called from the onDestroy() method of any activity
 * using the cached drawables. Failure to do so will result in the entire
 * activity being leaked./*from w w  w  .j  ava2 s  .  c  o  m*/
 */
public static void cleanupCache() {
    for (SoftReference<FastBitmapDrawable> reference : sArtCache.values()) {
        final FastBitmapDrawable drawable = reference.get();
        if (drawable != null)
            drawable.setCallback(null);
    }
}

From source file:biz.varkon.shelvesom.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 drawable is returned.
 * /*from   ww w. j av  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
 * @throws IOException
 * @throws FileNotFoundException
 */
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) {
        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;
}