commit 86fdcffec23c5abcf0381a531696b8dca1297b4f Author: HorizonCode Date: Fri Feb 18 09:48:31 2022 +0100 initial commit diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..adc72bc --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/google-java-format.xml b/.idea/google-java-format.xml new file mode 100644 index 0000000..8b57f45 --- /dev/null +++ b/.idea/google-java-format.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..712ab9d --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__com_google_code_findbugs_jsr305_3_0_2.xml b/.idea/libraries/Maven__com_google_code_findbugs_jsr305_3_0_2.xml new file mode 100644 index 0000000..1c380d0 --- /dev/null +++ b/.idea/libraries/Maven__com_google_code_findbugs_jsr305_3_0_2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__com_google_errorprone_error_prone_annotations_2_7_1.xml b/.idea/libraries/Maven__com_google_errorprone_error_prone_annotations_2_7_1.xml new file mode 100644 index 0000000..3df0929 --- /dev/null +++ b/.idea/libraries/Maven__com_google_errorprone_error_prone_annotations_2_7_1.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__com_google_guava_failureaccess_1_0_1.xml b/.idea/libraries/Maven__com_google_guava_failureaccess_1_0_1.xml new file mode 100644 index 0000000..36e948e --- /dev/null +++ b/.idea/libraries/Maven__com_google_guava_failureaccess_1_0_1.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__com_google_guava_guava_31_0_1_jre.xml b/.idea/libraries/Maven__com_google_guava_guava_31_0_1_jre.xml new file mode 100644 index 0000000..08b28cc --- /dev/null +++ b/.idea/libraries/Maven__com_google_guava_guava_31_0_1_jre.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__com_google_guava_listenablefuture_9999_0_empty_to_avoid_conflict_with_guava.xml b/.idea/libraries/Maven__com_google_guava_listenablefuture_9999_0_empty_to_avoid_conflict_with_guava.xml new file mode 100644 index 0000000..4e15702 --- /dev/null +++ b/.idea/libraries/Maven__com_google_guava_listenablefuture_9999_0_empty_to_avoid_conflict_with_guava.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__com_google_j2objc_j2objc_annotations_1_3.xml b/.idea/libraries/Maven__com_google_j2objc_j2objc_annotations_1_3.xml new file mode 100644 index 0000000..bacaa45 --- /dev/null +++ b/.idea/libraries/Maven__com_google_j2objc_j2objc_annotations_1_3.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__com_thoughtworks_qdox_qdox_2_0_1.xml b/.idea/libraries/Maven__com_thoughtworks_qdox_qdox_2_0_1.xml new file mode 100644 index 0000000..d663c32 --- /dev/null +++ b/.idea/libraries/Maven__com_thoughtworks_qdox_qdox_2_0_1.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__commons_io_commons_io_2_11_0.xml b/.idea/libraries/Maven__commons_io_commons_io_2_11_0.xml new file mode 100644 index 0000000..f94fc5a --- /dev/null +++ b/.idea/libraries/Maven__commons_io_commons_io_2_11_0.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_apache_commons_commons_lang3_3_8_1.xml b/.idea/libraries/Maven__org_apache_commons_commons_lang3_3_8_1.xml new file mode 100644 index 0000000..33b78e9 --- /dev/null +++ b/.idea/libraries/Maven__org_apache_commons_commons_lang3_3_8_1.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_apache_maven_plugins_maven_compiler_plugin_3_10_0.xml b/.idea/libraries/Maven__org_apache_maven_plugins_maven_compiler_plugin_3_10_0.xml new file mode 100644 index 0000000..12317a5 --- /dev/null +++ b/.idea/libraries/Maven__org_apache_maven_plugins_maven_compiler_plugin_3_10_0.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_apache_maven_shared_maven_shared_incremental_1_1.xml b/.idea/libraries/Maven__org_apache_maven_shared_maven_shared_incremental_1_1.xml new file mode 100644 index 0000000..86cb928 --- /dev/null +++ b/.idea/libraries/Maven__org_apache_maven_shared_maven_shared_incremental_1_1.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_apache_maven_shared_maven_shared_utils_3_3_4.xml b/.idea/libraries/Maven__org_apache_maven_shared_maven_shared_utils_3_3_4.xml new file mode 100644 index 0000000..18e1ac7 --- /dev/null +++ b/.idea/libraries/Maven__org_apache_maven_shared_maven_shared_utils_3_3_4.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_checkerframework_checker_qual_3_12_0.xml b/.idea/libraries/Maven__org_checkerframework_checker_qual_3_12_0.xml new file mode 100644 index 0000000..1b2a35b --- /dev/null +++ b/.idea/libraries/Maven__org_checkerframework_checker_qual_3_12_0.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_codehaus_plexus_plexus_compiler_api_2_10_0.xml b/.idea/libraries/Maven__org_codehaus_plexus_plexus_compiler_api_2_10_0.xml new file mode 100644 index 0000000..f5f6716 --- /dev/null +++ b/.idea/libraries/Maven__org_codehaus_plexus_plexus_compiler_api_2_10_0.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_codehaus_plexus_plexus_compiler_javac_2_10_0.xml b/.idea/libraries/Maven__org_codehaus_plexus_plexus_compiler_javac_2_10_0.xml new file mode 100644 index 0000000..cbfb463 --- /dev/null +++ b/.idea/libraries/Maven__org_codehaus_plexus_plexus_compiler_javac_2_10_0.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_codehaus_plexus_plexus_compiler_manager_2_10_0.xml b/.idea/libraries/Maven__org_codehaus_plexus_plexus_compiler_manager_2_10_0.xml new file mode 100644 index 0000000..df1a190 --- /dev/null +++ b/.idea/libraries/Maven__org_codehaus_plexus_plexus_compiler_manager_2_10_0.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_codehaus_plexus_plexus_component_annotations_1_5_5.xml b/.idea/libraries/Maven__org_codehaus_plexus_plexus_component_annotations_1_5_5.xml new file mode 100644 index 0000000..9292317 --- /dev/null +++ b/.idea/libraries/Maven__org_codehaus_plexus_plexus_component_annotations_1_5_5.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_codehaus_plexus_plexus_java_1_1_1.xml b/.idea/libraries/Maven__org_codehaus_plexus_plexus_java_1_1_1.xml new file mode 100644 index 0000000..808e3f3 --- /dev/null +++ b/.idea/libraries/Maven__org_codehaus_plexus_plexus_java_1_1_1.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_codehaus_plexus_plexus_utils_1_5_8.xml b/.idea/libraries/Maven__org_codehaus_plexus_plexus_utils_1_5_8.xml new file mode 100644 index 0000000..d337042 --- /dev/null +++ b/.idea/libraries/Maven__org_codehaus_plexus_plexus_utils_1_5_8.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_ow2_asm_asm_9_2.xml b/.idea/libraries/Maven__org_ow2_asm_asm_9_2.xml new file mode 100644 index 0000000..3f9b2da --- /dev/null +++ b/.idea/libraries/Maven__org_ow2_asm_asm_9_2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_projectlombok_lombok_1_18_22.xml b/.idea/libraries/Maven__org_projectlombok_lombok_1_18_22.xml new file mode 100644 index 0000000..72b596e --- /dev/null +++ b/.idea/libraries/Maven__org_projectlombok_lombok_1_18_22.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_projectlombok_lombok_maven_plugin_1_18_20_0.xml b/.idea/libraries/Maven__org_projectlombok_lombok_maven_plugin_1_18_20_0.xml new file mode 100644 index 0000000..c7c939a --- /dev/null +++ b/.idea/libraries/Maven__org_projectlombok_lombok_maven_plugin_1_18_20_0.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_sonatype_plexus_plexus_build_api_0_0_7.xml b/.idea/libraries/Maven__org_sonatype_plexus_plexus_build_api_0_0_7.xml new file mode 100644 index 0000000..9302938 --- /dev/null +++ b/.idea/libraries/Maven__org_sonatype_plexus_plexus_build_api_0_0_7.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..1bfe194 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,13 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..07fb49c --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/TicketTest.iml b/TicketTest.iml new file mode 100644 index 0000000..d1109fe --- /dev/null +++ b/TicketTest.iml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..768ac5d --- /dev/null +++ b/pom.xml @@ -0,0 +1,49 @@ + + 4.0.0 + + net.horizoncode.tickettest + tickettest + 1.0.0 + jar + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.10.0 + + 1.8 + 1.8 + + + + + + + org.projectlombok + lombok + 1.18.22 + + + org.projectlombok + lombok-maven-plugin + 1.18.20.0 + + + org.apache.maven.plugins + maven-compiler-plugin + 3.10.0 + + + commons-io + commons-io + 2.11.0 + + + com.google.guava + guava + 31.0.1-jre + + + \ No newline at end of file diff --git a/src/net/horizoncode/tickettest/TicketTest.java b/src/net/horizoncode/tickettest/TicketTest.java new file mode 100644 index 0000000..7016ec0 --- /dev/null +++ b/src/net/horizoncode/tickettest/TicketTest.java @@ -0,0 +1,33 @@ +package net.horizoncode.tickettest; + +import lombok.Getter; +import net.horizoncode.tickettest.manager.Ticket; +import net.horizoncode.tickettest.manager.TicketCallback; +import net.horizoncode.tickettest.manager.TicketManager; + +import java.util.stream.IntStream; + +public class TicketTest { + + @Getter private final TicketManager ticketManager; + + public static void main(String[] args) { + new TicketTest(); + } + + public TicketTest() { + this.ticketManager = TicketManager.builder().ticketLifespan(5000).maxTicketsExisting(2).build(); + + IntStream.range(0, 10) + .forEach( + (value) -> + getTicketManager() + .addTicket( + () -> { + System.out.println("i expired oof"); + }) + .use()); + + while (true) {} + } +} diff --git a/src/net/horizoncode/tickettest/manager/Ticket.java b/src/net/horizoncode/tickettest/manager/Ticket.java new file mode 100644 index 0000000..2aad54c --- /dev/null +++ b/src/net/horizoncode/tickettest/manager/Ticket.java @@ -0,0 +1,56 @@ +package net.horizoncode.tickettest.manager; + +import lombok.Builder; +import lombok.Getter; +import lombok.Setter; + +import java.util.UUID; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; + +@Builder( + toBuilder = true, + builderClassName = "InternalTicketBuilder", + builderMethodName = "internalBuilder") +public class Ticket extends CompletableFuture { + @Getter private UUID ticketID; + @Getter private Long timeTicketCreated; + private TicketManager ticketManager; + @Getter @Setter private Runnable onExpire; + + void init() { + ticketID = UUID.randomUUID(); + } + + public void use() { + ticketManager.getTicketSet().add(this); + ticketManager + .getService() + .submit( + () -> { + TimeUnit.MILLISECONDS.sleep(ticketManager.getTicketLifespan()); + System.out.println("ticket with UUID: " + getTicketID() + " expired."); + ticketManager.getTicketSet().remove(this); + getOnExpire().run(); + return null; + }); + } + + public static Ticket.Builder builder() { + return new Ticket.Builder(); + } + + public static class Builder extends InternalTicketBuilder { + + Builder() { + super(); + } + + @Override + public Ticket build() { + Ticket ticket = super.build(); + ticket.init(); + return ticket; + } + } +} diff --git a/src/net/horizoncode/tickettest/manager/TicketCallback.java b/src/net/horizoncode/tickettest/manager/TicketCallback.java new file mode 100644 index 0000000..9e71596 --- /dev/null +++ b/src/net/horizoncode/tickettest/manager/TicketCallback.java @@ -0,0 +1,8 @@ +package net.horizoncode.tickettest.manager; + +public class TicketCallback{ + public Ticket call(Ticket ticket) { + System.out.println("Ticket " + ticket.getTicketID() + " expired."); + return ticket; + } +} diff --git a/src/net/horizoncode/tickettest/manager/TicketManager.java b/src/net/horizoncode/tickettest/manager/TicketManager.java new file mode 100644 index 0000000..09606e9 --- /dev/null +++ b/src/net/horizoncode/tickettest/manager/TicketManager.java @@ -0,0 +1,76 @@ +package net.horizoncode.tickettest.manager; + +import com.google.common.util.concurrent.ListenableFuture; +import com.google.common.util.concurrent.ListeningExecutorService; +import com.google.common.util.concurrent.MoreExecutors; +import lombok.Builder; +import lombok.Getter; + +import java.util.HashSet; +import java.util.Set; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; + +@Builder( + toBuilder = true, + builderClassName = "InternalTicketManagerBuilder", + builderMethodName = "internalBuilder") +@Getter +public class TicketManager { + + private ListeningExecutorService service; + private Set ticketSet; + private int maxTicketsExisting; + private long ticketLifespan; + + public Ticket addTicket(Runnable onExpire) { + ListenableFuture asyncTask = + service.submit( + () -> { + if (waitForFreeTicket()) + System.out.println("max amount of tickets reached, waiting..."); + + while (waitForFreeTicket()) { + TimeUnit.MILLISECONDS.sleep(1); + } + Ticket t = + Ticket.builder().timeTicketCreated(ticketLifespan).onExpire(onExpire).ticketManager(this).build(); + System.out.println("Got ticket with UUID: " + t.getTicketID()); + return t; + }); + try { + return asyncTask.get(); + } catch (InterruptedException | ExecutionException e) { + e.printStackTrace(); + return null; + } + } + + private boolean waitForFreeTicket() { + return ticketSet.size() >= maxTicketsExisting; + } + + void init() { + service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10)); + ticketSet = new HashSet<>(); + } + + public static Builder builder() { + return new Builder(); + } + + public static class Builder extends InternalTicketManagerBuilder { + + Builder() { + super(); + } + + @Override + public TicketManager build() { + TicketManager ticketManager = super.build(); + ticketManager.init(); + return ticketManager; + } + } +} diff --git a/target/classes/net/horizoncode/tickettest/TicketTest.class b/target/classes/net/horizoncode/tickettest/TicketTest.class new file mode 100644 index 0000000..b4e1de4 Binary files /dev/null and b/target/classes/net/horizoncode/tickettest/TicketTest.class differ diff --git a/target/classes/net/horizoncode/tickettest/manager/Ticket$Builder.class b/target/classes/net/horizoncode/tickettest/manager/Ticket$Builder.class new file mode 100644 index 0000000..159d4b4 Binary files /dev/null and b/target/classes/net/horizoncode/tickettest/manager/Ticket$Builder.class differ diff --git a/target/classes/net/horizoncode/tickettest/manager/Ticket$InternalTicketBuilder.class b/target/classes/net/horizoncode/tickettest/manager/Ticket$InternalTicketBuilder.class new file mode 100644 index 0000000..991f2c3 Binary files /dev/null and b/target/classes/net/horizoncode/tickettest/manager/Ticket$InternalTicketBuilder.class differ diff --git a/target/classes/net/horizoncode/tickettest/manager/Ticket.class b/target/classes/net/horizoncode/tickettest/manager/Ticket.class new file mode 100644 index 0000000..12f86f1 Binary files /dev/null and b/target/classes/net/horizoncode/tickettest/manager/Ticket.class differ diff --git a/target/classes/net/horizoncode/tickettest/manager/TicketCallback.class b/target/classes/net/horizoncode/tickettest/manager/TicketCallback.class new file mode 100644 index 0000000..8be9555 Binary files /dev/null and b/target/classes/net/horizoncode/tickettest/manager/TicketCallback.class differ diff --git a/target/classes/net/horizoncode/tickettest/manager/TicketManager$Builder.class b/target/classes/net/horizoncode/tickettest/manager/TicketManager$Builder.class new file mode 100644 index 0000000..78f4ea8 Binary files /dev/null and b/target/classes/net/horizoncode/tickettest/manager/TicketManager$Builder.class differ diff --git a/target/classes/net/horizoncode/tickettest/manager/TicketManager$InternalTicketManagerBuilder.class b/target/classes/net/horizoncode/tickettest/manager/TicketManager$InternalTicketManagerBuilder.class new file mode 100644 index 0000000..308c10c Binary files /dev/null and b/target/classes/net/horizoncode/tickettest/manager/TicketManager$InternalTicketManagerBuilder.class differ diff --git a/target/classes/net/horizoncode/tickettest/manager/TicketManager.class b/target/classes/net/horizoncode/tickettest/manager/TicketManager.class new file mode 100644 index 0000000..447e286 Binary files /dev/null and b/target/classes/net/horizoncode/tickettest/manager/TicketManager.class differ