From 0ed886be6926c49c0cda008255135be9036bfe10 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sebastian=20Ho=C3=9F?= Date: Sun, 31 Jul 2016 17:44:24 +0200 Subject: [PATCH] fix #151 MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Sebastian Hoß --- README.asciidoc | 2 +- .../jcache/JCacheBasedObjIntConsumerMemoizer.java | 40 +++++++++ .../memoization/jcache/JCacheMemoize.java | 92 +++++++++++++++++++++ .../JCacheBasedObjIntConsumerMemoizerTest.java | 96 ++++++++++++++++++++++ .../jcache/JCacheMemoizeCustomKeyTest.java | 18 ++++ .../jcache/JCacheMemoizeDefaultsTest.java | 16 ++++ .../jcache/JCacheMemoizeLambdaTest.java | 15 ++++ 7 files changed, 278 insertions(+), 1 deletion(-) create mode 100644 memoization-jcache/src/main/java/de/xn__ho_hia/memoization/jcache/JCacheBasedObjIntConsumerMemoizer.java create mode 100644 memoization-jcache/src/test/java/de/xn__ho_hia/memoization/jcache/JCacheBasedObjIntConsumerMemoizerTest.java diff --git a/README.asciidoc b/README.asciidoc index e672674..69db13b 100755 --- a/README.asciidoc +++ b/README.asciidoc @@ -224,7 +224,7 @@ _Java link:https://en.wikipedia.org/wiki/Memoization[memoization] library - trad | link:{jdk-api}/java/util/function/ObjIntConsumer.html[ObjIntConsumer] | ✓ | ✓ -| link:{issue}/151[#151] +| ✓ | ✓ | link:{jdk-api}/java/util/function/ObjLongConsumer.html[ObjLongConsumer] diff --git a/memoization-jcache/src/main/java/de/xn__ho_hia/memoization/jcache/JCacheBasedObjIntConsumerMemoizer.java b/memoization-jcache/src/main/java/de/xn__ho_hia/memoization/jcache/JCacheBasedObjIntConsumerMemoizer.java new file mode 100644 index 0000000..7e63882 --- /dev/null +++ b/memoization-jcache/src/main/java/de/xn__ho_hia/memoization/jcache/JCacheBasedObjIntConsumerMemoizer.java @@ -0,0 +1,40 @@ +/* + * This file is part of memoization.java. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://creativecommons.org/publicdomain/zero/1.0/. No part of memoization.java, + * including this file, may be copied, modified, propagated, or distributed except according to the terms contained + * in the LICENSE file. + */ +package de.xn__ho_hia.memoization.jcache; + +import java.util.function.ObjIntConsumer; + +import javax.cache.Cache; + +import de.xn__ho_hia.memoization.shared.ObjIntFunction; + +final class JCacheBasedObjIntConsumerMemoizer + extends AbstractJCacheBasedMemoizer + implements ObjIntConsumer { + + private final ObjIntFunction keyFunction; + private final ObjIntConsumer biConsumer; + + JCacheBasedObjIntConsumerMemoizer( + final Cache cache, + final ObjIntFunction keyFunction, + final ObjIntConsumer biConsumer) { + super(cache); + this.keyFunction = keyFunction; + this.biConsumer = biConsumer; + } + + @Override + public void accept(final FIRST first, final int value) { + final KEY key = keyFunction.apply(first, value); + invoke(key, givenKey -> { + biConsumer.accept(first, value); + return givenKey; + }); + } + +} diff --git a/memoization-jcache/src/main/java/de/xn__ho_hia/memoization/jcache/JCacheMemoize.java b/memoization-jcache/src/main/java/de/xn__ho_hia/memoization/jcache/JCacheMemoize.java index a776b13..44dca20 100755 --- a/memoization-jcache/src/main/java/de/xn__ho_hia/memoization/jcache/JCacheMemoize.java +++ b/memoization-jcache/src/main/java/de/xn__ho_hia/memoization/jcache/JCacheMemoize.java @@ -12,6 +12,7 @@ import static de.xn__ho_hia.memoization.shared.MemoizationDefaults.hashCodeKeyFu import static de.xn__ho_hia.memoization.shared.MemoizationDefaults.intBinaryOperatorHashCodeKeyFunction; import static de.xn__ho_hia.memoization.shared.MemoizationDefaults.longBinaryOperatorHashCodeKeyFunction; import static de.xn__ho_hia.memoization.shared.MemoizationDefaults.objDoubleConsumerHashCodeKeyFunction; +import static de.xn__ho_hia.memoization.shared.MemoizationDefaults.objIntConsumerHashCodeKeyFunction; import java.lang.reflect.Type; import java.util.concurrent.atomic.AtomicLong; @@ -37,6 +38,7 @@ import java.util.function.LongFunction; import java.util.function.LongPredicate; import java.util.function.LongSupplier; import java.util.function.ObjDoubleConsumer; +import java.util.function.ObjIntConsumer; import java.util.function.Predicate; import java.util.function.Supplier; @@ -50,6 +52,7 @@ import de.xn__ho_hia.memoization.shared.IntBinaryFunction; import de.xn__ho_hia.memoization.shared.LongBinaryFunction; import de.xn__ho_hia.memoization.shared.MemoizationDefaults; import de.xn__ho_hia.memoization.shared.ObjDoubleFunction; +import de.xn__ho_hia.memoization.shared.ObjIntFunction; /** *

