All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jruby.util.collections.MapBasedClassValue Maven / Gradle / Ivy

package org.jruby.util.collections;

import java.util.concurrent.ConcurrentHashMap;

/**
 * A simple Map-based cache of proxies.
 */
public final class MapBasedClassValue extends ClassValue {
    
    public MapBasedClassValue(ClassValueCalculator calculator) {
        super(calculator);
    }

    @Override
    public T get(Class cls) {
        T obj = cache.get(cls);

        if (obj != null) return obj;

        synchronized (this) {
            obj = cache.get(cls);

            if (obj != null) return obj;

            obj = calculator.computeValue(cls);
            cache.put(cls, obj);
        }

        return obj;
    }

    // There's not a compelling reason to keep JavaClass instances in a weak map
    // (any proxies created are [were] kept in a non-weak map, so in most cases they will
    // stick around anyway), and some good reasons not to (JavaClass creation is
    // expensive, for one; many lookups are performed when passing parameters to/from
    // methods; etc.).
    // TODO: faster custom concurrent map
    private final ConcurrentHashMap,T> cache = new ConcurrentHashMap<>(128);
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy