From c9b6b9a5ca082fe7c1b6f58d7713f785a9eb6a5c Mon Sep 17 00:00:00 2001 From: Martial Simon Date: Mon, 15 Sep 2025 01:08:27 +0200 Subject: add: graphs et rushs --- graphs/java/scheduler/.gitignore | 38 ++++++ graphs/java/scheduler/pom.xml | 146 +++++++++++++++++++++ .../java/fr/epita/assistants/scheduler/MyTask.java | 48 +++++++ .../java/fr/epita/assistants/scheduler/Task.java | 75 +++++++++++ 4 files changed, 307 insertions(+) create mode 100644 graphs/java/scheduler/.gitignore create mode 100644 graphs/java/scheduler/pom.xml create mode 100644 graphs/java/scheduler/src/main/java/fr/epita/assistants/scheduler/MyTask.java create mode 100644 graphs/java/scheduler/src/main/java/fr/epita/assistants/scheduler/Task.java (limited to 'graphs/java/scheduler') diff --git a/graphs/java/scheduler/.gitignore b/graphs/java/scheduler/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/graphs/java/scheduler/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/graphs/java/scheduler/pom.xml b/graphs/java/scheduler/pom.xml new file mode 100644 index 0000000..007367f --- /dev/null +++ b/graphs/java/scheduler/pom.xml @@ -0,0 +1,146 @@ + + + 4.0.0 + fr.epita.assistants + scheduler + 1.0 + + + 21 + 5.9.1 + 3.13.0 + 3.5.0 + 3.1.1 + 3.1.0 + + UTF-8 + + ${project.build.directory}/surefire-reports + + + + + org.junit.jupiter + junit-jupiter + ${versions.junit} + + + org.apache.maven.surefire + surefire-junit-platform + ${versions.maven-surefire-plugin} + + + org.apache.maven + maven-compat + 3.9.8 + + + org.apache.maven + maven-plugin-api + 3.9.8 + + + org.apache.maven + maven-project + 2.2.1 + + + org.apache.maven + maven-core + 3.8.1 + + + org.apache.maven + maven-monitor + 2.2.1 + + + org.codehaus.plexus + plexus-utils + 3.0.24 + + + org.apache.maven.shared + maven-filtering + 3.3.2 + + + org.codehaus.plexus + plexus-interpolation + 1.13 + + + org.apache.maven + maven-profile + 2.2.1 + + + org.apache.maven + maven-artifact-manager + 2.2.1 + + + org.apache.maven + maven-plugin-registry + 2.2.1 + + + org.apache.maven + maven-repository-metadata + 2.2.1 + + + classworlds + classworlds + 1.1 + + + org.junit.platform + junit-platform-commons + 1.9.3 + + + + com.tngtech.archunit + archunit + 1.2.1 + test + + + com.tngtech.archunit + archunit-junit5-api + 1.0.0 + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${versions.maven-compiler-plugin} + + ${versions.java} + ${versions.java} + + + + org.apache.maven.plugins + maven-install-plugin + ${versions.maven-install-plugin} + + + + org.apache.maven.plugins + maven-surefire-plugin + ${versions.maven-surefire-plugin} + + ${surefire.reportsDirectory} + + + + + diff --git a/graphs/java/scheduler/src/main/java/fr/epita/assistants/scheduler/MyTask.java b/graphs/java/scheduler/src/main/java/fr/epita/assistants/scheduler/MyTask.java new file mode 100644 index 0000000..d1de4fb --- /dev/null +++ b/graphs/java/scheduler/src/main/java/fr/epita/assistants/scheduler/MyTask.java @@ -0,0 +1,48 @@ +package fr.epita.assistants.scheduler; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Executor; +import java.util.concurrent.TimeUnit; +import java.util.function.Function; +import java.util.function.Supplier; + +public class MyTask implements Task { + + CompletableFuture cpFuture; + + public MyTask(CompletableFuture cpFuture) { + this.cpFuture = cpFuture; + } + + public static Task of(Supplier actionSupplier) { + return new MyTask<>(CompletableFuture.supplyAsync(actionSupplier)); + } + + public static Task of(Supplier actionSupplier, Executor executor) { + return new MyTask<>(CompletableFuture.supplyAsync(actionSupplier, executor)); + } + + @Override + public CompletableFuture build() { + return cpFuture; + } + + @Override + public Task onErrorRecoverWith(Function recoveryFunction) { + return new MyTask<>(cpFuture.handle((result, exception) -> { + if (exception != null) + return recoveryFunction.apply(exception); + return result; + })); + } + + @Override + public Task andThenDo(Function action) { + return new MyTask<>(cpFuture.thenApply(action)); + } + + @Override + public Task andThenWait(long number, TimeUnit timeUnit) { + return new MyTask<>(cpFuture.thenApplyAsync(i -> i, CompletableFuture.delayedExecutor(number, timeUnit))); + } +} diff --git a/graphs/java/scheduler/src/main/java/fr/epita/assistants/scheduler/Task.java b/graphs/java/scheduler/src/main/java/fr/epita/assistants/scheduler/Task.java new file mode 100644 index 0000000..64c0c03 --- /dev/null +++ b/graphs/java/scheduler/src/main/java/fr/epita/assistants/scheduler/Task.java @@ -0,0 +1,75 @@ +package fr.epita.assistants.scheduler; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.function.Function; +import java.util.function.Supplier; + +/** + * Represent a single task to be executed. Tasks can be chained using the andThenDo method. + * + * @param The expected return type. + */ +public interface Task { + + /** + * Static initializer allowing the creation of a task instance with the given {@link Supplier}. + * + * @param actionSupplier The actual action to execute. + * @param The expected return type, inferred by the call chain. + * @return A {@link Task} instance. + */ + static Task of(Supplier actionSupplier) { + throw new UnsupportedOperationException("This default implementation should never be called, and should be" + + "implemented in classes that implement this interface"); + } + + /** + * Build the actual completable future according to this instance specifications. + * + * @return The built {@link CompletableFuture}. + */ + CompletableFuture build(); + + /** + * Execute a task and return its result. + * + * @return The result of the execution of this task. + */ + default RETURN_TYPE execute() { + try { + return build().get(); + } catch (InterruptedException | ExecutionException exception) { + throw new RuntimeException("Exception during task computing", exception); + } + } + + /** + * Specify a function that provides a recovery value to use if the task fails. + * + * @param recoveryFunction The function that will be called with the exception - should any happen - in order to + * compute a recovery value. + * @return The updated task. + */ + Task onErrorRecoverWith(final Function recoveryFunction); + + /** + * Chain a new task to be executed after the current one, taking the output of the current one as its input. + * + * @param action The action to perform. + * @param The return type of the task to create. + * @return The created task. + */ + Task andThenDo(final Function action); + + /** + * Wait for the given number of timeUnit. + * + * @param number The number of units to wait for. + * @param timeUnit The unit. + * @return The created task. + */ + Task andThenWait(final long number, final TimeUnit timeUnit); + +} -- cgit v1.2.3