Example usage for java.util WeakHashMap WeakHashMap

List of usage examples for java.util WeakHashMap WeakHashMap

Introduction

In this page you can find the example usage for java.util WeakHashMap WeakHashMap.

Prototype

public WeakHashMap() 

Source Link

Document

Constructs a new, empty WeakHashMap with the default initial capacity (16) and load factor (0.75).

Usage

From source file:org.openmrs.module.ModuleFactory.java

/**
 * Return all current classloaders keyed on module object
 * //www  . ja  v  a2  s  .com
 * @return Map<Module, ModuleClassLoader>
 */
public static Map<Module, ModuleClassLoader> getModuleClassLoaderMap() {
    if (moduleClassLoaders == null) {
        moduleClassLoaders = new WeakHashMap<Module, ModuleClassLoader>();
    }

    return moduleClassLoaders;
}

From source file:org.openmrs.module.ModuleFactory.java

/**
 * Return the current extension map keyed on extension point id
 * //from   w ww  .  j av  a  2 s  . c o  m
 * @return Map&lt;String, List&lt;Extension&gt;&gt;
 */
public static Map<String, List<Extension>> getExtensionMap() {
    if (extensionMap == null) {
        extensionMap = new WeakHashMap<String, List<Extension>>();
    }

    return extensionMap;
}

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/* ww  w  .  jav  a  2 s  .com*/
 *
 * @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;
    }
}