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

com.netflix.hystrix.util.InternMap Maven / Gradle / Ivy

There is a newer version: 1.5.18
Show newest version
package com.netflix.hystrix.util;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * Utility to have 'intern' - like functionality, which holds single instance of wrapper for a given key
 */
public class InternMap {
    private final ConcurrentMap storage = new ConcurrentHashMap();
    private final ValueConstructor valueConstructor;

    public interface ValueConstructor {
        V create(K key);
    }

    public InternMap(ValueConstructor valueConstructor) {
        this.valueConstructor = valueConstructor;
    }

    public V interned(K key) {
        V existingKey = storage.get(key);
        V newKey = null;
        if (existingKey == null) {
            newKey = valueConstructor.create(key);
            existingKey = storage.putIfAbsent(key, newKey);
        }
        return existingKey != null ? existingKey : newKey;
    }

    public int size() {
        return storage.size();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy