I can't seem to think in java. What I want is a single function that takes two functions as arguments, and executes one of them, checks for a cache miss, and executes the second one if there was. I can't figure out how to do this in java without rewriting all the function definitions in subclasses/interface implementations.
so, how would I write something like
def(cacheFun, diskFun){
String astring = cacheFun.call()
if(!isBlank(astring)){
return astring
} else {
return diskFun.call()
}
}
in java?
What I don't want is a bunch of anonymous classes that implement the function that I want under an interface (at least not in the way that I'm thinking about it). There are a lot of functions that handle the cache in different ways. This means that the superclass would have to have a function for each way of caching.
public String getUsingWayOne(){
String retval = WayOneCacheClass.new.execute()
if(retval is not empty){
return retval
} else {
retval = WayOneDiskClass.new.execute()
return retval
}
}
and repeat this for all the different ways there are of caching
Is there a solved way of doing this in java? Please do not suggest caching libraries. The purpose of this question isn't to actually cache things, but to learn to write code the "right" (java) way.