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

me.xethh.libs.toolkits.cache.Cache Maven / Gradle / Ivy

The newest version!
package me.xethh.libs.toolkits.cache;

import java.util.function.Function;
import java.util.function.Supplier;

public interface Cache {
    default V get(K key){
       V value = getInternal(key);
       if(value!=null) return value;

       value = getValueProvider().apply(key);
       if(value!=null){
           cacheInternal(key, value);
           return value;
       }

       return ifNull().get();
    }
    default V getOrDefault(K key, Supplier valueProvider){
        V value = get(key);
        return value==null?valueProvider.get():value;
    }
    default V getOrDefault(K key, V defaultValue){
        V value = get(key);
        return value==null?defaultValue:value;
    }

    Function getValueProvider();

    V getInternal(K key);
    void cacheInternal(K key, V value);

    default Supplier ifNull(){
        return ()->null;
    }

    int size();
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy