All Downloads are FREE. Search and download functionalities are using the official Maven repository.

wtf.metio.memoization.jool.Function2Memoizer Maven / Gradle / Ivy

The newest version!
/*
 * SPDX-FileCopyrightText: The memoization.java Authors
 * SPDX-License-Identifier: 0BSD
 */

package wtf.metio.memoization.jool;

import org.jooq.lambda.function.Function2;
import wtf.metio.memoization.core.AbstractMemoizer;

import java.util.concurrent.ConcurrentMap;
import java.util.function.BiFunction;

import static java.util.Objects.requireNonNull;

final class Function2Memoizer
        extends AbstractMemoizer
        implements Function2 {

    private final BiFunction keyFunction;
    private final Function2 function;

    Function2Memoizer(
            final ConcurrentMap cache,
            final BiFunction keyFunction,
            final Function2 function) {
        super(cache);
        this.keyFunction = requireNonNull(keyFunction, "Provide a key function.");
        this.function = requireNonNull(function,
                "Cannot memoize a NULL Function2 - provide an actual Function2 to fix this.");
    }

    @Override
    public OUTPUT apply(final FIRST first, final SECOND second) {
        return computeIfAbsent(keyFunction.apply(first, second), key -> function.apply(first, second));
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy