Example usage for java.lang.ref WeakReference WeakReference

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

Introduction

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

Prototype

public WeakReference(T referent) 

Source Link

Document

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

Usage

From source file:javadz.beanutils.MethodUtils.java

/**
 * Add a method to the cache./*  w ww. j  a v  a 2s  . c  o m*/
 *
 * @param md The method descriptor
 * @param method The method to cache
 */
private static void cacheMethod(MethodDescriptor md, Method method) {
    if (CACHE_METHODS) {
        if (method != null) {
            cache.put(md, new WeakReference(method));
        }
    }
}

From source file:com.corebase.android.framework.http.client.AsyncHttpClient.java

/**
 * ???//w ww. ja  v a 2 s  .  c om
 * 
 * @param client
 * @param httpContext
 * @param uriRequest
 * @param contentType
 * @param cacheParams
 * @param responseHandler
 * @param context
 * @param isGetDataBase
 */
protected void sendRequestByDateBase(DefaultHttpClient client, HttpContext httpContext,
        HttpUriRequest uriRequest, String contentType, CacheParams cacheParams,
        AsyncHttpResponseHandler responseHandler, Context context, boolean isGetDataBase) {
    if (contentType != null) {
        uriRequest.addHeader("Content-Type", contentType);
    }

    Future<?> request = threadPool.submit(new AsyncHttpRequest(client, httpContext, uriRequest, cacheParams,
            responseHandler, context, isGetDataBase));
    if (context != null) {
        // Add request to request map
        List<WeakReference<Future<?>>> requestList = requestMap.get(context);
        if (requestList == null) {
            requestList = new LinkedList<WeakReference<Future<?>>>();
            requestMap.put(context, requestList);
        }

        requestList.add(new WeakReference<Future<?>>(request));

    }
}

From source file:czd.lib.network.AsyncHttpClient.java

/**
 * Puts a new request in queue as a new thread in pool to be executed
 *
 * @param client          HttpClient to be used for request, can differ in single requests
 * @param contentType     MIME body type, for POST and PUT requests, may be null
 * @param context         Context of Android application, to hold the reference of request
 * @param httpContext     HttpContext in which the request will be executed
 * @param responseHandler ResponseHandler or its subclass to put the response into
 * @param uriRequest      instance of HttpUriRequest, which means it must be of HttpDelete,
 *                        HttpPost, HttpGet, HttpPut, etc.
 *
 * @return RequestHandle of future request process
 *///from   w w  w . ja  v  a 2s .com
protected RequestHandle sendRequest(DefaultHttpClient client, HttpContext httpContext,
        HttpUriRequest uriRequest, String contentType, ResponseHandlerInterface responseHandler,
        Context context) {
    if (contentType != null) {
        uriRequest.setHeader("Content-Type", contentType);
    }

    if (responseHandler instanceof RangeFileAsyncHttpResponseHandler)
        ((RangeFileAsyncHttpResponseHandler) responseHandler).updateRequestHeaders(uriRequest);
    responseHandler.setRequestHeaders(uriRequest.getAllHeaders());
    responseHandler.setRequestURI(uriRequest.getURI());

    Future<?> request = threadPool
            .submit(new AsyncHttpRequest(client, httpContext, uriRequest, responseHandler));

    if (context != null) {
        // Add request to request map
        List<WeakReference<Future<?>>> requestList = requestMap.get(context);
        if (requestList == null) {
            requestList = new LinkedList<WeakReference<Future<?>>>();
            requestMap.put(context, requestList);
        }

        requestList.add(new WeakReference<Future<?>>(request));

        // TODO: Remove dead weakrefs from requestLists?
    }

    return new RequestHandle(request);
}

From source file:eu.qualityontime.commons.MethodUtils.java

/**
 * Add a method to the cache./* ww w . jav  a2 s .  c  o  m*/
 *
 * @param md
 *            The method descriptor
 * @param method
 *            The method to cache
 */
private static void cacheMethod(final MethodDescriptor md, final Method method) {
    if (CACHE_METHODS) {
        if (method != null) {
            cache.put(md, new WeakReference<Method>(method));
        }
    }
}

From source file:com.rapidminer.gui.new_plotter.configuration.PlotConfiguration.java

public void addPlotConfigurationProcessingListener(PlotConfigurationProcessingListener l) {
    processingListeners.add(new WeakReference<PlotConfigurationProcessingListener>(l));
}

From source file:com.meltmedia.cadmium.servlets.ClassLoaderLeakPreventor.java

/**
 * Unlike <code>{@link System#gc()}</code> this method guarantees that garbage collection has been performed before
 * returning.//from w  w  w .  j a  v a  2 s .  c  o m
 */
protected static void gc() {
    Object obj = new Object();
    WeakReference ref = new WeakReference<Object>(obj);
    //noinspection UnusedAssignment
    obj = null;
    while (ref.get() != null) {
        System.gc();
    }
}

From source file:io.realm.Realm.java

/**
 * Add a change listener to the Realm/*from   w  ww  .  ja  v a 2 s.  c o  m*/
 *
 * @param listener the change listener
 * @see io.realm.RealmChangeListener
 */
public void addChangeListener(RealmChangeListener listener) {
    checkIfValid();
    for (WeakReference<RealmChangeListener> ref : changeListeners) {
        if (ref.get() == listener) {
            // It has already been added before
            return;
        }
    }

    changeListeners.add(new WeakReference<RealmChangeListener>(listener));
}

From source file:com.scoreflex.Scoreflex.java

protected static void setCurrentScoreflexView(ScoreflexView view) {
    mScoreflexView = new WeakReference<ScoreflexView>(view);
}

From source file:com.rapidminer.gui.new_plotter.engine.jfreechart.JFreeChartPlotEngine.java

public void addPlotEngineListener(JFreeChartPlotEngineListener l) {
    listeners.add(new WeakReference<JFreeChartPlotEngineListener>(l));
}

From source file:com.buaa.cfs.conf.Configuration.java

/**
 * Load a class by name, returning null rather than throwing an exception if it couldn't be loaded. This is to avoid
 * the overhead of creating an exception.
 *
 * @param name the class name/*w w  w.  j av a2s. c o  m*/
 *
 * @return the class object, or null if it could not be found.
 */
public Class<?> getClassByNameOrNull(String name) {
    Map<String, WeakReference<Class<?>>> map;

    synchronized (CACHE_CLASSES) {
        map = CACHE_CLASSES.get(classLoader);
        if (map == null) {
            map = Collections.synchronizedMap(new WeakHashMap<String, WeakReference<Class<?>>>());
            CACHE_CLASSES.put(classLoader, map);
        }
    }

    Class<?> clazz = null;
    WeakReference<Class<?>> ref = map.get(name);
    if (ref != null) {
        clazz = ref.get();
    }

    if (clazz == null) {
        try {
            clazz = Class.forName(name, true, classLoader);
        } catch (ClassNotFoundException e) {
            // Leave a marker that the class isn't found
            map.put(name, new WeakReference<Class<?>>(NEGATIVE_CACHE_SENTINEL));
            return null;
        }
        // two putters can race here, but they'll put the same class
        map.put(name, new WeakReference<Class<?>>(clazz));
        return clazz;
    } else if (clazz == NEGATIVE_CACHE_SENTINEL) {
        return null; // not found
    } else {
        // cache hit
        return clazz;
    }
}