com.github.tonivade.purefun.MemoizedProducer Maven / Gradle / Ivy
/*
* Copyright (c) 2018-2021, Antonio Gabriel Muñoz Conejo
* Distributed under the terms of the MIT License
*/
package com.github.tonivade.purefun;
import static com.github.tonivade.purefun.Precondition.checkNonNull;
import static com.github.tonivade.purefun.Unit.unit;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
final class MemoizedProducer implements Producer {
private final Map cache = new ConcurrentHashMap<>();
private final Function1 function;
MemoizedProducer(Producer extends T> producer) {
this.function = checkNonNull(producer).asFunction();
}
@Override
public T run() {
return cache.computeIfAbsent(unit(), function::apply);
}
@Override
public Producer memoized() {
return this;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy