diff --git a/README.adoc b/README.adoc index 4730474..fbe87db 100644 --- a/README.adoc +++ b/README.adoc @@ -3532,7 +3532,7 @@ public class Day093 { public static void main(String[] args) { List myList = getList(); - // If myList is null, a NullPointerException will be thrown +// If myList is null, a NullPointerException will be thrown for (String s : myList) { System.out.println(s); } @@ -3554,4 +3554,62 @@ public class Day093 { return Instant.now().get(ChronoField.MILLI_OF_SECOND) % 2 == 0; } } +---- + +== Day 94 - Using Reactive Streams from Java 9 +[source,java] +---- +package com.thegreatapi.ahundreddaysofjava.day094; + +import java.util.List; +import java.util.concurrent.Flow; +import java.util.concurrent.SubmissionPublisher; + +public class Day094 { + + public static void main(String[] args) { + List judasPriestMembers = List.of( + "Rob Halford", + "K.K. Downing", + "Glenn Tipton", + "Ian Hill", + "Scott Travis" + ); + + var subscriber = new MySubscriber(); + + try (var publisher = new SubmissionPublisher()) { + publisher.subscribe(subscriber); + judasPriestMembers.forEach(publisher::submit); + } + } + + static class MySubscriber implements Flow.Subscriber { + + private Flow.Subscription subscription; + + @Override + public void onSubscribe(Flow.Subscription subscription) { + System.out.println("Started subscription"); + this.subscription = subscription; + subscription.request(1); + } + + @Override + public void onNext(String item) { + System.out.println(item); + subscription.request(1); + } + + @Override + public void onError(Throwable throwable) { + System.err.println("Error: " + throwable); + } + + @Override + public void onComplete() { + System.out.println("Subscription complete"); + } + } +} ---- \ No newline at end of file diff --git a/days/day093/src/main/java/com/thegreatapi/ahundreddaysofjava/day093/Day093.java b/days/day093/src/main/java/com/thegreatapi/ahundreddaysofjava/day093/Day093.java index e97ac82..49c64f9 100644 --- a/days/day093/src/main/java/com/thegreatapi/ahundreddaysofjava/day093/Day093.java +++ b/days/day093/src/main/java/com/thegreatapi/ahundreddaysofjava/day093/Day093.java @@ -9,7 +9,7 @@ public class Day093 { public static void main(String[] args) { List myList = getList(); - // If myList is null, a NullPointerException will be thrown +// If myList is null, a NullPointerException will be thrown for (String s : myList) { System.out.println(s); } diff --git a/days/day094/pom.xml b/days/day094/pom.xml new file mode 100644 index 0000000..380bfa4 --- /dev/null +++ b/days/day094/pom.xml @@ -0,0 +1,19 @@ + + + + days + com.thegreatapi.100daysofjava + 1.0-SNAPSHOT + + 4.0.0 + + day094 + + + 16 + 16 + + + \ No newline at end of file diff --git a/days/day094/src/main/java/com/thegreatapi/ahundreddaysofjava/day094/Day094.java b/days/day094/src/main/java/com/thegreatapi/ahundreddaysofjava/day094/Day094.java new file mode 100644 index 0000000..b8e4ad4 --- /dev/null +++ b/days/day094/src/main/java/com/thegreatapi/ahundreddaysofjava/day094/Day094.java @@ -0,0 +1,53 @@ +package com.thegreatapi.ahundreddaysofjava.day094; + +import java.util.List; +import java.util.concurrent.Flow; +import java.util.concurrent.SubmissionPublisher; + +public class Day094 { + + public static void main(String[] args) { + List judasPriestMembers = List.of( + "Rob Halford", + "K.K. Downing", + "Glenn Tipton", + "Ian Hill", + "Scott Travis" + ); + + var subscriber = new MySubscriber(); + + try (var publisher = new SubmissionPublisher()) { + publisher.subscribe(subscriber); + judasPriestMembers.forEach(publisher::submit); + } + } + + static class MySubscriber implements Flow.Subscriber { + + private Flow.Subscription subscription; + + @Override + public void onSubscribe(Flow.Subscription subscription) { + System.out.println("Started subscription"); + this.subscription = subscription; + subscription.request(1); + } + + @Override + public void onNext(String item) { + System.out.println(item); + subscription.request(1); + } + + @Override + public void onError(Throwable throwable) { + System.err.println("Error: " + throwable); + } + + @Override + public void onComplete() { + System.out.println("Subscription complete"); + } + } +} \ No newline at end of file diff --git a/days/pom.xml b/days/pom.xml index 83c2b04..cc59669 100644 --- a/days/pom.xml +++ b/days/pom.xml @@ -104,6 +104,7 @@ day091 day092 day093 + day094 \ No newline at end of file