Commit ca7e278e authored by Colin DAMON's avatar Colin DAMON

Java memoizers implementation

parent 0c8572af
......@@ -39,3 +39,4 @@ include:
- local: "/java-monades/.gitlab-ci.yml"
- local: "/java-diamond/.gitlab-ci.yml"
- local: "/diamond-ts/.gitlab-ci.yml"
- local: "/java-memoizers/.gitlab-ci.yml"
package-java-memoizers:
variables:
PROJECT_FOLDER: "java-memoizers"
extends: .java
only:
refs:
- master
- merge_requests
changes:
- ".gitlab-common-ci.yml"
- "java-memoizers/**/*"
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<version>1.0.0</version>
<groupId>fr.ippon.kata</groupId>
<artifactId>java-parent</artifactId>
<relativePath>../java-parent</relativePath>
</parent>
<version>1.0.0-SNAPSHOT</version>
<artifactId>java-memoizers</artifactId>
<name>JavaMemoizers</name>
<developers>
<developer>
<email>arey@ippon.fr</email>
<name>Anthont REY</name>
</developer>
<developer>
<email>cdamon@ippon.fr</email>
<name>Colin DAMON</name>
</developer>
</developers>
</project>
# Java memoizer
Implémentation de memoizers en Java
- **Auteurs** : Anthony REY et Colin DAMON
- **Date** : 18/06/2021
- **Langage** : Java
- **Niveau** : Enervé
- **Replay** : [Twitch](https://www.twitch.tv/videos/1059837117)
package fr.craft.memoizer;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Supplier;
public final class Memoizers {
private Memoizers() {}
public static <Result> Supplier<Result> of(Supplier<Result> supplier) {
return () -> of(dummy -> supplier.get()).apply(null);
}
public static <Input, Result> Function<Input, Result> of(Function<Input, Result> function) {
return new MemoizedFunction<>(function);
}
private static class MemoizedFunction<Input, Result> implements Function<Input, Result> {
private final Function<Input, Result> function;
private final Map<MemoizedInput<Input>, MemoizedResult<Result>> results = new ConcurrentHashMap<>();
public MemoizedFunction(Function<Input, Result> function) {
this.function = function;
}
@Override
public Result apply(Input input) {
return results.computeIfAbsent(new MemoizedInput<>(input), this::toMemoizedResult).result();
}
private MemoizedResult<Result> toMemoizedResult(MemoizedInput<Input> input) {
return new MemoizedResult<>(function.apply(input.input()));
}
private static record MemoizedInput<Input>(Input input) {}
private static record MemoizedResult<Result>(Result result) {}
}
}
package fr.craft.memoizer;
import static org.assertj.core.api.Assertions.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.function.Supplier;
import org.junit.jupiter.api.Test;
class MemoizersTest {
@Test
void shouldGetFunctionResult() {
Function<Double, Double> memoizer = Memoizers.of(d -> d * d);
assertThat(memoizer.apply(2D)).isEqualTo(4D);
}
@Test
void shouldMemoizeFunctionResult() {
AtomicInteger result = new AtomicInteger();
Function<Object, Integer> memoizer = Memoizers.of(d -> result.incrementAndGet());
assertThat(memoizer.apply(1)).isEqualTo(memoizer.apply(1));
assertThat(memoizer.apply(1)).isNotEqualTo(memoizer.apply(2));
}
@Test
void shouldMemoizeNullResult() {
NullFactory factory = new NullFactory();
Function<Object, String> memoizer = Memoizers.of(factory);
memoizer.apply(1);
memoizer.apply(1);
assertThat(factory.callsCount()).isEqualTo(1);
assertThat(memoizer.apply(1)).isNull();
}
@Test
void shouldMemoizeSupplier() {
Supplier<String> supplier = Memoizers.of(() -> "Pouet");
assertThat(supplier.get()).isEqualTo("Pouet");
}
private static class NullFactory implements Function<Object, String> {
private final AtomicInteger callsCount = new AtomicInteger();
public int callsCount() {
return callsCount.get();
}
@Override
public String apply(Object input) {
callsCount.incrementAndGet();
return null;
}
}
}
......@@ -86,6 +86,7 @@ Un kata de code est un petit exercice pensé pour s'entrainer jusqu'à maitriser
- [Concurrence en Java](/java-concurrence)
- [Try monade](/try-monade)
- [Memoizers](/java-memoizers)
### Bon Chance
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment