fix #64
[memoization.java.git] / memoization-guava / src / main / java / de / xn__ho_hia / memoization / guava / GuavaCacheBasedBiConsumerMemoizer.java
blob0b905268ccd0832f4ee42b19ebcece885ba3ae95
1 /*
2 * This file is part of memoization.java. It is subject to the license terms in the LICENSE file found in the top-level
3 * directory of this distribution and at http://creativecommons.org/publicdomain/zero/1.0/. No part of memoization.java,
4 * including this file, may be copied, modified, propagated, or distributed except according to the terms contained
5 * in the LICENSE file.
6 */
7 package de.xn__ho_hia.memoization.guava;
9 import java.util.function.BiConsumer;
10 import java.util.function.BiFunction;
12 import com.google.common.cache.Cache;
14 final class GuavaCacheBasedBiConsumerMemoizer<FIRST, SECOND, KEY>
15 extends AbstractGuavaCacheBasedMemoizer<KEY, KEY>
16 implements BiConsumer<FIRST, SECOND> {
18 private final BiFunction<FIRST, SECOND, KEY> keyFunction;
19 private final BiConsumer<FIRST, SECOND> biConsumer;
21 GuavaCacheBasedBiConsumerMemoizer(
22 final Cache<KEY, KEY> cache,
23 final BiFunction<FIRST, SECOND, KEY> keyFunction,
24 final BiConsumer<FIRST, SECOND> biConsumer) {
25 super(cache);
26 this.keyFunction = keyFunction;
27 this.biConsumer = biConsumer;
30 @Override
31 public void accept(final FIRST first, final SECOND second) {
32 final KEY key = keyFunction.apply(first, second);
33 get(key, givenKey -> {
34 biConsumer.accept(first, second);
35 return givenKey;
36 });