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

ru.progrm_jarvis.javacommons.cache.Cache Maven / Gradle / Ivy

package ru.progrm_jarvis.javacommons.cache;

import lombok.NonNull;
import org.jetbrains.annotations.NotNull;

import java.util.function.Function;

/**
 * Simple cache interface.
 * {@link #never() No-op implementation} (the one which never performs any caching) is considered correct.
 *
 * @param  type of cache keys
 * @param  type of cached values
 *
 * @apiNote currently java-commons does not implement its own functional cache,
 * it simply provides a universal interface and wrappers for known implementations
 */
@FunctionalInterface
public interface Cache {

    /**
     * Gets the value from the cache computing it on demand using the provided function.
     *
     * @param key key by which ti get the value from cache
     * @param mappingFunction function used to create the value if there is no cached one
     * @return the current value associated with the specified key
     *
     * @throws NullPointerException if {@code key} is {@code null}
     * @throws NullPointerException if {@code mappingFunction} is {@code null}
     */
    V get(@NonNull K key, @NonNull Function mappingFunction);

    /**
     * Creates a cache which actually never performs caching.
     *
     * @param  type of cache keys
     * @param  type of cached values
     * @return cache which actually never performs caching
     */
    static @NotNull  Cache never() {
        return (key, mappingFunction) -> mappingFunction.apply(key);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy