Mapping strings from InputStream to ExecutorService
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
|
show 2 more comments
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
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
|
show 2 more comments
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
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
java java-8 java-stream inputstream iot
edited Nov 12 '18 at 21:31
ETO
2,6281629
2,6281629
asked Nov 12 '18 at 20:39
Hans van KesselsHans van Kessels
7013
7013
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
|
show 2 more comments
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
|
show 2 more comments
1 Answer
1
active
oldest
votes
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])
));
Okay, thanks. But how can I submit the commands to the executor? Do I need to iterate overmap
?
– Hans van Kessels
Nov 12 '18 at 20:53
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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])
));
Okay, thanks. But how can I submit the commands to the executor? Do I need to iterate overmap
?
– Hans van Kessels
Nov 12 '18 at 20:53
add a comment |
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])
));
Okay, thanks. But how can I submit the commands to the executor? Do I need to iterate overmap
?
– Hans van Kessels
Nov 12 '18 at 20:53
add a comment |
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])
));
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])
));
edited Nov 12 '18 at 21:08
answered Nov 12 '18 at 20:51
ETOETO
2,6281629
2,6281629
Okay, thanks. But how can I submit the commands to the executor? Do I need to iterate overmap
?
– Hans van Kessels
Nov 12 '18 at 20:53
add a comment |
Okay, thanks. But how can I submit the commands to the executor? Do I need to iterate overmap
?
– 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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