Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add a workshop example for Multi::split #1370

Merged
merged 1 commit into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
///usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS io.smallrye.reactive:mutiny:2.4.0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering how you plan to keep this version in sync?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(and the WIP CI branch has been rebased to call it: jponge@7511a87)

package _07_advanced_streaming;

import static _07_advanced_streaming._01_Multi_Split.Country.*;

import java.util.List;

import io.smallrye.mutiny.Multi;

public class _01_Multi_Split {

static class TemperatureRecord {
final Country country;
final String city;
final long timestamp;
final double value;

TemperatureRecord(Country country, String city, long timestamp, double value) {
this.country = country;
this.city = city;
this.timestamp = timestamp;
this.value = value;
}

@Override
public String toString() {
return "TemperatureRecord{" +
"country=" + country +
", city='" + city + '\'' +
", timestamp=" + timestamp +
", value=" + value +
'}';
}
}

enum Country {
FRANCE,
UK,
AUSTRALIA
}

public static void main(String[] args) {
System.out.println("⚡️ Multi split operator");

var data = List.of(
new TemperatureRecord(FRANCE, "Tassin-La-Demi-Lune", System.nanoTime(), 28.0),
new TemperatureRecord(FRANCE, "Clermont-Ferrand", System.nanoTime(), 27.0),
new TemperatureRecord(FRANCE, "Nevers", System.nanoTime(), 27.0),
new TemperatureRecord(FRANCE, "Aubière", System.nanoTime(), 28.0),
new TemperatureRecord(AUSTRALIA, "Sydney", System.nanoTime(), 16.0),
new TemperatureRecord(FRANCE, "Lyon", System.nanoTime(), 29.0),
new TemperatureRecord(AUSTRALIA, "Kensington", System.nanoTime(), 16.0),
new TemperatureRecord(UK, "Newcastle", System.nanoTime(), 13.0),
new TemperatureRecord(AUSTRALIA, "Coogee", System.nanoTime(), 16.0),
new TemperatureRecord(UK, "Bexhill", System.nanoTime(), 22.0));

var splitter = Multi.createFrom().iterable(data)
.split(Country.class, record -> record.country);

splitter.get(FRANCE)
.subscribe().with(
record -> System.out.println("🇫🇷 => " + record),
Throwable::printStackTrace,
() -> System.out.println("✅ Done with France"));

splitter.get(AUSTRALIA)
.subscribe().with(
record -> System.out.println("🇦🇺🦘 => " + record),
Throwable::printStackTrace,
() -> System.out.println("✅ Done with Australia"));

splitter.get(UK)
.subscribe().with(
record -> System.out.println("🇬🇧 => " + record),
Throwable::printStackTrace,
() -> System.out.println("✅ Done with the UK"));

}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
///usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS io.smallrye.reactive:mutiny:2.4.0
package _07_misc;
package _08_misc;

import java.util.concurrent.ThreadLocalRandom;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
///usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS io.smallrye.reactive:mutiny:2.4.0
package _07_misc;
package _08_misc;

import io.smallrye.mutiny.Multi;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
///usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS io.smallrye.reactive:mutiny:2.4.0
package _07_misc;
package _08_misc;

import java.util.List;

Expand Down