me.xethh.libs.toolkits.cache.Cache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons Show documentation
Show all versions of commons Show documentation
Library of common used tools - major focus on common tools
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();
}