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

wtf.metio.memoization.jool.Consumer2Memoizer 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.Consumer2;
import wtf.metio.memoization.core.AbstractMemoizer;

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

import static java.util.Objects.requireNonNull;

final class Consumer2Memoizer
        extends AbstractMemoizer
        implements Consumer2 {

    private final BiFunction keyFunction;
    private final Consumer2 consumer;

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

    @Override
    public void accept(final TYPE1 v1, final TYPE2 v2) {
        computeIfAbsent(keyFunction.apply(v1, v2), key -> {
            consumer.accept(v1, v2);
            return key;
        });
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy