import java.util.ArrayDeque; import java.util.List; import java.util.Queue; import java.util.concurrent.atomic.AtomicInteger; /// One participant's side of the world: scripted input, a lookup table, a /// counter, and tagged stdout. Same responsibilities as the Rust `Host`s in /// `compare/`, minus `Sleep`, which belongs to the scheduler. final class Host { final String tag; private final Queue script; private final AtomicInteger count = new AtomicInteger(); Host(String tag, List script) { this.tag = tag; this.script = new ArrayDeque<>(script); } String readLine() { var line = script.poll(); if (line == null) { line = "quit"; } write("> " + line); return line; } static String greetingFor(String name) { return switch (name) { case "Monde", "Amélie" -> "Bonjour"; case "Welt", "Hans" -> "Hallo"; case "Mundo", "Inés" -> "Hola"; default -> "Hello"; }; } int count() { return count.incrementAndGet(); } void write(String text) { System.out.printf("[%s @ %s] %s%n", tag, Thread.currentThread().getName(), text); } }