Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,17 @@ Function<INPUT, OUTPUT> memoizedFunction = Memoize.function(function, cache.asMa

// memoize in Caffeine cache
Supplier<OUTPUT> supplier = ...;
Cache<String, OUTPUT> cache = ...; // com.github.benmanes.caffeine.cache.Cache
Cache<Integer, OUTPUT> cache = ...; // com.github.benmanes.caffeine.cache.Cache
Supplier<OUTPUT> memoizedSupplier = MemoizeRx.supplier(supplier, cache.asMap());

// memoize in Guava cache
Function3<T1, T2, T3, OUTPUT> function = ...;
Cache<String, OUTPUT> cache = ...; // com.google.common.cache.Cache
Cache<Integer, OUTPUT> cache = ...; // com.google.common.cache.Cache
Function3<T1, T2, T3, OUTPUT> memoizedFunction = MemoizeJool.function3(function, cache.asMap());

// memoize in ConcurrentMap
Fn4<T1, T2, T3, T4, OUTPUT> function = ...;
Map<String, OUTPUT> cache = ...;
Map<Integer, OUTPUT> cache = ...;
Fn4<T1, T2, T3, T4, OUTPUT> memoizedFunction = MemoizeLambda.fn4(function, cache);
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
package wtf.metio.memoization.core;

import java.util.Arrays;
import java.util.Objects;
import java.util.function.Supplier;
import java.util.stream.Collectors;

/**
* Defaults used throughout the library.
Expand All @@ -23,8 +21,8 @@ private MemoizationDefaults() {
*
* @return The default key supplier used throughout the library.
*/
public static Supplier<String> staticKey() {
return () -> "SUPPLIED";
public static Supplier<Integer> staticKey() {
return () -> 1;
}

/**
Expand All @@ -33,13 +31,8 @@ public static Supplier<String> staticKey() {
* @param values The values to use.
* @return The constructed cache key.
*/
public static String hashCodes(final Object... values) {
// return Arrays.hashCode(values);

return Arrays.stream(values)
.map(Object::hashCode)
.map(Objects::toString)
.collect(Collectors.joining(" "));
public static int hashCodes(final Object... values) {
return Arrays.hashCode(values);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class MemoizationDefaultsTest {
@Test
void shouldDefineDefaultKeySupplier() {
// given
Supplier<String> keySupplier;
Supplier<Integer> keySupplier;

// when
keySupplier = MemoizationDefaults.staticKey();
Expand All @@ -29,24 +29,14 @@ void shouldDefineDefaultKeySupplier() {
@Test
void shouldDefineDefaultKey() {
// given
final Supplier<String> keySupplier = MemoizationDefaults.staticKey();
final Supplier<Integer> keySupplier = MemoizationDefaults.staticKey();

// when
final String defaultSuppliedKey = keySupplier.get();
final Integer defaultSuppliedKey = keySupplier.get();

// then
Assertions.assertNotNull(defaultSuppliedKey);
Assertions.assertEquals("SUPPLIED", defaultSuppliedKey);
}

@Test
void shouldConcatHashCodesWithWhitespaceInbetween() {
// given
// when
final String calculatedKey = MemoizationDefaults.hashCodes(1, 2);

// when
Assertions.assertEquals("1 2", calculatedKey);
Assertions.assertEquals(1, defaultSuppliedKey);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public static <OUTPUT> Callable<OUTPUT> callable(final Callable<OUTPUT> callable
@CheckReturnValue
public static <OUTPUT> Callable<OUTPUT> callable(
final Callable<OUTPUT> callable,
final Map<String, OUTPUT> cache) {
final Map<Integer, OUTPUT> cache) {
return callable(callable, staticKey(), cache);
}

Expand Down Expand Up @@ -219,7 +219,7 @@ public static Runnable runnable(final Runnable runnable) {
@CheckReturnValue
public static Runnable runnable(
final Runnable runnable,
final Map<String, String> cache) {
final Map<Integer, Integer> cache) {
return runnable(runnable, staticKey(), cache);
}

Expand Down Expand Up @@ -332,7 +332,7 @@ public static <FIRST, SECOND, KEY> BiConsumer<FIRST, SECOND> biConsumer(
@CheckReturnValue
public static <FIRST, SECOND> BiConsumer<FIRST, SECOND> biConsumer(
final BiConsumer<FIRST, SECOND> biConsumer,
final Map<String, String> cache) {
final Map<Integer, Integer> cache) {
return biConsumer(biConsumer, MemoizationDefaults::hashCodes, cache);
}

Expand Down Expand Up @@ -429,7 +429,7 @@ public static <KEY, FIRST, SECOND, OUTPUT> BiFunction<FIRST, SECOND, OUTPUT> biF
@CheckReturnValue
public static <FIRST, SECOND, OUTPUT> BiFunction<FIRST, SECOND, OUTPUT> biFunction(
final BiFunction<FIRST, SECOND, OUTPUT> biFunction,
final Map<String, OUTPUT> cache) {
final Map<Integer, OUTPUT> cache) {
return biFunction(biFunction, MemoizationDefaults::hashCodes, cache);
}

Expand Down Expand Up @@ -523,7 +523,7 @@ public static <KEY, FIRST, SECOND> BiPredicate<FIRST, SECOND> biPredicate(
@CheckReturnValue
public static <FIRST, SECOND> BiPredicate<FIRST, SECOND> biPredicate(
final BiPredicate<FIRST, SECOND> predicate,
final Map<String, Boolean> cache) {
final Map<Integer, Boolean> cache) {
return biPredicate(predicate, MemoizationDefaults::hashCodes, cache);
}

Expand Down Expand Up @@ -588,7 +588,7 @@ public static BooleanSupplier booleanSupplier(final BooleanSupplier supplier) {
@CheckReturnValue
public static BooleanSupplier booleanSupplier(
final BooleanSupplier supplier,
final Map<String, Boolean> cache) {
final Map<Integer, Boolean> cache) {
return booleanSupplier(supplier, staticKey(), cache);
}

Expand Down Expand Up @@ -808,7 +808,7 @@ public static <KEY> DoubleBinaryOperator doubleBinaryOperator(
@CheckReturnValue
public static DoubleBinaryOperator doubleBinaryOperator(
final DoubleBinaryOperator operator,
final Map<String, Double> cache) {
final Map<Integer, Double> cache) {
return doubleBinaryOperator(operator, MemoizationDefaults::hashCodes, cache);
}

Expand Down Expand Up @@ -1106,7 +1106,7 @@ public static DoubleSupplier doubleSupplier(final DoubleSupplier supplier) {
@CheckReturnValue
public static DoubleSupplier doubleSupplier(
final DoubleSupplier supplier,
final Map<String, Double> cache) {
final Map<Integer, Double> cache) {
return doubleSupplier(supplier, staticKey(), cache);
}

Expand Down Expand Up @@ -1585,7 +1585,7 @@ public static <KEY> IntBinaryOperator intBinaryOperator(
@CheckReturnValue
public static IntBinaryOperator intBinaryOperator(
final IntBinaryOperator operator,
final Map<String, Integer> cache) {
final Map<Integer, Integer> cache) {
return intBinaryOperator(operator, MemoizationDefaults::hashCodes, cache);
}

Expand Down Expand Up @@ -1883,7 +1883,7 @@ public static IntSupplier intSupplier(final IntSupplier supplier) {
@CheckReturnValue
public static IntSupplier intSupplier(
final IntSupplier supplier,
final Map<String, Integer> cache) {
final Map<Integer, Integer> cache) {
return intSupplier(supplier, staticKey(), cache);
}

Expand Down Expand Up @@ -2269,7 +2269,7 @@ public static <KEY> LongBinaryOperator longBinaryOperator(
@CheckReturnValue
public static LongBinaryOperator longBinaryOperator(
final LongBinaryOperator operator,
final Map<String, Long> cache) {
final Map<Integer, Long> cache) {
return longBinaryOperator(operator, MemoizationDefaults::hashCodes, cache);
}

Expand Down Expand Up @@ -2567,7 +2567,7 @@ public static LongSupplier longSupplier(final LongSupplier supplier) {
@CheckReturnValue
public static LongSupplier longSupplier(
final LongSupplier supplier,
final Map<String, Long> cache) {
final Map<Integer, Long> cache) {
return longSupplier(supplier, staticKey(), cache);
}

Expand Down Expand Up @@ -2909,7 +2909,7 @@ public static <INPUT> ObjDoubleConsumer<INPUT> objDoubleConsumer(final ObjDouble
@CheckReturnValue
public static <INPUT> ObjDoubleConsumer<INPUT> objDoubleConsumer(
final ObjDoubleConsumer<INPUT> consumer,
final Map<String, String> cache) {
final Map<Integer, Integer> cache) {
return objDoubleConsumer(consumer, MemoizationDefaults::hashCodes, cache);
}

Expand Down Expand Up @@ -2998,7 +2998,7 @@ public static <INPUT> ObjIntConsumer<INPUT> objIntConsumer(final ObjIntConsumer<
@CheckReturnValue
public static <INPUT> ObjIntConsumer<INPUT> objIntConsumer(
final ObjIntConsumer<INPUT> consumer,
final Map<String, String> cache) {
final Map<Integer, Integer> cache) {
return objIntConsumer(consumer, MemoizationDefaults::hashCodes, cache);
}

Expand Down Expand Up @@ -3087,7 +3087,7 @@ public static <INPUT> ObjLongConsumer<INPUT> objLongConsumer(final ObjLongConsum
@CheckReturnValue
public static <INPUT> ObjLongConsumer<INPUT> objLongConsumer(
final ObjLongConsumer<INPUT> consumer,
final Map<String, String> cache) {
final Map<Integer, Integer> cache) {
return objLongConsumer(consumer, MemoizationDefaults::hashCodes, cache);
}

Expand Down Expand Up @@ -3265,7 +3265,7 @@ public static <OUTPUT> Supplier<OUTPUT> supplier(final Supplier<OUTPUT> supplier
@CheckReturnValue
public static <OUTPUT> Supplier<OUTPUT> supplier(
final Supplier<OUTPUT> supplier,
final Map<String, OUTPUT> cache) {
final Map<Integer, OUTPUT> cache) {
return supplier(supplier, staticKey(), asConcurrentMap(cache));
}

Expand Down Expand Up @@ -3407,7 +3407,7 @@ public static <FIRST, SECOND, KEY> ToDoubleBiFunction<FIRST, SECOND> toDoubleBiF
@CheckReturnValue
public static <FIRST, SECOND> ToDoubleBiFunction<FIRST, SECOND> toDoubleBiFunction(
final ToDoubleBiFunction<FIRST, SECOND> function,
final Map<String, Double> cache) {
final Map<Integer, Double> cache) {
return toDoubleBiFunction(function, MemoizationDefaults::hashCodes, cache);
}

Expand Down Expand Up @@ -3590,7 +3590,7 @@ public static <FIRST, SECOND, KEY> ToIntBiFunction<FIRST, SECOND> toIntBiFunctio
@CheckReturnValue
public static <FIRST, SECOND> ToIntBiFunction<FIRST, SECOND> toIntBiFunction(
final ToIntBiFunction<FIRST, SECOND> function,
final Map<String, Integer> cache) {
final Map<Integer, Integer> cache) {
return toIntBiFunction(function, MemoizationDefaults::hashCodes, cache);
}

Expand Down Expand Up @@ -3773,7 +3773,7 @@ public static <FIRST, SECOND, KEY> ToLongBiFunction<FIRST, SECOND> toLongBiFunct
@CheckReturnValue
public static <FIRST, SECOND> ToLongBiFunction<FIRST, SECOND> toLongBiFunction(
final ToLongBiFunction<FIRST, SECOND> function,
final Map<String, Long> cache) {
final Map<Integer, Long> cache) {
return toLongBiFunction(function, MemoizationDefaults::hashCodes, cache);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class Consumer0Memoizer<KEY>
final Consumer0 consumer) {
super(cache);
this.keySupplier = requireNonNull(keySupplier,
"Provide a key supplier, might just be 'MemoizationDefaults.defaultKeySupplier()'.");
"Provide a key supplier, might just be 'MemoizationDefaults.staticKey()'.");
this.consumer = requireNonNull(consumer,
"Cannot memoize a NULL Consumer0 - provide an actual Consumer0 to fix this.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class Consumer1Memoizer<KEY, TYPE1>
final Consumer1<TYPE1> consumer) {
super(cache);
this.keyFunction = requireNonNull(keyFunction,
"Provide a key function, might just be 'MemoizationDefaults.defaultKeySupplier()'.");
"Provide a key function, might just be 'MemoizationDefaults.staticKey()'.");
this.consumer = requireNonNull(consumer,
"Cannot memoize a NULL Consumer1 - provide an actual Consumer1 to fix this.");
}
Expand Down
Loading