@@ -98,6 +101,7 @@ import de.xn__ho_hia.memoization.shared.ObjDoubleFunction; * @see LongPredicate * @see LongSupplier * @see ObjDoubleConsumer + * @see ObjIntConsumer * @see Predicate * @see Supplier * @see Wikipedia: Memoization @@ -2035,6 +2039,94 @@ public final class JCacheMemoize { /** *

+ * Memoizes a {@link ObjIntConsumer} in a JCache {@link Cache}. + *

+ *

Features

+ *
    + *
  • Default cache
  • + *
  • Default cache key
  • + *
+ * + * @param objIntConsumer + * The {@link ObjIntConsumer} to memoize. + * @return The wrapped {@link ObjIntConsumer}. + */ + public static final ObjIntConsumer objIntConsumer( + final ObjIntConsumer objIntConsumer) { + return objIntConsumer(objIntConsumer, createCache(ObjIntConsumer.class)); + } + + /** + *

+ * Memoizes a {@link ObjIntConsumer} in a JCache {@link Cache}. + *

+ *

Features

+ *
    + *
  • Default cache
  • + *
  • Custom cache key
  • + *
+ * + * @param objIntConsumer + * The {@link ObjIntConsumer} to memoize. + * @param keyFunction + * The {@link ObjIntFunction} to compute the cache key. + * @return The wrapped {@link ObjIntConsumer}. + */ + public static final ObjIntConsumer objIntConsumer( + final ObjIntConsumer objIntConsumer, + final ObjIntFunction keyFunction) { + return objIntConsumer(objIntConsumer, keyFunction, createCache(ObjIntConsumer.class)); + } + + /** + *

+ * Memoizes a {@link ObjIntConsumer} in a JCache {@link Cache}. + *

+ *

Features

+ *
    + *
  • Custom cache
  • + *
  • Custom cache key
  • + *
+ * + * @param objIntConsumer + * The {@link ObjIntConsumer} to memoize. + * @param keyFunction + * The {@link ObjIntFunction} to compute the cache key. + * @param cache + * The {@link Cache} to use. + * @return The wrapped {@link ObjIntConsumer}. + */ + public static final ObjIntConsumer objIntConsumer( + final ObjIntConsumer objIntConsumer, + final ObjIntFunction keyFunction, + final Cache cache) { + return new JCacheBasedObjIntConsumerMemoizer<>(cache, keyFunction, objIntConsumer); + } + + /** + *

+ * Memoizes a {@link ObjIntConsumer} in a JCache {@link Cache}. + *

+ *

Features

+ *
    + *
  • Custom cache
  • + *
  • Default cache key
  • + *
+ * + * @param objIntConsumer + * The {@link ObjIntConsumer} to memoize. + * @param cache + * The {@link Cache} to use. + * @return The wrapped {@link ObjIntConsumer}. + */ + public static final ObjIntConsumer objIntConsumer( + final ObjIntConsumer objIntConsumer, + final Cache cache) { + return objIntConsumer(objIntConsumer, objIntConsumerHashCodeKeyFunction(), cache); + } + + /** + *

* Memoizes a {@link Predicate} in a JCache {@link Cache}. *

*

Features

diff --git a/memoization-jcache/src/test/java/de/xn__ho_hia/memoization/jcache/JCacheBasedObjIntConsumerMemoizerTest.java b/memoization-jcache/src/test/java/de/xn__ho_hia/memoization/jcache/JCacheBasedObjIntConsumerMemoizerTest.java new file mode 100644 index 0000000..8eee70c --- /dev/null +++ b/memoization-jcache/src/test/java/de/xn__ho_hia/memoization/jcache/JCacheBasedObjIntConsumerMemoizerTest.java @@ -0,0 +1,96 @@ +/* + * This file is part of memoization.java. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://creativecommons.org/publicdomain/zero/1.0/. No part of memoization.java, + * including this file, may be copied, modified, propagated, or distributed except according to the terms contained + * in the LICENSE file. + */ +package de.xn__ho_hia.memoization.jcache; + +import static org.mockito.BDDMockito.given; +import static org.mockito.Matchers.any; + +import java.util.function.BiConsumer; +import java.util.function.ObjIntConsumer; + +import javax.cache.Cache; + +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.mockito.Mockito; + +import de.xn__ho_hia.memoization.shared.MemoizationException; +import de.xn__ho_hia.memoization.shared.ObjIntFunction; +import de.xn__ho_hia.quality.suppression.CompilerWarnings; + +/** + * + */ +@SuppressWarnings({ CompilerWarnings.NLS, CompilerWarnings.STATIC_METHOD }) +public class JCacheBasedObjIntConsumerMemoizerTest { + + /** Captures expected exceptions. */ + @Rule + public ExpectedException thrown = ExpectedException.none(); + + /** + * + */ + @Test + public void shouldMemoizeBiConsumer() { + // given + final ObjIntConsumer biConsumer = (first, second) -> System.out.println(first + second); + final ObjIntFunction keyfunction = (a, b) -> "key"; + try (final Cache cache = JCacheMemoize.createCache(BiConsumer.class)) { + // when + final JCacheBasedObjIntConsumerMemoizer memoizer = new JCacheBasedObjIntConsumerMemoizer<>( + cache, keyfunction, biConsumer); + + // then + Assert.assertNotNull("Memoizer is NULL", memoizer); + } + } + + /** + * + */ + @Test + @SuppressWarnings(CompilerWarnings.UNCHECKED) + public void shouldAcceptInput() { + // given + final ObjIntConsumer biConsumer = Mockito.mock(ObjIntConsumer.class); + final ObjIntFunction keyfunction = (a, b) -> "key"; + try (final Cache cache = JCacheMemoize.createCache(BiConsumer.class)) { + // when + final JCacheBasedObjIntConsumerMemoizer memoizer = new JCacheBasedObjIntConsumerMemoizer<>( + cache, keyfunction, biConsumer); + + // then + memoizer.accept("first", 123); + Mockito.verify(biConsumer).accept("first", 123); + } + } + + /** + * + */ + @Test + @SuppressWarnings(CompilerWarnings.UNCHECKED) + public void shouldWrapRuntimeExceptionInMemoizationException() { + // given + final ObjIntFunction keyfunction = (a, b) -> "key"; + try (final Cache cache = Mockito.mock(Cache.class)) { + final JCacheBasedObjIntConsumerMemoizer loader = new JCacheBasedObjIntConsumerMemoizer<>( + cache, keyfunction, null); + given(cache.invoke(any(), any())).willThrow(RuntimeException.class); + + // when + thrown.expect(MemoizationException.class); + + // then + loader.accept("first", 123); + } + } + +} diff --git a/memoization-jcache/src/test/java/de/xn__ho_hia/memoization/jcache/JCacheMemoizeCustomKeyTest.java b/memoization-jcache/src/test/java/de/xn__ho_hia/memoization/jcache/JCacheMemoizeCustomKeyTest.java index f250a3f..6a3ba8e 100644 --- a/memoization-jcache/src/test/java/de/xn__ho_hia/memoization/jcache/JCacheMemoizeCustomKeyTest.java +++ b/memoization-jcache/src/test/java/de/xn__ho_hia/memoization/jcache/JCacheMemoizeCustomKeyTest.java @@ -28,6 +28,7 @@ import java.util.function.LongFunction; import java.util.function.LongPredicate; import java.util.function.LongSupplier; import java.util.function.ObjDoubleConsumer; +import java.util.function.ObjIntConsumer; import java.util.function.Predicate; import java.util.function.Supplier; @@ -38,6 +39,7 @@ import de.xn__ho_hia.memoization.shared.DoubleBinaryFunction; import de.xn__ho_hia.memoization.shared.IntBinaryFunction; import de.xn__ho_hia.memoization.shared.LongBinaryFunction; import de.xn__ho_hia.memoization.shared.ObjDoubleFunction; +import de.xn__ho_hia.memoization.shared.ObjIntFunction; import de.xn__ho_hia.quality.suppression.CompilerWarnings; /** @@ -402,6 +404,22 @@ public class JCacheMemoizeCustomKeyTest { * */ @Test + public void shouldMemoizeObjIntConsumerWithKeyFunction() { + // given + final ObjIntConsumer consumer = (a, b) -> System.out.println(a + b); + final ObjIntFunction keyFunction = (a, b) -> "key"; + + // when + final ObjIntConsumer memoize = JCacheMemoize.objIntConsumer(consumer, keyFunction); + + // then + Assert.assertNotNull("Memoized ObjIntConsumer is NULL", memoize); + } + + /** + * + */ + @Test public void shouldMemoizePredicateWithKeyFunction() { // given final Predicate predicate = a -> true; diff --git a/memoization-jcache/src/test/java/de/xn__ho_hia/memoization/jcache/JCacheMemoizeDefaultsTest.java b/memoization-jcache/src/test/java/de/xn__ho_hia/memoization/jcache/JCacheMemoizeDefaultsTest.java index abb2592..2382dd0 100644 --- a/memoization-jcache/src/test/java/de/xn__ho_hia/memoization/jcache/JCacheMemoizeDefaultsTest.java +++ b/memoization-jcache/src/test/java/de/xn__ho_hia/memoization/jcache/JCacheMemoizeDefaultsTest.java @@ -28,6 +28,7 @@ import java.util.function.LongFunction; import java.util.function.LongPredicate; import java.util.function.LongSupplier; import java.util.function.ObjDoubleConsumer; +import java.util.function.ObjIntConsumer; import java.util.function.Predicate; import java.util.function.Supplier; @@ -376,6 +377,21 @@ public class JCacheMemoizeDefaultsTest { * */ @Test + public void shouldMemoizeObjIntConsumer() { + // given + final ObjIntConsumer consumer = (a, b) -> System.out.println(a + b); + + // when + final ObjIntConsumer memoize = JCacheMemoize.objIntConsumer(consumer); + + // then + Assert.assertNotNull("Memoized ObjIntConsumer is NULL", memoize); + } + + /** + * + */ + @Test public void shouldMemoizePredicate() { // given final Predicate predicate = a -> true; diff --git a/memoization-jcache/src/test/java/de/xn__ho_hia/memoization/jcache/JCacheMemoizeLambdaTest.java b/memoization-jcache/src/test/java/de/xn__ho_hia/memoization/jcache/JCacheMemoizeLambdaTest.java index ea4fd46..6340366 100644 --- a/memoization-jcache/src/test/java/de/xn__ho_hia/memoization/jcache/JCacheMemoizeLambdaTest.java +++ b/memoization-jcache/src/test/java/de/xn__ho_hia/memoization/jcache/JCacheMemoizeLambdaTest.java @@ -28,6 +28,7 @@ import java.util.function.LongFunction; import java.util.function.LongPredicate; import java.util.function.LongSupplier; import java.util.function.ObjDoubleConsumer; +import java.util.function.ObjIntConsumer; import java.util.function.Predicate; import java.util.function.Supplier; @@ -354,6 +355,20 @@ public class JCacheMemoizeLambdaTest { * */ @Test + public void shouldMemoizeObjIntConsumerWithLambda() { + // given + + // when + final ObjIntConsumer memoize = JCacheMemoize.objIntConsumer((a, b) -> System.out.println(a + b)); + + // then + Assert.assertNotNull("Memoized ObjIntConsumer is NULL", memoize); + } + + /** + * + */ + @Test public void shouldMemoizePredicateWithLambda() { // given -- 2.11.4.GIT