Skip to content

Commit

Permalink
Replace unnecessary usage of AtomicReference with Optional for storin…
Browse files Browse the repository at this point in the history
…g command execution context.
  • Loading branch information
pferraro committed Oct 22, 2016
1 parent 039cb2d commit 1641459
Showing 1 changed file with 4 additions and 4 deletions.
Expand Up @@ -77,7 +77,7 @@
*/
public class ChannelCommandDispatcherFactory implements CommandDispatcherFactory, RequestHandler, AutoCloseable, Group, MembershipListener {

final Map<Object, AtomicReference<Object>> contexts = new ConcurrentHashMap<>();
final Map<Object, Optional<Object>> contexts = new ConcurrentHashMap<>();
final MarshallingContext marshallingContext;

private final ExecutorService viewExecutor = Executors.newSingleThreadExecutor(createThreadFactory());
Expand Down Expand Up @@ -130,15 +130,15 @@ public Object handle(Message message) throws Exception {
try (Unmarshaller unmarshaller = this.marshallingContext.createUnmarshaller(version)) {
unmarshaller.start(Marshalling.createByteInput(input));
Object clientId = unmarshaller.readObject();
AtomicReference<Object> context = this.contexts.get(clientId);
Optional<Object> context = this.contexts.get(clientId);
if (context == null) return NoSuchService.INSTANCE;
@SuppressWarnings("unchecked")
Command<Object, Object> command = (Command<Object, Object>) unmarshaller.readObject();
Callable<Optional<Object>> task = new Callable<Optional<Object>>() {
@Override
public Optional<Object> call() throws Exception {
// Wrap in an Optional, since command execution might return null
return Optional.ofNullable(command.execute(context.get()));
return Optional.ofNullable(command.execute(context.orElse(null)));
}
};
return this.executor.execute(task).orElse(Optional.of(NoSuchService.INSTANCE)).orElse(null);
Expand Down Expand Up @@ -170,7 +170,7 @@ public <R> byte[] marshal(Command<R, C> command) throws IOException {
}
}
};
this.contexts.put(id, new AtomicReference<Object>(context));
this.contexts.put(id, Optional.ofNullable(context));
final CommandDispatcher<C> localDispatcher = new LocalCommandDispatcher<>(this.getLocalNode(), context);
return new ChannelCommandDispatcher<C>(this.dispatcher, marshaller, this.nodeFactory, this.timeout, localDispatcher) {
@Override
Expand Down

0 comments on commit 1641459

Please sign in to comment.