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

com.ebmwebsourcing.easycommons.thread.ConcurrentHashMapWithDefaults Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (c) 2010-2012 EBM WebSourcing, 2012-2023 Linagora
 * 
 * This program/library is free software: you can redistribute it and/or modify
 * it under the terms of the New BSD License (3-clause license).
 *
 * This program/library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the New BSD License (3-clause license)
 * for more details.
 *
 * You should have received a copy of the New BSD License (3-clause license)
 * along with this program/library; If not, see http://directory.fsf.org/wiki/License:BSD_3Clause/
 * for the New BSD License (3-clause license).
 */
package com.ebmwebsourcing.easycommons.thread;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * This class extends the usual {@link ConcurrentHashMap} behaviour by possibly
 * returning a default value from a parent map.
 * 
 * @param 
 * @param 
 * @author Marc Jambert - EBM WebSourcing
 */
public class ConcurrentHashMapWithDefaults extends ConcurrentHashMap {

    private static final long serialVersionUID = -636167196756455345L;

    private final Map defaults;

    public ConcurrentHashMapWithDefaults() {
        this(new ConcurrentHashMap());
    }

    public ConcurrentHashMapWithDefaults(Map defaults) {
        assert defaults != null;
        this.defaults = defaults;
    }

    
    public final Map getDefaults() {
        return defaults;
    }
    
    public V getValueOrDefault(Object key) {
        if (containsKey(key)) {
            return super.get(key);
        } else if (defaults instanceof ConcurrentHashMapWithDefaults) {
            return ((ConcurrentHashMapWithDefaults) defaults).getValueOrDefault(key); 
        } else {
            return defaults.get(key);
        }
    }


    
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy