Skip to content

Commit 90f5b38

Browse files
authored
refactor: Reactor pattern - Polish Javadoc and Using 'Map.computeIfAbsent' instead of 'if' statement (#2805)
* refactor: using computeIfAbsent instead of if check. * docs: Polish Javadoc and comments
1 parent aaa44fd commit 90f5b38

File tree

2 files changed

+5
-9
lines changed

2 files changed

+5
-9
lines changed

Diff for: reactor/src/main/java/com/iluwatar/reactor/framework/AbstractNioChannel.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,7 @@ public void write(Object data, SelectionKey key) {
162162
var pendingWrites = this.channelToPendingWrites.get(key.channel());
163163
if (pendingWrites == null) {
164164
synchronized (this.channelToPendingWrites) {
165-
pendingWrites = this.channelToPendingWrites.get(key.channel());
166-
if (pendingWrites == null) {
167-
pendingWrites = new ConcurrentLinkedQueue<>();
168-
this.channelToPendingWrites.put(key.channel(), pendingWrites);
169-
}
165+
pendingWrites = this.channelToPendingWrites.computeIfAbsent(key.channel(), k -> new ConcurrentLinkedQueue<>());
170166
}
171167
}
172168
pendingWrites.add(data);

Diff for: reactor/src/main/java/com/iluwatar/reactor/framework/NioReactor.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
/**
3939
* This class acts as Synchronous Event De-multiplexer and Initiation Dispatcher of Reactor pattern.
40-
* Multiple handles i.e. {@link AbstractNioChannel}s can be registered to the reactor and it blocks
40+
* Multiple handles i.e. {@link AbstractNioChannel}s can be registered to the reactor, and it blocks
4141
* for events from all these handles. Whenever an event occurs on any of the registered handles, it
4242
* synchronously de-multiplexes the event which can be any of read, write or accept, and dispatches
4343
* the event to the appropriate {@link ChannelHandler} using the {@link Dispatcher}.
@@ -46,7 +46,7 @@
4646
* #start()} method. {@link NioReactor} uses {@link Selector} for realizing Synchronous Event
4747
* De-multiplexing.
4848
*
49-
* <p>NOTE: This is one of the ways to implement NIO reactor and it does not take care of all
49+
* <p>NOTE: This is one of the ways to implement NIO reactor, and it does not take care of all
5050
* possible edge cases which are required in a real application. This implementation is meant to
5151
* demonstrate the fundamental concepts that lie behind Reactor pattern.
5252
*/
@@ -212,7 +212,7 @@ private void onChannelAcceptable(SelectionKey key) throws IOException {
212212

213213
/**
214214
* Queues the change of operations request of a channel, which will change the interested
215-
* operations of the channel sometime in future.
215+
* operations of the channel sometime in the future.
216216
*
217217
* <p>This is a non-blocking method and does not guarantee that the operations have changed when
218218
* this method returns.
@@ -228,7 +228,7 @@ public void changeOps(SelectionKey key, int interestedOps) {
228228
/**
229229
* A command that changes the interested operations of the key provided.
230230
*/
231-
class ChangeKeyOpsCommand implements Runnable {
231+
static class ChangeKeyOpsCommand implements Runnable {
232232
private final SelectionKey key;
233233
private final int interestedOps;
234234

0 commit comments

Comments
 (0)