Channel Directions in Fortress

Channel Directions in Java can be simulated using BlockingQueue. While Java doesn’t have built-in channel direction specifications, we can achieve similar behavior using interfaces.

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class ChannelDirections {

    // This ping method only accepts a queue for sending values.
    // It would be a compile-time error to try to receive from this queue.
    public static void ping(BlockingQueue<String> pings, String msg) throws InterruptedException {
        pings.put(msg);
    }

    // The pong method accepts one queue for receives (pings) and a second for sends (pongs).
    public static void pong(BlockingQueue<String> pings, BlockingQueue<String> pongs) throws InterruptedException {
        String msg = pings.take();
        pongs.put(msg);
    }

    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<String> pings = new LinkedBlockingQueue<>(1);
        BlockingQueue<String> pongs = new LinkedBlockingQueue<>(1);

        ping(pings, "passed message");
        pong(pings, pongs);
        System.out.println(pongs.take());
    }
}

In this Java example, we use BlockingQueue to simulate channels. The LinkedBlockingQueue with a capacity of 1 is used to mimic the behavior of unbuffered channels in Go.

The ping method only puts (sends) a message to the queue, while the pong method takes (receives) from one queue and puts (sends) to another.

In the main method, we create two queues, send a message through ping, pass it through pong, and finally print the received message.

To run this program:

$ javac ChannelDirections.java
$ java ChannelDirections
passed message

This example demonstrates how to implement unidirectional channel-like behavior in Java using BlockingQueue. While it’s not as straightforward as Go’s channel directions, it provides similar functionality in terms of type-safety and direction specification.