Mapping strings from InputStream to ExecutorService










4














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));









share|improve this question























  • Did you try to improve the code by yourself? Where did you reached so far?
    – ETO
    Nov 12 '18 at 20:42










  • Yes, I did. But without success full results.
    – Hans van Kessels
    Nov 12 '18 at 20:43










  • I think there is a better place for such questions codereview.stackexchange.com
    – uli
    Nov 12 '18 at 20:43










  • Please post your intermediary code.
    – ETO
    Nov 12 '18 at 20:44










  • upadtedc the description
    – Hans van Kessels
    Nov 12 '18 at 20:45















4














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));









share|improve this question























  • Did you try to improve the code by yourself? Where did you reached so far?
    – ETO
    Nov 12 '18 at 20:42










  • Yes, I did. But without success full results.
    – Hans van Kessels
    Nov 12 '18 at 20:43










  • I think there is a better place for such questions codereview.stackexchange.com
    – uli
    Nov 12 '18 at 20:43










  • Please post your intermediary code.
    – ETO
    Nov 12 '18 at 20:44










  • upadtedc the description
    – Hans van Kessels
    Nov 12 '18 at 20:45













4












4








4


1





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));









share|improve this question















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));






java java-8 java-stream inputstream iot






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 21:31









ETO

1,866422




1,866422










asked Nov 12 '18 at 20:39









Hans van Kessels

8812




8812











  • Did you try to improve the code by yourself? Where did you reached so far?
    – ETO
    Nov 12 '18 at 20:42










  • Yes, I did. But without success full results.
    – Hans van Kessels
    Nov 12 '18 at 20:43










  • I think there is a better place for such questions codereview.stackexchange.com
    – uli
    Nov 12 '18 at 20:43










  • Please post your intermediary code.
    – ETO
    Nov 12 '18 at 20:44










  • upadtedc the description
    – Hans van Kessels
    Nov 12 '18 at 20:45
















  • Did you try to improve the code by yourself? Where did you reached so far?
    – ETO
    Nov 12 '18 at 20:42










  • Yes, I did. But without success full results.
    – Hans van Kessels
    Nov 12 '18 at 20:43










  • I think there is a better place for such questions codereview.stackexchange.com
    – uli
    Nov 12 '18 at 20:43










  • Please post your intermediary code.
    – ETO
    Nov 12 '18 at 20:44










  • upadtedc the description
    – Hans van Kessels
    Nov 12 '18 at 20:45















Did you try to improve the code by yourself? Where did you reached so far?
– ETO
Nov 12 '18 at 20:42




Did you try to improve the code by yourself? Where did you reached so far?
– ETO
Nov 12 '18 at 20:42












Yes, I did. But without success full results.
– Hans van Kessels
Nov 12 '18 at 20:43




Yes, I did. But without success full results.
– Hans van Kessels
Nov 12 '18 at 20:43












I think there is a better place for such questions codereview.stackexchange.com
– uli
Nov 12 '18 at 20:43




I think there is a better place for such questions codereview.stackexchange.com
– uli
Nov 12 '18 at 20:43












Please post your intermediary code.
– ETO
Nov 12 '18 at 20:44




Please post your intermediary code.
– ETO
Nov 12 '18 at 20:44












upadtedc the description
– Hans van Kessels
Nov 12 '18 at 20:45




upadtedc the description
– Hans van Kessels
Nov 12 '18 at 20:45












1 Answer
1






active

oldest

votes


















4














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])
));






share|improve this answer






















  • Okay, thanks. But how can I submit the commands to the executor? Do I need to iterate over map ?
    – Hans van Kessels
    Nov 12 '18 at 20:53










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269762%2fmapping-strings-from-inputstream-to-executorservice%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









4














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])
));






share|improve this answer






















  • Okay, thanks. But how can I submit the commands to the executor? Do I need to iterate over map ?
    – Hans van Kessels
    Nov 12 '18 at 20:53















4














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])
));






share|improve this answer






















  • Okay, thanks. But how can I submit the commands to the executor? Do I need to iterate over map ?
    – Hans van Kessels
    Nov 12 '18 at 20:53













4












4








4






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])
));






share|improve this answer














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])
));







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 '18 at 21:08

























answered Nov 12 '18 at 20:51









ETO

1,866422




1,866422











  • Okay, thanks. But how can I submit the commands to the executor? Do I need to iterate over map ?
    – Hans van Kessels
    Nov 12 '18 at 20:53
















  • Okay, thanks. But how can I submit the commands to the executor? Do I need to iterate over map ?
    – Hans van Kessels
    Nov 12 '18 at 20:53















Okay, thanks. But how can I submit the commands to the executor? Do I need to iterate over map ?
– Hans van Kessels
Nov 12 '18 at 20:53




Okay, thanks. But how can I submit the commands to the executor? Do I need to iterate over map ?
– Hans van Kessels
Nov 12 '18 at 20:53

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269762%2fmapping-strings-from-inputstream-to-executorservice%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3