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

org.faktorips.runtime.SimpleCache Maven / Gradle / Ivy

Go to download

Runtime library for Faktor-IPS. When using the JAXB support use either faktorips-runtime-jakarta-xml or faktorips-runtime-javax-xml as dependency. When using CSV to read tables, add the optional dependencies to opencsv, commons-lang3 and commons-text. If you want to run Faktor-IPS tests as JUnit tests, you need to provide either junit (JUnit 4) or junit-jupiter-api (JUnit 5).

There is a newer version: 25.1.0.a20241030-01
Show newest version
/*******************************************************************************
 * Copyright (c) Faktor Zehn GmbH - faktorzehn.org
 * 
 * This source code is available under the terms of the AGPL Affero General Public License version
 * 3.
 * 
 * Please see LICENSE.txt for full license terms, including the additional permissions and
 * restrictions as well as the possibility of alternative license terms.
 *******************************************************************************/

package org.faktorips.runtime;

import java.util.concurrent.ConcurrentHashMap;

import org.faktorips.runtime.caching.IComputable;

/**
 * Simple cache that just uses a HashMap to cache objects and never releases them. This Cache is
 * thread safe but not very high-performance.
 * 
 * @author Jan Ortmann
 */
public class SimpleCache implements IComputable {

    private final ConcurrentHashMap objects;
    private final IComputable computable;

    public SimpleCache(IComputable computable) {
        this(computable, 16);
    }

    /**
     * 
     */
    public SimpleCache(IComputable computable, int initialCapacity) {
        this.computable = computable;
        objects = new ConcurrentHashMap<>(initialCapacity);
    }

    @Override
    public Object compute(Object key) throws InterruptedException {
        Object result = objects.get(key);
        if (result != null) {
            return result;
        }
        synchronized (this) {
            result = objects.get(key);
            if (result != null) {
                return result;
            }
            result = computable.compute(key);
            if (result != null) {
                objects.put(key, result);
                return result;
            }
            return null;
        }
    }

    @Override
    public Class getValueClass() {
        return computable.getValueClass();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy