Add comments to ClassLoader scoped weak map

This commit is contained in:
Luca Abbati 2019-07-16 11:00:48 +02:00
parent de6f33c035
commit bfa5712c04
No known key found for this signature in database
GPG Key ID: 74DBB952D9BA17F2
1 changed files with 9 additions and 0 deletions

View File

@ -4,12 +4,17 @@ import datadog.trace.bootstrap.WeakMap;
import java.util.HashMap;
import java.util.Map;
/** A registry which is scoped per-classloader able to hold key-value pairs with weak keys. */
public class ClassLoaderScopedWeakMap {
public static final ClassLoaderScopedWeakMap INSTANCE = new ClassLoaderScopedWeakMap();
private final WeakMap<ClassLoader, Map<Object, Object>> map = WeakMap.Supplier.DEFAULT.get();
/**
* Gets the element registered at the specified key or register as new one retrieved by the
* provided supplier.
*/
public synchronized Object getOrCreate(
ClassLoader classLoader, Object key, Supplier valueSupplier) {
Map<Object, Object> classLoaderMap = map.get(classLoader);
@ -27,6 +32,10 @@ public class ClassLoaderScopedWeakMap {
return value;
}
/**
* Supplies the value to be stored and it is called only when a value does not exists yet in the
* registry.
*/
public interface Supplier {
Object get();
}