Skip to content

Commit 7a031d7

Browse files
sebhossSebastian Hoß
andauthored
fix #186 add jOOL implementation (#205)
Co-authored-by: Sebastian Hoß <seb@hoß.de>
1 parent d4cbedd commit 7a031d7

File tree

171 files changed

+10324
-14496
lines changed

Some content is hidden

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

171 files changed

+10324
-14496
lines changed

README.md

Lines changed: 67 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -9,200 +9,146 @@ Java [memoization](https://en.wikipedia.org/wiki/Memoization) library - trade sp
99

1010
## Features
1111

12-
* Memoize calls to `Consumer`, `Function`, `Predicate`, `Supplier` and other functional interfaces in `java.util.function`
13-
* Cache values using [Caffeine](https://github.com/ben-manes/caffeine), [Guava](https://github.com/google/guava/wiki/CachesExplained), [cache2k](https://cache2k.org/), or any [`ConcurrentMap`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/concurrent/ConcurrentMap.html).
14-
* Customize caches and the cache keys
12+
* Memoize calls to JDK interfaces like `Consumer`, `Function`, `Predicate`, `Supplier`, and more
13+
* Memoize calls to [jOOL](https://github.com/jOOQ/jOOL) interfaces like `Consumer0..16` and `Function0..16`
14+
* 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).
15+
* Use custom cache keys for fine-tuning
1516

1617
## Usage
1718

1819
Memoize any of the supported types by using the static factory methods supplied by:
1920

20-
* `Cache2kMemoize` if you want to use [cache2k](https://cache2k.org/) caches.
21-
* `CaffeineMemoize` if you want to use [Caffeine](https://github.com/ben-manes/caffeine) caches.
22-
* `GuavaMemoize` if you want to use [Guava](https://github.com/google/guava/wiki/CachesExplained) caches.
23-
* `MapMemoize` if you want to use any [`ConcurrentMap`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/concurrent/ConcurrentMap.html) as cache.
21+
* `Memoize` if you want to memoize JDK interfaces.
22+
* `MemoizeJool` if you want to memoize jOOL interfaces.
2423

2524
### Default cache with default cache keys
2625

2726
```java
28-
wtf.metio.memoization.cache2k.Cache2kMemoize;
29-
wtf.metio.memoization.caffeine.CaffeineMemoize;
30-
wtf.metio.memoization.guava.GuavaMemoize;
31-
wtf.metio.memoization.map.MapMemoize;
27+
wtf.metio.memoization.jdk.Memoize;
28+
wtf.metio.memoization.jool.MemoizeJool;
3229

33-
// memoize in cache2k cache
34-
Consumer<INPUT> predicate = ...;
35-
Consumer<INPUT> memoizedPredicate = Cache2kMemoize.predicate(predicate);
36-
37-
// memoize in Caffeine cache
38-
Consumer<INPUT> consumer = ...;
39-
Consumer<INPUT> memoizedConsumer = CaffeineMemoize.consumer(consumer);
40-
41-
// memoize in Guava cache
4230
Function<INPUT, OUTPUT> function = ...;
43-
Function<INPUT, OUTPUT> memoizedFunction = GuavaMemoize.function(function);
31+
Function<INPUT, OUTPUT> memoizedFunction = Memoize.function(function);
4432

45-
// memoize in ConcurrentMap
4633
Supplier<OUTPUT> supplier = ...;
47-
Supplier<OUTPUT> memoizedSupplier = MapMemoize.supplier(supplier);
34+
Supplier<OUTPUT> memoizedSupplier = Memoize.supplier(supplier);
35+
36+
Function3<T1, T2, T3, OUTPUT> function = ...;
37+
Function3<T1, T2, T3, OUTPUT> memoizedFunction = MemoizeJool.function3(function);
38+
39+
Consumer4<T1, T2, T3, T4> consumer = ...;
40+
Consumer4<T1, T2, T3, T4> memoizedConsumer = MemoizeJool.consumer4(consumer);
4841
```
4942

5043
### Default cache with custom cache keys
5144

5245
```java
53-
wtf.metio.memoization.cache2k.Cache2kMemoize;
54-
wtf.metio.memoization.caffeine.CaffeineMemoize;
55-
wtf.metio.memoization.guava.GuavaMemoize;
56-
wtf.metio.memoization.map.MapMemoize;
57-
58-
// memoize in cache2k cache
59-
Consumer<INPUT> predicate = ...;
60-
Function<INPUT, KEY> keyFunction = ...;
61-
Consumer<INPUT> memoizedPredicate = Cache2kMemoize.predicate(predicate, keyFunction);
62-
63-
// memoize in Caffeine cache
64-
Consumer<INPUT> consumer = ...;
65-
Function<INPUT, KEY> keyFunction = ...;
66-
Consumer<INPUT> memoizedConsumer = CaffeineMemoize.consumer(consumer, keyFunction);
46+
wtf.metio.memoization.jdk.Memoize;
47+
wtf.metio.memoization.jool.MemoizeJool;
6748

68-
// memoize in Guava cache
6949
Function<INPUT, OUTPUT> function = ...;
7050
Function<INPUT, KEY> keyFunction = ...;
71-
Function<INPUT, OUTPUT> memoizedFunction = GuavaMemoize.function(function, keyFunction);
51+
Function<INPUT, OUTPUT> memoizedFunction = Memoize.function(function, keyFunction);
7252

73-
// memoize in ConcurrentMap
7453
Supplier<OUTPUT> supplier = ...;
7554
Supplier<KEY> keySupplier = ...;
76-
Supplier<OUTPUT> memoizedSupplier = MapMemoize.supplier(supplier, keySupplier);
55+
Supplier<OUTPUT> memoizedSupplier = Memoize.supplier(supplier, keySupplier);
56+
57+
Function3<T1, T2, T3, OUTPUT> function = ...;
58+
Function3<T1, T2, T3, KEY> keyFunction = ...;
59+
Function3<T1, T2, T3, OUTPUT> memoizedFunction = MemoizeJool.function3(function, keyFunction);
60+
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);
7764
```
7865

7966
### Custom cache with default cache keys
8067

8168
```java
82-
wtf.metio.memoization.cache2k.Cache2kMemoize;
83-
wtf.metio.memoization.caffeine.CaffeineMemoize;
84-
wtf.metio.memoization.guava.GuavaMemoize;
85-
wtf.metio.memoization.map.MapMemoize;
69+
wtf.metio.memoization.jdk.Memoize;
70+
wtf.metio.memoization.jool.MemoizeJool;
8671

8772
// memoize in cache2k cache
88-
Consumer<INPUT> predicate = ...;
89-
Cache<INPUT, INPUT> cache = ...; // org.cache2k.Cache
90-
Consumer<INPUT> memoizedPredicate = Cache2kMemoize.predicate(predicate, cache);
73+
Function<INPUT, OUTPUT> function = ...;
74+
Cache<INPUT, OUTPUT> cache = ...; // org.cache2k.Cache
75+
Function<INPUT, OUTPUT> memoizedFunction = Memoize.function(function, cache.asMap());
9176

9277
// memoize in Caffeine cache
93-
Consumer<INPUT> consumer = ...;
94-
Cache<INPUT, INPUT> cache = ...; // com.github.benmanes.caffeine.cache.Cache
95-
Consumer<INPUT> memoizedConsumer = CaffeineMemoize.consumer(consumer, cache);
78+
Supplier<OUTPUT> supplier = ...;
79+
Cache<String, OUTPUT> cache = ...; // com.github.benmanes.caffeine.cache.Cache
80+
Supplier<OUTPUT> memoizedSupplier = Memoize.supplier(supplier, cache.asMap());
9681

9782
// memoize in Guava cache
98-
Function<INPUT, OUTPUT> function = ...;
99-
Cache<INPUT, OUTPUT> cache = ...; // com.google.common.cache.Cache
100-
Function<INPUT, OUTPUT> memoizedFunction = GuavaMemoize.function(function, cache);
83+
Function3<T1, T2, T3, OUTPUT> function = ...;
84+
Cache<String, OUTPUT> cache = ...; // com.google.common.cache.Cache
85+
Function3<T1, T2, T3, OUTPUT> memoizedFunction = MemoizeJool.function3(function, cache.asMap());
10186

10287
// memoize in ConcurrentMap
103-
Supplier<OUTPUT> supplier = ...;
104-
Map<String, OUTPUT> cache = ...;
105-
Supplier<OUTPUT> memoizedSupplier = MapMemoize.supplier(supplier, cache);
88+
Consumer4<T1, T2, T3, T4> consumer = ...;
89+
Map<String, String> cache = ...;
90+
Consumer4<T1, T2, T3, T4> memoizedConsumer = MemoizeJool.consumer4(consumer, cache);
10691
```
10792

10893
### Custom cache with custom cache keys
10994

11095
```java
111-
wtf.metio.memoization.cache2k.Cache2kMemoize;
112-
wtf.metio.memoization.caffeine.CaffeineMemoize;
113-
wtf.metio.memoization.guava.GuavaMemoize;
114-
wtf.metio.memoization.map.MapMemoize;
96+
wtf.metio.memoization.jdk.Memoize;
97+
wtf.metio.memoization.jool.MemoizeJool;
11598

11699
// memoize in cache2k cache
117-
Consumer<INPUT> predicate = ...;
100+
Function<INPUT, OUTPUT> function = ...;
118101
Function<INPUT, KEY> keyFunction = ...;
119-
Cache<KEY, INPUT> cache = ...; // org.cache2k.Cache
120-
Consumer<INPUT> memoizedPredicate = Cache2kMemoize.predicate(predicate, keyFunction, cache);
102+
Cache<KEY, OUTPUT> cache = ...; // org.cache2k.Cache
103+
Function<INPUT, OUTPUT> memoizedFunction = Memoize.function(function, keyFunction, cache.asMap());
121104

122105
// memoize in Caffeine cache
123-
Consumer<INPUT> consumer = ...;
124-
Function<INPUT, KEY> keyFunction = ...;
125-
Cache<KEY, INPUT> cache = ...; // com.github.benmanes.caffeine.cache.Cache
126-
Consumer<INPUT> memoizedConsumer = CaffeineMemoize.consumer(consumer, keyFunction, cache);
106+
Supplier<OUTPUT> supplier = ...;
107+
Supplier<KEY> keySupplier = ...;
108+
Cache<KEY, OUTPUT> cache = ...; // com.github.benmanes.caffeine.cache.Cache
109+
Supplier<OUTPUT> memoizedSupplier = Memoize.supplier(supplier, keySupplier, cache.asMap());
127110

128111
// memoize in Guava cache
129-
Function<INPUT, OUTPUT> function = ...;
130-
Function<INPUT, KEY> keyFunction = ...;
131-
Cache<KEY, OUTPUT> cache = ...; // com.google.common.cache.Cache
132-
Function<INPUT, OUTPUT> memoizedFunction = GuavaMemoize.function(function, keyFunction, cache);
112+
Function3<T1, T2, T3, OUTPUT> function = ...;
113+
Function3<T1, T2, T3, KEY> keyFunction = ...;
114+
Cache<KEY, OUTPUT> cache = ...; // com.google.common.cache.Cache
115+
Function3<T1, T2, T3, OUTPUT> memoizedFunction = MemoizeJool.function3(function, keyFunction, cache.asMap());
133116

134117
// memoize in ConcurrentMap
135-
Supplier<OUTPUT> supplier = ...;
136-
Supplier<KEY> keySupplier = ...;
137-
Map<KEY, OUTPUT> cache = ...;
138-
Supplier<OUTPUT> memoizedSupplier = MapMemoize.supplier(supplier, keySupplier, cache);
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);
139122
```
140123

141-
Note that `MapMemoize` does accept any `Map`, however copies the entries in the map to a new `ConcurrentHashMap` in case the provided `Map` is not a `ConcurrentMap` as well. This is done in order to ensure atomic `computeIfAbsent` behavior.
124+
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.
142125

143126
## Integration
144127

145128
In order to use this project, declare the following dependencies in your project:
146129

147130
```xml
148131
<dependencies>
149-
<!-- ConcurrentMap ONLY -->
150-
<dependency>
151-
<groupId>wtf.metio.memoization</groupId>
152-
<artifactId>memoization-map</artifactId>
153-
<version>${version.memoization}</version>
154-
</dependency>
155-
<!-- ConcurrentMap ONLY -->
156-
157-
<!-- cache2k ONLY -->
158-
<dependency>
159-
<groupId>wtf.metio.memoization</groupId>
160-
<artifactId>memoization-cache2k</artifactId>
161-
<version>${version.memoization}</version>
162-
</dependency>
163-
<dependency>
164-
<groupId>org.cache2k</groupId>
165-
<artifactId>cache2k-api</artifactId>
166-
<version>${version.cache2k}</version>
167-
</dependency>
168-
<dependency>
169-
<groupId>org.cache2k</groupId>
170-
<artifactId>cache2k-core</artifactId>
171-
<version>${version.cache2k}</version>
172-
<scope>runtime</scope>
173-
</dependency>
174-
<!-- cache2k ONLY -->
175-
176-
<!-- Caffeine ONLY -->
132+
<!-- support for JDK interfaces -->
177133
<dependency>
178134
<groupId>wtf.metio.memoization</groupId>
179-
<artifactId>memoization-caffeine</artifactId>
135+
<artifactId>memoization-jdk</artifactId>
180136
<version>${version.memoization}</version>
181137
</dependency>
182-
<dependency>
183-
<groupId>com.github.ben-manes.caffeine</groupId>
184-
<artifactId>caffeine</artifactId>
185-
<version>${version.caffeine}</version>
186-
</dependency>
187-
<!-- Caffeine ONLY -->
138+
<!-- support for JDK interfaces -->
188139

189-
<!-- Guava ONLY -->
140+
<!-- support for jOOL interfaces -->
190141
<dependency>
191142
<groupId>wtf.metio.memoization</groupId>
192-
<artifactId>memoization-guava</artifactId>
143+
<artifactId>memoization-jool</artifactId>
193144
<version>${version.memoization}</version>
194145
</dependency>
195-
<dependency>
196-
<groupId>com.google.guava</groupId>
197-
<artifactId>guava</artifactId>
198-
<version>${version.guava}</version>
199-
</dependency>
200-
<!-- Guava ONLY -->
146+
<!-- support for jOOL interfaces -->
201147

202148
</dependencies>
203149
```
204150

205-
Replace `${version.memoization}` with the latest release. Use [ExpiringMap](https://github.com/jhalterman/expiringmap), [ConcurrentLinkedHashMap](https://github.com/ben-manes/concurrentlinkedhashmap), [Chronicle-Map](https://github.com/OpenHFT/Chronicle-Map), [Cacheonix](http://www.cacheonix.org/) or other `ConcurrentMap` implementations as alternatives to the default `ConcurrentHashMap` used in the `MapMemoize` factory.
151+
Replace `${version.memoization}` with the latest release.
206152

207153
## Alternatives
208154

memoization-cache2k/pom.xml

Lines changed: 0 additions & 64 deletions
This file was deleted.

memoization-cache2k/src/main/java/module-info.java

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)