Skip to content

Commit 083dadb

Browse files
Sebastian Hoßsebhoss
authored andcommitted
support lambda
1 parent 7a031d7 commit 083dadb

File tree

45 files changed

+2690
-53
lines changed

Some content is hidden

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

45 files changed

+2690
-53
lines changed

README.md

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Java [memoization](https://en.wikipedia.org/wiki/Memoization) library - trade sp
1111

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`
14+
* Memoize calls to [lambda](https://github.com/palatable/lambda) interfaces like `Fn0..8`
1415
* 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).
1516
* Use custom cache keys for fine-tuning
1617

@@ -20,12 +21,14 @@ Memoize any of the supported types by using the static factory methods supplied
2021

2122
* `Memoize` if you want to memoize JDK interfaces.
2223
* `MemoizeJool` if you want to memoize jOOL interfaces.
24+
* `MemoizeLambda` if you want to memoize lambda interfaces.
2325

2426
### Default cache with default cache keys
2527

2628
```java
2729
wtf.metio.memoization.jdk.Memoize;
2830
wtf.metio.memoization.jool.MemoizeJool;
31+
wtf.metio.memoization.jool.MemoizeLambda;
2932

3033
Function<INPUT, OUTPUT> function = ...;
3134
Function<INPUT, OUTPUT> memoizedFunction = Memoize.function(function);
@@ -36,15 +39,16 @@ Supplier<OUTPUT> memoizedSupplier = Memoize.supplier(supplier);
3639
Function3<T1, T2, T3, OUTPUT> function = ...;
3740
Function3<T1, T2, T3, OUTPUT> memoizedFunction = MemoizeJool.function3(function);
3841

39-
Consumer4<T1, T2, T3, T4> consumer = ...;
40-
Consumer4<T1, T2, T3, T4> memoizedConsumer = MemoizeJool.consumer4(consumer);
42+
Fn4<T1, T2, T3, T4, OUTPUT> function = ...;
43+
Fn4<T1, T2, T3, T4, OUTPUT> memoizedFunction = MemoizeLambda.fn4(function);
4144
```
4245

4346
### Default cache with custom cache keys
4447

4548
```java
4649
wtf.metio.memoization.jdk.Memoize;
4750
wtf.metio.memoization.jool.MemoizeJool;
51+
wtf.metio.memoization.jool.MemoizeLambda;
4852

4953
Function<INPUT, OUTPUT> function = ...;
5054
Function<INPUT, KEY> keyFunction = ...;
@@ -58,16 +62,17 @@ Function3<T1, T2, T3, OUTPUT> function = ...;
5862
Function3<T1, T2, T3, KEY> keyFunction = ...;
5963
Function3<T1, T2, T3, OUTPUT> memoizedFunction = MemoizeJool.function3(function, keyFunction);
6064

61-
Consumer4<T1, T2, T3, T4> consumer = ...;
62-
Function3<T1, T2, T3, T4, KEY> keyFunction = ...;
63-
Consumer4<T1, T2, T3, T4> memoizedConsumer = MemoizeJool.consumer4(consumer, keyFunction);
65+
Fn4<T1, T2, T3, T4, OUTPUT> function = ...;
66+
Fn4<T1, T2, T3, T4, KEY> keyFunction = ...;
67+
Fn4<T1, T2, T3, T4, OUTPUT> memoizedFunction = MemoizeLambda.fn4(function, keyFunction);
6468
```
6569

6670
### Custom cache with default cache keys
6771

6872
```java
6973
wtf.metio.memoization.jdk.Memoize;
7074
wtf.metio.memoization.jool.MemoizeJool;
75+
wtf.metio.memoization.jool.MemoizeLambda;
7176

7277
// memoize in cache2k cache
7378
Function<INPUT, OUTPUT> function = ...;
@@ -85,16 +90,17 @@ Cache<String, OUTPUT> cache = ...; // com.google.common.cache
8590
Function3<T1, T2, T3, OUTPUT> memoizedFunction = MemoizeJool.function3(function, cache.asMap());
8691

8792
// memoize in ConcurrentMap
88-
Consumer4<T1, T2, T3, T4> consumer = ...;
89-
Map<String, String> cache = ...;
90-
Consumer4<T1, T2, T3, T4> memoizedConsumer = MemoizeJool.consumer4(consumer, cache);
93+
Fn4<T1, T2, T3, T4, OUTPUT> function = ...;
94+
Map<String, OUTPUT> cache = ...;
95+
Fn4<T1, T2, T3, T4, OUTPUT> memoizedFunction = MemoizeLambda.fn4(function, cache);
9196
```
9297

9398
### Custom cache with custom cache keys
9499

95100
```java
96101
wtf.metio.memoization.jdk.Memoize;
97102
wtf.metio.memoization.jool.MemoizeJool;
103+
wtf.metio.memoization.jool.MemoizeLambda;
98104

99105
// memoize in cache2k cache
100106
Function<INPUT, OUTPUT> function = ...;
@@ -115,10 +121,10 @@ Cache<KEY, OUTPUT> cache = ...; // com.google.common.cache
115121
Function3<T1, T2, T3, OUTPUT> memoizedFunction = MemoizeJool.function3(function, keyFunction, cache.asMap());
116122

117123
// memoize in ConcurrentMap
118-
Consumer4<T1, T2, T3, T4> consumer = ...;
119-
Function4<T1, T2, T3, T4, KEY> keyFunction = ...;
120-
Map<KEY, KEY> cache = ...;
121-
Consumer4<T1, T2, T3, T4> memoizedConsumer = MemoizeJool.consumer4(consumer, keyFunction, cache);
124+
Fn4<T1, T2, T3, T4, OUTPUT> function = ...;
125+
Fn4<T1, T2, T3, T4, KEY> keyFunction = ...;
126+
Map<KEY, OUTPUT> cache = ...;
127+
Fn4<T1, T2, T3, T4, OUTPUT> memoizedFunction = MemoizeLambda.fn4(function, keyFunction, cache);
122128
```
123129

124130
Note that `Memoize` and `MemoizeJool` do accept any `Map`, however they copy the entries in the map to a new `ConcurrentHashMap` in case the provided `Map` is not a `ConcurrentMap`. This is done in order to ensure atomic `computeIfAbsent` behavior.
@@ -145,6 +151,13 @@ In order to use this project, declare the following dependencies in your project
145151
</dependency>
146152
<!-- support for jOOL interfaces -->
147153

154+
<!-- support for lambda interfaces -->
155+
<dependency>
156+
<groupId>wtf.metio.memoization</groupId>
157+
<artifactId>memoization-lambda</artifactId>
158+
<version>${version.memoization}</version>
159+
</dependency>
160+
<!-- support for lambda interfaces -->
148161
</dependencies>
149162
```
150163

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @param <RESULT> The resulting type.
99
*/
1010
@FunctionalInterface
11-
interface DoubleBinaryFunction<RESULT> {
11+
public interface DoubleBinaryFunction<RESULT> {
1212

1313
/**
1414
* @param first The first double to apply.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @param <RESULT> The resulting type.
99
*/
1010
@FunctionalInterface
11-
interface IntBinaryFunction<RESULT> {
11+
public interface IntBinaryFunction<RESULT> {
1212

1313
/**
1414
* @param first The first int to apply.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @param <RESULT> The resulting type.
99
*/
1010
@FunctionalInterface
11-
interface LongBinaryFunction<RESULT> {
11+
public interface LongBinaryFunction<RESULT> {
1212

1313
/**
1414
* @param first The first long to apply.

0 commit comments

Comments
 (0)