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

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

The newest version!
package org.jruby.util.collections;

import java.util.concurrent.ConcurrentHashMap;
import org.jruby.Ruby;

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

    public T get(Class cls) {
        T obj = cache.get(cls);
        
        if (obj == null) {
            T newObj = calculator.computeValue(cls);
            obj = cache.putIfAbsent(cls, newObj);
            if (obj == null) {
                obj = newObj;
            }
        }
        
        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 cache =
        new ConcurrentHashMap(128);
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy