Groovy web console

subscribe to the feed Subscribe
to this
site

elvis-vs-orElseGet

Published 4 months ago by trankitron
Actions Execute script  ▶ Edit in console Back to console Show/hide line numbers View recent scripts
import java.util.function.Supplier

def loadDataIfNeccesary(cachedData) {
    //cachedData?:loadData()
    Optional.ofNullable(cachedData).orElseGet({ loadData() } as Supplier)
}

def loadData() {
   sleep(5000)
   // very expensive operation
   "fresh data"
}

loadDataIfNeccesary("dataz")