com.github.tonivade.purefun.MemoizedFunction 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 java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
final class MemoizedFunction implements Function1 {
private final Map cache = new ConcurrentHashMap<>();
private final Function1 super T, ? extends R> function;
MemoizedFunction(Function1 super T, ? extends R> function) {
this.function = checkNonNull(function);
}
@Override
public R run(T value) {
return cache.computeIfAbsent(value, function::apply);
}
@Override
public Function1 memoized() {
return this;
}
} © 2015 - 2025 Weber Informatics LLC | Privacy Policy