summaryrefslogtreecommitdiff
path: root/graphs/java/scheduler
diff options
context:
space:
mode:
Diffstat (limited to 'graphs/java/scheduler')
-rw-r--r--graphs/java/scheduler/.gitignore38
-rw-r--r--graphs/java/scheduler/pom.xml146
-rw-r--r--graphs/java/scheduler/src/main/java/fr/epita/assistants/scheduler/MyTask.java48
-rw-r--r--graphs/java/scheduler/src/main/java/fr/epita/assistants/scheduler/Task.java75
4 files changed, 307 insertions, 0 deletions
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 @@
+<?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>
+ <groupId>fr.epita.assistants</groupId>
+ <artifactId>scheduler</artifactId>
+ <version>1.0</version>
+
+ <properties>
+ <versions.java>21</versions.java>
+ <versions.junit>5.9.1</versions.junit>
+ <versions.maven-compiler-plugin>3.13.0</versions.maven-compiler-plugin>
+ <versions.maven-surefire-plugin>3.5.0</versions.maven-surefire-plugin>
+ <versions.maven-jar-plugin>3.1.1</versions.maven-jar-plugin>
+ <versions.maven-install-plugin>3.1.0</versions.maven-install-plugin>
+
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+
+ <surefire.reportsDirectory>${project.build.directory}/surefire-reports</surefire.reportsDirectory>
+ </properties>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.junit.jupiter</groupId>
+ <artifactId>junit-jupiter</artifactId>
+ <version>${versions.junit}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.maven.surefire</groupId>
+ <artifactId>surefire-junit-platform</artifactId>
+ <version>${versions.maven-surefire-plugin}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-compat</artifactId>
+ <version>3.9.8</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-plugin-api</artifactId>
+ <version>3.9.8</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-project</artifactId>
+ <version>2.2.1</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-core</artifactId>
+ <version>3.8.1</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-monitor</artifactId>
+ <version>2.2.1</version>
+ </dependency>
+ <dependency>
+ <groupId>org.codehaus.plexus</groupId>
+ <artifactId>plexus-utils</artifactId>
+ <version>3.0.24</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.maven.shared</groupId>
+ <artifactId>maven-filtering</artifactId>
+ <version>3.3.2</version>
+ </dependency>
+ <dependency>
+ <groupId>org.codehaus.plexus</groupId>
+ <artifactId>plexus-interpolation</artifactId>
+ <version>1.13</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-profile</artifactId>
+ <version>2.2.1</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-artifact-manager</artifactId>
+ <version>2.2.1</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-plugin-registry</artifactId>
+ <version>2.2.1</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-repository-metadata</artifactId>
+ <version>2.2.1</version>
+ </dependency>
+ <dependency>
+ <groupId>classworlds</groupId>
+ <artifactId>classworlds</artifactId>
+ <version>1.1</version>
+ </dependency>
+ <dependency>
+ <groupId>org.junit.platform</groupId>
+ <artifactId>junit-platform-commons</artifactId>
+ <version>1.9.3</version>
+ </dependency>
+ <!-- FIXME This should only be for testing (not included in student pom) -->
+ <dependency>
+ <groupId>com.tngtech.archunit</groupId>
+ <artifactId>archunit</artifactId>
+ <version>1.2.1</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>com.tngtech.archunit</groupId>
+ <artifactId>archunit-junit5-api</artifactId>
+ <version>1.0.0</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>${versions.maven-compiler-plugin}</version>
+ <configuration>
+ <source>${versions.java}</source>
+ <target>${versions.java}</target>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-install-plugin</artifactId>
+ <version>${versions.maven-install-plugin}</version>
+ </plugin>
+
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <version>${versions.maven-surefire-plugin}</version>
+ <configuration>
+ <reportsDirectory>${surefire.reportsDirectory}</reportsDirectory>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project>
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<INPUT_TYPE, RETURN_TYPE> implements Task<RETURN_TYPE> {
+
+ CompletableFuture<RETURN_TYPE> cpFuture;
+
+ public MyTask(CompletableFuture<RETURN_TYPE> cpFuture) {
+ this.cpFuture = cpFuture;
+ }
+
+ public static <RETURN_TYPE> Task<RETURN_TYPE> of(Supplier<RETURN_TYPE> actionSupplier) {
+ return new MyTask<>(CompletableFuture.supplyAsync(actionSupplier));
+ }
+
+ public static <RETURN_TYPE> Task<RETURN_TYPE> of(Supplier<RETURN_TYPE> actionSupplier, Executor executor) {
+ return new MyTask<>(CompletableFuture.supplyAsync(actionSupplier, executor));
+ }
+
+ @Override
+ public CompletableFuture<RETURN_TYPE> build() {
+ return cpFuture;
+ }
+
+ @Override
+ public Task<RETURN_TYPE> onErrorRecoverWith(Function<Throwable, RETURN_TYPE> recoveryFunction) {
+ return new MyTask<>(cpFuture.handle((result, exception) -> {
+ if (exception != null)
+ return recoveryFunction.apply(exception);
+ return result;
+ }));
+ }
+
+ @Override
+ public <NEW_RETURN_TYPE> Task<NEW_RETURN_TYPE> andThenDo(Function<RETURN_TYPE, NEW_RETURN_TYPE> action) {
+ return new MyTask<>(cpFuture.thenApply(action));
+ }
+
+ @Override
+ public Task<RETURN_TYPE> 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 <RETURN_TYPE> The expected return type.
+ */
+public interface Task<RETURN_TYPE> {
+
+ /**
+ * Static initializer allowing the creation of a task instance with the given {@link Supplier}.
+ *
+ * @param actionSupplier The actual action to execute.
+ * @param <RETURN_TYPE> The expected return type, inferred by the call chain.
+ * @return A {@link Task} instance.
+ */
+ static <RETURN_TYPE> Task<RETURN_TYPE> of(Supplier<RETURN_TYPE> 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<RETURN_TYPE> 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<RETURN_TYPE> onErrorRecoverWith(final Function<Throwable, RETURN_TYPE> 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 <NEW_RETURN_TYPE> The return type of the task to create.
+ * @return The created task.
+ */
+ <NEW_RETURN_TYPE> Task<NEW_RETURN_TYPE> andThenDo(final Function<RETURN_TYPE, NEW_RETURN_TYPE> 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<RETURN_TYPE> andThenWait(final long number, final TimeUnit timeUnit);
+
+}