Skip to content

Commit 9a1dfa0

Browse files
Sebastian Hoßsebhoss
authored andcommitted
support RxJava
1 parent 083dadb commit 9a1dfa0

File tree

66 files changed

+4646
-160
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+4646
-160
lines changed

README.md

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Java [memoization](https://en.wikipedia.org/wiki/Memoization) library - trade sp
1212
* Memoize calls to JDK interfaces like `Consumer`, `Function`, `Predicate`, `Supplier`, and more
1313
* Memoize calls to [jOOL](https://github.com/jOOQ/jOOL) interfaces like `Consumer0..16` and `Function0..16`
1414
* Memoize calls to [lambda](https://github.com/palatable/lambda) interfaces like `Fn0..8`
15+
* Memoize calls to [RxJava](https://github.com/ReactiveX/RxJava) interfaces like `Action`, `Cancellable`, and more
1516
* Use custom caches like [Caffeine](https://github.com/ben-manes/caffeine), [Guava](https://github.com/google/guava/wiki/CachesExplained), [cache2k](https://cache2k.org/), or any other [`ConcurrentMap`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/concurrent/ConcurrentMap.html).
1617
* Use custom cache keys for fine-tuning
1718

@@ -22,19 +23,21 @@ Memoize any of the supported types by using the static factory methods supplied
2223
* `Memoize` if you want to memoize JDK interfaces.
2324
* `MemoizeJool` if you want to memoize jOOL interfaces.
2425
* `MemoizeLambda` if you want to memoize lambda interfaces.
26+
* `MemoizeRx` if you want to memoize RxJava interfaces.
2527

2628
### Default cache with default cache keys
2729

2830
```java
2931
wtf.metio.memoization.jdk.Memoize;
3032
wtf.metio.memoization.jool.MemoizeJool;
3133
wtf.metio.memoization.jool.MemoizeLambda;
34+
wtf.metio.memoization.rxjava.MemoizeRx;
3235

3336
Function<INPUT, OUTPUT> function = ...;
3437
Function<INPUT, OUTPUT> memoizedFunction = Memoize.function(function);
3538

3639
Supplier<OUTPUT> supplier = ...;
37-
Supplier<OUTPUT> memoizedSupplier = Memoize.supplier(supplier);
40+
Supplier<OUTPUT> memoizedSupplier = MemoizeRx.supplier(supplier);
3841

3942
Function3<T1, T2, T3, OUTPUT> function = ...;
4043
Function3<T1, T2, T3, OUTPUT> memoizedFunction = MemoizeJool.function3(function);
@@ -49,14 +52,15 @@ Fn4<T1, T2, T3, T4, OUTPUT> memoizedFunction = MemoizeLambda.fn4(function);
4952
wtf.metio.memoization.jdk.Memoize;
5053
wtf.metio.memoization.jool.MemoizeJool;
5154
wtf.metio.memoization.jool.MemoizeLambda;
55+
wtf.metio.memoization.rxjava.MemoizeRx;
5256

5357
Function<INPUT, OUTPUT> function = ...;
5458
Function<INPUT, KEY> keyFunction = ...;
5559
Function<INPUT, OUTPUT> memoizedFunction = Memoize.function(function, keyFunction);
5660

5761
Supplier<OUTPUT> supplier = ...;
5862
Supplier<KEY> keySupplier = ...;
59-
Supplier<OUTPUT> memoizedSupplier = Memoize.supplier(supplier, keySupplier);
63+
Supplier<OUTPUT> memoizedSupplier = MemoizeRx.supplier(supplier, keySupplier);
6064

6165
Function3<T1, T2, T3, OUTPUT> function = ...;
6266
Function3<T1, T2, T3, KEY> keyFunction = ...;
@@ -73,6 +77,7 @@ Fn4<T1, T2, T3, T4, OUTPUT> memoizedFunction = MemoizeLambda.fn4(function, keyFu
7377
wtf.metio.memoization.jdk.Memoize;
7478
wtf.metio.memoization.jool.MemoizeJool;
7579
wtf.metio.memoization.jool.MemoizeLambda;
80+
wtf.metio.memoization.rxjava.MemoizeRx;
7681

7782
// memoize in cache2k cache
7883
Function<INPUT, OUTPUT> function = ...;
@@ -82,7 +87,7 @@ Function<INPUT, OUTPUT> memoizedFunction = Memoize.function(function, cache.asMa
8287
// memoize in Caffeine cache
8388
Supplier<OUTPUT> supplier = ...;
8489
Cache<String, OUTPUT> cache = ...; // com.github.benmanes.caffeine.cache.Cache
85-
Supplier<OUTPUT> memoizedSupplier = Memoize.supplier(supplier, cache.asMap());
90+
Supplier<OUTPUT> memoizedSupplier = MemoizeRx.supplier(supplier, cache.asMap());
8691

8792
// memoize in Guava cache
8893
Function3<T1, T2, T3, OUTPUT> function = ...;
@@ -101,6 +106,7 @@ Fn4<T1, T2, T3, T4, OUTPUT> memoizedFunction = MemoizeLambda.fn4(function, cache
101106
wtf.metio.memoization.jdk.Memoize;
102107
wtf.metio.memoization.jool.MemoizeJool;
103108
wtf.metio.memoization.jool.MemoizeLambda;
109+
wtf.metio.memoization.rxjava.MemoizeRx;
104110

105111
// memoize in cache2k cache
106112
Function<INPUT, OUTPUT> function = ...;
@@ -112,7 +118,7 @@ Function<INPUT, OUTPUT> memoizedFunction = Memoize.function(function, keyFunctio
112118
Supplier<OUTPUT> supplier = ...;
113119
Supplier<KEY> keySupplier = ...;
114120
Cache<KEY, OUTPUT> cache = ...; // com.github.benmanes.caffeine.cache.Cache
115-
Supplier<OUTPUT> memoizedSupplier = Memoize.supplier(supplier, keySupplier, cache.asMap());
121+
Supplier<OUTPUT> memoizedSupplier = MemoizeRx.supplier(supplier, keySupplier, cache.asMap());
116122

117123
// memoize in Guava cache
118124
Function3<T1, T2, T3, OUTPUT> function = ...;
@@ -158,6 +164,14 @@ In order to use this project, declare the following dependencies in your project
158164
<version>${version.memoization}</version>
159165
</dependency>
160166
<!-- support for lambda interfaces -->
167+
168+
<!-- support for RxJava interfaces -->
169+
<dependency>
170+
<groupId>wtf.metio.memoization</groupId>
171+
<artifactId>memoization-rxjava</artifactId>
172+
<version>${version.memoization}</version>
173+
</dependency>
174+
<!-- support for RxJava interfaces -->
161175
</dependencies>
162176
```
163177

memoization-core/src/main/java/wtf/metio/memoization/core/MemoizationDefaults.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public static Supplier<String> staticKey() {
3434
* @return The constructed cache key.
3535
*/
3636
public static String hashCodes(final Object... values) {
37+
// return Arrays.hashCode(values);
38+
3739
return Arrays.stream(values)
3840
.map(Object::hashCode)
3941
.map(Objects::toString)

memoization-jdk/src/main/java/wtf/metio/memoization/jdk/BiConsumerMemoizer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ final class BiConsumerMemoizer<FIRST, SECOND, KEY>
1919
private final BiFunction<FIRST, SECOND, KEY> keyFunction;
2020
private final BiConsumer<FIRST, SECOND> biConsumer;
2121

22-
public BiConsumerMemoizer(
22+
BiConsumerMemoizer(
2323
final ConcurrentMap<KEY, KEY> cache,
2424
final BiFunction<FIRST, SECOND, KEY> keyFunction,
2525
final BiConsumer<FIRST, SECOND> biConsumer) {

memoization-jdk/src/main/java/wtf/metio/memoization/jdk/BiPredicateMemoizer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ final class BiPredicateMemoizer<FIRST, SECOND, KEY>
1919
private final BiFunction<FIRST, SECOND, KEY> keyFunction;
2020
private final BiPredicate<FIRST, SECOND> biPredicate;
2121

22-
public BiPredicateMemoizer(
22+
BiPredicateMemoizer(
2323
final ConcurrentMap<KEY, Boolean> cache,
2424
final BiFunction<FIRST, SECOND, KEY> keyFunction,
2525
final BiPredicate<FIRST, SECOND> biPredicate) {

memoization-jdk/src/main/java/wtf/metio/memoization/jdk/BooleanSupplierMemoizer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ final class BooleanSupplierMemoizer<KEY>
2525
final BooleanSupplier supplier) {
2626
super(cache);
2727
this.keySupplier = requireNonNull(keySupplier,
28-
"Provide a key function, might just be 'MemoizationDefaults.staticKey()'.");
28+
"Provide a key supplier, might just be 'MemoizationDefaults.staticKey()'.");
2929
this.supplier = requireNonNull(supplier,
30-
"Cannot memoize a NULL Supplier - provide an actual Supplier to fix this.");
30+
"Cannot memoize a NULL BooleanSupplier - provide an actual BooleanSupplier to fix this.");
3131
}
3232

3333
@Override

memoization-jdk/src/main/java/wtf/metio/memoization/jdk/CallableMemoizer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,21 @@ final class CallableMemoizer<OUTPUT, KEY>
2121
private final Supplier<KEY> keySupplier;
2222
private final Callable<OUTPUT> callable;
2323

24-
public CallableMemoizer(
24+
CallableMemoizer(
2525
final ConcurrentMap<KEY, OUTPUT> cache,
2626
final Supplier<KEY> keySupplier,
2727
final Callable<OUTPUT> callable) {
2828
super(cache);
2929
this.keySupplier = requireNonNull(keySupplier,
30-
"Provide a key function, might just be 'MemoizationDefaults.staticKey()'.");
30+
"Provide a key supplier, might just be 'MemoizationDefaults.staticKey()'.");
3131
this.callable = requireNonNull(callable,
3232
"Cannot memoize a NULL Callable - provide an actual Callable to fix this.");
3333
}
3434

3535
@Override
3636
public OUTPUT call() throws Exception {
3737
try {
38-
return computeIfAbsent(keySupplier.get(), givenKey -> {
38+
return computeIfAbsent(keySupplier.get(), key -> {
3939
try {
4040
return callable.call();
4141
} catch (final Exception exception) {

memoization-jdk/src/main/java/wtf/metio/memoization/jdk/ConsumerMemoizer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ final class ConsumerMemoizer<INPUT, KEY>
3232

3333
@Override
3434
public void accept(final INPUT input) {
35-
computeIfAbsent(keyFunction.apply(input), givenKey -> {
35+
computeIfAbsent(keyFunction.apply(input), key -> {
3636
consumer.accept(input);
37-
return givenKey;
37+
return key;
3838
});
3939
}
4040

memoization-jdk/src/main/java/wtf/metio/memoization/jdk/DoubleBinaryOperatorMemoizer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ final class DoubleBinaryOperatorMemoizer<KEY>
1818
private final DoubleBinaryFunction<KEY> keyFunction;
1919
private final DoubleBinaryOperator operator;
2020

21-
public DoubleBinaryOperatorMemoizer(
21+
DoubleBinaryOperatorMemoizer(
2222
final ConcurrentMap<KEY, Double> cache,
2323
final DoubleBinaryFunction<KEY> keyFunction,
2424
final DoubleBinaryOperator operator) {

memoization-jdk/src/main/java/wtf/metio/memoization/jdk/DoubleSupplierMemoizer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ final class DoubleSupplierMemoizer<KEY>
2525
final DoubleSupplier supplier) {
2626
super(cache);
2727
this.keySupplier = requireNonNull(keySupplier,
28-
"Provide a key function, might just be 'MemoizationDefaults.staticKey()'.");
28+
"Provide a key supplier, might just be 'MemoizationDefaults.staticKey()'.");
2929
this.supplier = requireNonNull(supplier,
3030
"Cannot memoize a NULL Supplier - provide an actual Supplier to fix this.");
3131
}

memoization-jdk/src/main/java/wtf/metio/memoization/jdk/FunctionMemoizer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ final class FunctionMemoizer<INPUT, KEY, OUTPUT>
3030

3131
@Override
3232
public OUTPUT apply(final INPUT input) {
33-
return computeIfAbsent(keyFunction.apply(input), givenKey -> function.apply(input));
33+
return computeIfAbsent(keyFunction.apply(input), key -> function.apply(input));
3434
}
3535

3636
}

memoization-jdk/src/main/java/wtf/metio/memoization/jdk/IntBinaryOperatorMemoizer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ final class IntBinaryOperatorMemoizer<KEY>
1818
private final IntBinaryFunction<KEY> keyFunction;
1919
private final IntBinaryOperator operator;
2020

21-
public IntBinaryOperatorMemoizer(
21+
IntBinaryOperatorMemoizer(
2222
final ConcurrentMap<KEY, Integer> cache,
2323
final IntBinaryFunction<KEY> keyFunction,
2424
final IntBinaryOperator operator) {

memoization-jdk/src/main/java/wtf/metio/memoization/jdk/IntSupplierMemoizer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ final class IntSupplierMemoizer<KEY>
2525
final IntSupplier supplier) {
2626
super(cache);
2727
this.keySupplier = requireNonNull(keySupplier,
28-
"Provide a key function, might just be 'MemoizationDefaults.staticKey()'.");
28+
"Provide a key supplier, might just be 'MemoizationDefaults.staticKey()'.");
2929
this.supplier = requireNonNull(supplier,
3030
"Cannot memoize a NULL Supplier - provide an actual Supplier to fix this.");
3131
}

memoization-jdk/src/main/java/wtf/metio/memoization/jdk/LongBinaryOperatorMemoizer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ final class LongBinaryOperatorMemoizer<KEY>
1818
private final LongBinaryFunction<KEY> keyFunction;
1919
private final LongBinaryOperator operator;
2020

21-
public LongBinaryOperatorMemoizer(
21+
LongBinaryOperatorMemoizer(
2222
final ConcurrentMap<KEY, Long> cache,
2323
final LongBinaryFunction<KEY> keyFunction,
2424
final LongBinaryOperator operator) {

memoization-jdk/src/main/java/wtf/metio/memoization/jdk/LongConsumerMemoizer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ final class LongConsumerMemoizer<KEY>
1919
private final LongFunction<KEY> keyFunction;
2020
private final LongConsumer consumer;
2121

22-
public LongConsumerMemoizer(
22+
LongConsumerMemoizer(
2323
final ConcurrentMap<KEY, KEY> cache,
2424
final LongFunction<KEY> keyFunction,
2525
final LongConsumer consumer) {
2626
super(cache);
2727
this.keyFunction = requireNonNull(keyFunction, "Provide a key function.");
2828
this.consumer = requireNonNull(consumer,
29-
"Cannot memoize a NULL Consumer - provide an actual Consumer to fix this.");
29+
"Cannot memoize a NULL LongConsumer - provide an actual LongConsumer to fix this.");
3030
}
3131

3232
@Override

memoization-jdk/src/main/java/wtf/metio/memoization/jdk/LongSupplierMemoizer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ final class LongSupplierMemoizer<KEY>
2525
final LongSupplier supplier) {
2626
super(cache);
2727
this.keySupplier = requireNonNull(keySupplier,
28-
"Provide a key function, might just be 'MemoizationDefaults.staticKey()'.");
28+
"Provide a key supplier, might just be 'MemoizationDefaults.staticKey()'.");
2929
this.supplier = requireNonNull(supplier,
3030
"Cannot memoize a NULL Supplier - provide an actual Supplier to fix this.");
3131
}

0 commit comments

Comments
 (0)