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:Main.java

public static void main(String[] argv) throws Exception {
    SoftReference<String> sr = new SoftReference<String>("object");

    Object o = sr.get();
    if (o != null) {
        System.out.println(o);// ww w .ja v  a  2 s. c  om
    } else {
        System.out.println("collected or has been reclaimed");
    }
}

From source file:Main.java

public static Typeface createFromAsset(final AssetManager assets, final String fontname) {
    Typeface result = null;/* ww w . j  a v  a  2  s . co  m*/
    SoftReference<Typeface> cachedFont = getFromCache(fontname);

    if (null != cachedFont && cachedFont.get() != null) {
        result = cachedFont.get();
    } else {
        result = Typeface.createFromAsset(assets, fontname);
        putIntoCache(fontname, result);
    }

    return result;
}

From source file:Main.java

/**
 * Returns a cached <i>JAXB Context</i>.
 *
 * @since 3.8//from  w ww .jav a2  s . c om
 */
public synchronized static JAXBContext createContext(final Class<?> bind) throws JAXBException {
    JAXBContext context = null;
    SoftReference<JAXBContext> ref = contextCache.get(bind);
    if (ref != null) {
        context = ref.get();
        if (context == null)
            contextCache.remove(bind);
    }

    if (context != null)
        return context;

    synchronized (bind) {
        synchronized (JAXBContext.class) {
            context = JAXBContext.newInstance(bind);
        }
    }

    contextCache.put(bind, new SoftReference<JAXBContext>(context));

    return context;
}

From source file:Main.java

public static Bitmap byteToBitmap(byte[] imgByte) {
    InputStream input = null;//  w w w.  j  ava 2 s  .  c o m
    Bitmap bitmap = null;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 1;
    input = new ByteArrayInputStream(imgByte);
    SoftReference softRef = new SoftReference(BitmapFactory.decodeStream(input, null, options));
    bitmap = (Bitmap) softRef.get();
    if (imgByte != null) {
        imgByte = null;
    }

    try {
        if (input != null) {
            input.close();
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return bitmap;
}

From source file:Main.java

public synchronized static ResourceBundle loadBundle(String resource) {
    if (sRefMap == null) {
        sRefMap = new HashMap<String, SoftReference<ResourceBundle>>();
    }/*from  w w  w  .  j a v  a 2 s . co  m*/
    SoftReference<ResourceBundle> bundleRef = sRefMap.get(resource);
    if (bundleRef == null || bundleRef.get() == null) {
        // Attempt to load the messages.
        try {
            ResourceBundle bundle = setLocale(Locale.getDefault(), resource);
            bundleRef = new SoftReference<ResourceBundle>(bundle);
            sRefMap.put(resource, bundleRef);
            return bundle;
        } catch (Throwable e) {
            //Logger.global.warning("Got Throwable " + e + " loading messages");
            return null;
        }
    } else {
        return bundleRef.get();
    }
}

From source file:Main.java

public static XPathExpression getXPathExpression(String xPathString) throws XPathExpressionException {
    SoftReference<XPathExpression> ref = xPathExpressionsCache.get(xPathString);
    XPathExpression expression = ref == null ? null : ref.get();
    if (expression == null)
        xPathExpressionsCache.put(xPathString,
                new SoftReference<XPathExpression>(expression = getSharedXPath().compile(xPathString)));

    return expression;
}

From source file:org.openanzo.rdf.MemVariable.java

/**
 * Static factory method that uses an WeakHashMap cache to reuse {@link MemVariable} objects for variables with the same name.
 * /*from   ww w .  ja v a  2  s . c o  m*/
 * @param variableName
 *            name of variable
 * @return {@link MemVariable} for given name
 */
static public MemVariable createVariable(String variableName) {
    if (variableName.startsWith("?")) {
        throw new AnzoRuntimeException(ExceptionConstants.CLIENT.VARIABLE_START_QUESTION, variableName);
    }
    if (!StringUtils.containsNone(variableName, " \t\n\r\f")) {
        throw new AnzoRuntimeException(ExceptionConstants.CLIENT.VARIABLE_NO_SPACES, variableName);
    }
    SoftReference<MemVariable> ref = cache.get(variableName);
    MemVariable var = (ref != null) ? ref.get() : null;
    if (var == null) {
        var = new MemVariable(variableName);
        cache.put(variableName, new SoftReference<MemVariable>(var));
    }
    return var;
}

From source file:de.undercouch.bson4jackson.io.StaticBuffers.java

/**
 * @return a thread-local singleton instance of this class
 *///from   w  w w  .j av a  2s.  c om
public static StaticBuffers getInstance() {
    SoftReference<StaticBuffers> ref = _instance.get();
    StaticBuffers buf = (ref == null ? null : ref.get());
    if (buf == null) {
        buf = new StaticBuffers();
        _instance.set(new SoftReference<StaticBuffers>(buf));
    }
    return buf;
}

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 w  w  w.  j av a  2 s .com

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

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

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

public static JSONObject readAssetAsJsonObject(String pPath, Context pContext)
        throws IOException, JSONException {

    synchronized (JSON_OBJECT_CACHE) {

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

            return ref.get();
        }// w w  w .  j  a va2  s  . c  om

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

        JSON_OBJECT_CACHE.put(pPath, new SoftReference<JSONObject>(json));
        return json;
    }
}