Mapping strings from InputStream to ExecutorService

Hans van Kessels :

I'm implementing my DIY IoT. I have a central node (server) which receives the commands from different sources and executes them.

Input format:

<DEVICE_NAME>_<COMMAND> <DEVICE_NAME>_<COMMAND>  <DEVICE_NAME>_<COMMAND>
<DEVICE_NAME>_<COMMAND>  <DEVICE_NAME>_<COMMAND> 
<DEVICE_NAME>_<COMMAND>  <DEVICE_NAME>_<COMMAND>  <DEVICE_NAME>_<COMMAND> 

Each line may contain multiple commands.

I have implemented a command executor server which takes the commands from the session as an InputStream. Then I split the data and process it:

private Device c0 = // Device constructor
private Device c1 = // Device constructor
private Device c2 = // Device constructor
private Device c3 = // Device constructor

private ExecutorService executor = Executors.newFixedThreadPool(3);

public void onConnection(InputStream in)     
    InputStreamReader isr = new InputStreamReader(in);
    LineNumberReader reader = new LineNumberReader(isr);

    String line = null;
    while ((line = reader.readLine()) != null) {
        String[] strings = line.split(",");
        for (String raw : strings) {
            String command = raw.substring(0, 3);
            if (raw.startsWith("C0_")) {
                executor.submit(() -> c0.execute(command));
            } else if (raw.startsWith("C1_")) {
                executor.submit(() -> c1.execute(command));
            } else if (raw.startsWith("C2_")) {
                executor.submit(() -> c2.execute(command));
            } else if (raw.startsWith("C3_")) {
                executor.submit(() -> c3.execute(command));
            }
        }
    }
}

I understand the code looks ugly. Do you have any improvement ideas? Maybe I could use Steam API? Any hints/advices are appreciated.

UPDATE

I've tried to clean the code a bit by submitting the task only once, but the compailer sais the device must be final or effectively final so this does not work:

String command = raw.substring(0, 3);
Device device;
if (raw.startsWith("C0_")) {
    device = c0;
} else if (raw.startsWith("C1_")) {
    device = c1;
} else if (raw.startsWith("C2_")) {
    device = c2;
} else if (raw.startsWith("C3_")) {
    device = c3;
}
executor.submit(() -> device.execute(command));
ETO :

You can map your commands like this:

InputStreamReader isr = new InputStreamReader(is);
BufferedReader buffReader = new BufferedReader(isr);

Map<String, List<String>> map = buffReader.lines()
            .map(s -> s.split(" "))
            .flatMap(Arrays::stream)
            .map(s -> s.split("(?<=_)", 2))
            .collect(groupingBy(p -> p[0], mapping(p -> p[1], toList())));

Update

Actually you can combine both mapping and submitting:

// Register your devices
Map<String, Device> devices = new HashMap<>();
devices.put("c0", c0);
devices.put("c1", c1);
devices.put("c2", c2);
devices.put("c3", c3);
...

public void onConnection(InputStream in) {
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader buffReader = new BufferedReader(isr);
    buffReader.lines()
              .parallel()
              .map(s -> s.split(" "))
              .flatMap(Arrays::stream)
              .map(s -> s.split("(?<=_)", 2))
              .forEach(p -> executor.submit(
                      () -> devices.get(p[0]).execute(p[1])
               ));
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=95720&siteId=1