Refactor FCP dialog
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Thu, 15 Oct 2015 19:20:25 +0000 (21:20 +0200)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Thu, 15 Oct 2015 19:20:25 +0000 (21:20 +0200)
22 files changed:
src/main/java/net/pterodactylus/fcp/quelaton/AddPeerCommandImpl.java
src/main/java/net/pterodactylus/fcp/quelaton/ClientGetCommandImpl.java
src/main/java/net/pterodactylus/fcp/quelaton/ClientHelloImpl.java
src/main/java/net/pterodactylus/fcp/quelaton/ClientPutCommandImpl.java
src/main/java/net/pterodactylus/fcp/quelaton/FcpDialog.java
src/main/java/net/pterodactylus/fcp/quelaton/GenerateKeypairCommandImpl.java
src/main/java/net/pterodactylus/fcp/quelaton/GetConfigCommandImpl.java
src/main/java/net/pterodactylus/fcp/quelaton/GetNodeCommandImpl.java
src/main/java/net/pterodactylus/fcp/quelaton/GetPluginInfoCommandImpl.java
src/main/java/net/pterodactylus/fcp/quelaton/ListPeerCommandImpl.java
src/main/java/net/pterodactylus/fcp/quelaton/ListPeerNotesCommandImpl.java
src/main/java/net/pterodactylus/fcp/quelaton/ListPeersCommandImpl.java
src/main/java/net/pterodactylus/fcp/quelaton/LoadPluginCommandImpl.java
src/main/java/net/pterodactylus/fcp/quelaton/ModifyConfigCommandImpl.java
src/main/java/net/pterodactylus/fcp/quelaton/ModifyPeerCommandImpl.java
src/main/java/net/pterodactylus/fcp/quelaton/ModifyPeerNoteCommandImpl.java
src/main/java/net/pterodactylus/fcp/quelaton/ReloadPluginCommandImpl.java
src/main/java/net/pterodactylus/fcp/quelaton/RemovePeerCommandImpl.java
src/main/java/net/pterodactylus/fcp/quelaton/RemovePluginCommandImpl.java
src/main/java/net/pterodactylus/fcp/quelaton/SubscribeUskCommandImpl.java
src/main/java/net/pterodactylus/fcp/quelaton/UnsubscribeUskCommandImpl.java
src/test/java/net/pterodactylus/fcp/quelaton/FcpDialogTest.java

index b34d2ba..9dca0dc 100644 (file)
@@ -6,7 +6,6 @@ import java.net.URL;
 import java.util.Optional;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
-import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicReference;
 import java.util.function.Supplier;
 
@@ -77,32 +76,18 @@ public class AddPeerCommandImpl implements AddPeerCommand {
 
        private class AddPeerDialog extends FcpDialog<Optional<Peer>> {
 
-               private final AtomicBoolean finished = new AtomicBoolean();
-               private final AtomicReference<Peer> peer = new AtomicReference<>();
-
                public AddPeerDialog() throws IOException {
-                       super(threadPool, connectionSupplier.get());
-               }
-
-               @Override
-               protected boolean isFinished() {
-                       return finished.get();
-               }
-
-               @Override
-               protected Optional<Peer> getResult() {
-                       return Optional.ofNullable(peer.get());
+                       super(threadPool, connectionSupplier.get(), Optional.empty());
                }
 
                @Override
                protected void consumePeer(Peer peer) {
-                       this.peer.set(peer);
-                       finished.set(true);
+                       setResult(Optional.ofNullable(peer));
                }
 
                @Override
                protected void consumeProtocolError(ProtocolError protocolError) {
-                       finished.set(true);
+                       finish();
                }
 
        }
index d01b82e..7908f5c 100644 (file)
@@ -5,7 +5,6 @@ import java.io.InputStream;
 import java.util.Optional;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
-import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.function.Supplier;
 
 import net.pterodactylus.fcp.AllData;
@@ -116,25 +115,27 @@ class ClientGetCommandImpl implements ClientGetCommand {
 
        private class ClientGetDialog extends FcpDialog<Optional<Data>> {
 
-               private final AtomicBoolean finished = new AtomicBoolean();
-               private final AtomicBoolean failed = new AtomicBoolean();
-
-               private String contentType;
-               private long dataLength;
-               private InputStream payload;
-
                public ClientGetDialog() throws IOException {
-                       super(ClientGetCommandImpl.this.threadPool, ClientGetCommandImpl.this.connectionSupplier.get());
+                       super(ClientGetCommandImpl.this.threadPool, ClientGetCommandImpl.this.connectionSupplier.get(), Optional.<Data>empty());
                }
 
                @Override
-               protected boolean isFinished() {
-                       return finished.get() || failed.get();
+               protected void consumeAllData(AllData allData) {
+                       synchronized (this) {
+                               String contentType = allData.getContentType();
+                               long dataLength = allData.getDataLength();
+                               try {
+                                       InputStream payload = new TempInputStream(allData.getPayloadInputStream(), dataLength);
+                                       setResult(Optional.of(createData(contentType, dataLength, payload)));
+                               } catch (IOException e) {
+                                       // TODO – logging
+                                       finish();
+                               }
+                       }
                }
 
-               @Override
-               protected Optional<Data> getResult() {
-                       return failed.get() ? Optional.empty() : Optional.of(new Data() {
+               private Data createData(String contentType, long dataLength, InputStream payload) {
+                       return new Data() {
                                @Override
                                public String getMimeType() {
                                        return contentType;
@@ -149,27 +150,12 @@ class ClientGetCommandImpl implements ClientGetCommand {
                                public InputStream getInputStream() {
                                        return payload;
                                }
-                       });
-               }
-
-               @Override
-               protected void consumeAllData(AllData allData) {
-                       synchronized (this) {
-                               contentType = allData.getContentType();
-                               dataLength = allData.getDataLength();
-                               try {
-                                       payload = new TempInputStream(allData.getPayloadInputStream(), dataLength);
-                                       finished.set(true);
-                               } catch (IOException e) {
-                                       // TODO – logging
-                                       failed.set(true);
-                               }
-                       }
+                       };
                }
 
                @Override
                protected void consumeGetFailed(GetFailed getFailed) {
-                       failed.set(true);
+                       finish();
                }
 
        }
index cb85f60..551b52d 100644 (file)
@@ -58,25 +58,13 @@ public class ClientHelloImpl {
 
        private class ClientHelloDialog extends FcpDialog<Boolean> {
 
-               private final AtomicReference<NodeHello> receivedNodeHello = new AtomicReference<>();
-
                public ClientHelloDialog(FcpConnection connection) {
-                       super(ClientHelloImpl.this.threadPool, connection);
-               }
-
-               @Override
-               protected boolean isFinished() {
-                       return receivedNodeHello.get() != null;
-               }
-
-               @Override
-               protected Boolean getResult() {
-                       return receivedNodeHello.get() != null;
+                       super(ClientHelloImpl.this.threadPool, connection, false);
                }
 
                @Override
                protected void consumeNodeHello(NodeHello nodeHello) {
-                       receivedNodeHello.set(nodeHello);
+                       setResult(true);
                }
 
        }
index 6eeddd9..982fd76 100644 (file)
@@ -10,7 +10,6 @@ import java.util.Optional;
 import java.util.concurrent.CopyOnWriteArrayList;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
-import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicLong;
 import java.util.concurrent.atomic.AtomicReference;
 import java.util.function.Consumer;
@@ -140,21 +139,9 @@ class ClientPutCommandImpl implements ClientPutCommand {
 
                private final AtomicReference<FcpMessage> originalClientPut = new AtomicReference<>();
                private final AtomicReference<String> directory = new AtomicReference<>();
-               private final AtomicReference<Key> finalKey = new AtomicReference<>();
-               private final AtomicBoolean putFinished = new AtomicBoolean();
 
                public ClientPutDialog() throws IOException {
-                       super(ClientPutCommandImpl.this.threadPool, ClientPutCommandImpl.this.connectionSupplier.get());
-               }
-
-               @Override
-               protected boolean isFinished() {
-                       return putFinished.get();
-               }
-
-               @Override
-               protected Optional<Key> getResult() {
-                       return Optional.ofNullable(finalKey.get());
+                       super(ClientPutCommandImpl.this.threadPool, ClientPutCommandImpl.this.connectionSupplier.get(), Optional.<Key>empty());
                }
 
                @Override
@@ -176,13 +163,12 @@ class ClientPutCommandImpl implements ClientPutCommand {
 
                @Override
                protected void consumePutSuccessful(PutSuccessful putSuccessful) {
-                       finalKey.set(new Key(putSuccessful.getURI()));
-                       putFinished.set(true);
+                       setResult(Optional.of(new Key(putSuccessful.getURI())));
                }
 
                @Override
                protected void consumePutFailed(PutFailed putFailed) {
-                       putFinished.set(true);
+                       finish();
                }
 
                @Override
@@ -191,7 +177,7 @@ class ClientPutCommandImpl implements ClientPutCommand {
                                setIdentifier(directory.get());
                                sendMessage(new TestDDARequest(directory.get(), true, false));
                        } else {
-                               putFinished.set(true);
+                               finish();
                        }
                }
 
index f218610..5b030bd 100644 (file)
@@ -72,17 +72,31 @@ public abstract class FcpDialog<R> implements AutoCloseable, FcpListener {
        private final AtomicReference<String> identifier = new AtomicReference<>();
        private final AtomicBoolean connectionClosed = new AtomicBoolean();
        private final AtomicReference<Throwable> connectionFailureReason = new AtomicReference<>();
+       private final AtomicBoolean finished = new AtomicBoolean();
+       private final AtomicReference<R> result = new AtomicReference<>();
 
-       public FcpDialog(ExecutorService executorService, FcpConnection fcpConnection) {
+       public FcpDialog(ExecutorService executorService, FcpConnection fcpConnection, R initialResult) {
                this.executorService = MoreExecutors.listeningDecorator(executorService);
                this.fcpConnection = fcpConnection;
+               result.set(initialResult);
        }
 
        protected void setIdentifier(String identifier) {
                this.identifier.set(identifier);
        }
 
-       protected abstract boolean isFinished();
+       public final boolean isFinished() {
+               return finished.get();
+       }
+
+       protected final void finish() {
+               finished.set(true);
+       }
+
+       protected final void setResult(R result) {
+               this.result.set(result);
+               finish();
+       }
 
        public ListenableFuture<R> send(FcpMessage fcpMessage) throws IOException {
                setIdentifier(fcpMessage.getField("Identifier"));
@@ -120,8 +134,8 @@ public abstract class FcpDialog<R> implements AutoCloseable, FcpListener {
                }
        }
 
-       protected R getResult() {
-               return null;
+       protected final R getResult() {
+               return result.get();
        }
 
        @Override
index 69fcf63..5a96450 100644 (file)
@@ -3,7 +3,6 @@ package net.pterodactylus.fcp.quelaton;
 import java.io.IOException;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
-import java.util.concurrent.atomic.AtomicReference;
 
 import net.pterodactylus.fcp.FcpKeyPair;
 import net.pterodactylus.fcp.GenerateSSK;
@@ -41,25 +40,13 @@ class GenerateKeypairCommandImpl implements GenerateKeypairCommand {
 
        private class FcpKeyPairDialog extends FcpDialog<FcpKeyPair> {
 
-               private AtomicReference<FcpKeyPair> keyPair = new AtomicReference<>();
-
                public FcpKeyPairDialog() throws IOException {
-                       super(GenerateKeypairCommandImpl.this.threadPool, GenerateKeypairCommandImpl.this.connectionSupplier.get());
-               }
-
-               @Override
-               protected boolean isFinished() {
-                       return keyPair.get() != null;
-               }
-
-               @Override
-               protected FcpKeyPair getResult() {
-                       return keyPair.get();
+                       super(GenerateKeypairCommandImpl.this.threadPool, GenerateKeypairCommandImpl.this.connectionSupplier.get(), null);
                }
 
                @Override
                protected void consumeSSKKeypair(SSKKeypair sskKeypair) {
-                       keyPair.set(new FcpKeyPair(sskKeypair.getRequestURI(), sskKeypair.getInsertURI()));
+                       setResult(new FcpKeyPair(sskKeypair.getRequestURI(), sskKeypair.getInsertURI()));
                }
 
        }
index cac2535..1a5971c 100644 (file)
@@ -4,7 +4,6 @@ import java.io.IOException;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.atomic.AtomicBoolean;
-import java.util.concurrent.atomic.AtomicReference;
 import java.util.function.Supplier;
 
 import net.pterodactylus.fcp.ConfigData;
@@ -109,25 +108,13 @@ public class GetConfigCommandImpl implements GetConfigCommand {
 
        private class GetConfigDialog extends FcpDialog<ConfigData> {
 
-               private final AtomicReference<ConfigData> configData = new AtomicReference<>();
-
                public GetConfigDialog() throws IOException {
-                       super(threadPool, connectionSupplier.get());
-               }
-
-               @Override
-               protected boolean isFinished() {
-                       return configData.get() != null;
-               }
-
-               @Override
-               protected ConfigData getResult() {
-                       return configData.get();
+                       super(threadPool, connectionSupplier.get(), null);
                }
 
                @Override
                protected void consumeConfigData(ConfigData configData) {
-                       this.configData.set(configData);
+                       setResult(configData);
                }
 
        }
index 12f936d..d6999f2 100644 (file)
@@ -4,7 +4,6 @@ import java.io.IOException;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.atomic.AtomicBoolean;
-import java.util.concurrent.atomic.AtomicReference;
 import java.util.function.Supplier;
 
 import net.pterodactylus.fcp.GetNode;
@@ -67,25 +66,13 @@ public class GetNodeCommandImpl implements GetNodeCommand {
 
        private class GetNodeDialog extends FcpDialog<NodeData> {
 
-               private final AtomicReference<NodeData> nodeData = new AtomicReference<>();
-
                public GetNodeDialog() throws IOException {
-                       super(threadPool, connectionSupplier.get());
-               }
-
-               @Override
-               protected boolean isFinished() {
-                       return nodeData.get() != null;
-               }
-
-               @Override
-               protected NodeData getResult() {
-                       return nodeData.get();
+                       super(threadPool, connectionSupplier.get(), null);
                }
 
                @Override
                protected void consumeNodeData(NodeData nodeData) {
-                       this.nodeData.set(nodeData);
+                       setResult(nodeData);
                }
 
        }
index 49db826..e8a30ec 100644 (file)
@@ -4,8 +4,6 @@ import java.io.IOException;
 import java.util.Optional;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
-import java.util.concurrent.atomic.AtomicBoolean;
-import java.util.concurrent.atomic.AtomicReference;
 import java.util.function.Supplier;
 
 import net.pterodactylus.fcp.GetPluginInfo;
@@ -57,32 +55,18 @@ public class GetPluginInfoCommandImpl implements GetPluginInfoCommand {
 
        private class GetPluginInfoDialog extends FcpDialog<Optional<PluginInfo>> {
 
-               private final AtomicBoolean finished = new AtomicBoolean();
-               private final AtomicReference<PluginInfo> pluginInfo = new AtomicReference<>();
-
                public GetPluginInfoDialog() throws IOException {
-                       super(threadPool, connectionSupplier.get());
-               }
-
-               @Override
-               protected boolean isFinished() {
-                       return finished.get();
-               }
-
-               @Override
-               protected Optional<PluginInfo> getResult() {
-                       return Optional.ofNullable(pluginInfo.get());
+                       super(threadPool, connectionSupplier.get(), Optional.<PluginInfo>empty());
                }
 
                @Override
                protected void consumePluginInfo(PluginInfo pluginInfo) {
-                       this.pluginInfo.set(pluginInfo);
-                       finished.set(true);
+                       setResult(Optional.ofNullable(pluginInfo));
                }
 
                @Override
                protected void consumeProtocolError(ProtocolError protocolError) {
-                       finished.set(true);
+                       finish();
                }
 
        }
index fc9aeec..4bcd2fa 100644 (file)
@@ -4,7 +4,6 @@ import java.io.IOException;
 import java.util.Optional;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
-import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicReference;
 import java.util.function.Supplier;
 
@@ -65,32 +64,18 @@ public class ListPeerCommandImpl implements ListPeerCommand {
 
        private class ListPeerDialog extends FcpDialog<Peer> {
 
-               private final AtomicBoolean finished = new AtomicBoolean();
-               private final AtomicReference<Peer> peer = new AtomicReference<>();
-
                public ListPeerDialog() throws IOException {
-                       super(threadPool, connectionSupplier.get());
-               }
-
-               @Override
-               protected boolean isFinished() {
-                       return finished.get();
-               }
-
-               @Override
-               protected Peer getResult() {
-                       return peer.get();
+                       super(threadPool, connectionSupplier.get(), null);
                }
 
                @Override
                protected void consumePeer(Peer peer) {
-                       this.peer.set(peer);
-                       finished.set(true);
+                       setResult(peer);
                }
 
                @Override
                protected void consumeUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) {
-                       finished.set(true);
+                       finish();
                }
 
        }
index 8e1df8c..95e885f 100644 (file)
@@ -4,7 +4,6 @@ import java.io.IOException;
 import java.util.Optional;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
-import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicReference;
 import java.util.function.Supplier;
 
@@ -66,36 +65,23 @@ public class ListPeerNotesCommandImpl implements ListPeerNotesCommand {
 
        private class ListPeerNotesDialog extends FcpDialog<Optional<PeerNote>> {
 
-               private final AtomicReference<PeerNote> peerNote = new AtomicReference<>();
-               private final AtomicBoolean finished = new AtomicBoolean();
-
                public ListPeerNotesDialog() throws IOException {
-                       super(threadPool, connectionSupplier.get());
-               }
-
-               @Override
-               protected boolean isFinished() {
-                       return finished.get();
-               }
-
-               @Override
-               protected Optional<PeerNote> getResult() {
-                       return Optional.ofNullable(peerNote.get());
+                       super(threadPool, connectionSupplier.get(), Optional.<PeerNote>empty());
                }
 
                @Override
                protected void consumePeerNote(PeerNote peerNote) {
-                       this.peerNote.set(peerNote);
+                       setResult(Optional.ofNullable(peerNote));
                }
 
                @Override
                protected void consumeEndListPeerNotes(EndListPeerNotes endListPeerNotes) {
-                       finished.set(true);
+                       finish();
                }
 
                @Override
                protected void consumeUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) {
-                       finished.set(true);
+                       finish();
                }
 
        }
index 6a352d5..c628749 100644 (file)
@@ -2,6 +2,7 @@ package net.pterodactylus.fcp.quelaton;
 
 import java.io.IOException;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.HashSet;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
@@ -62,20 +63,9 @@ public class ListPeersCommandImpl implements ListPeersCommand {
        private class ListPeersDialog extends FcpDialog<Collection<Peer>> {
 
                private final Collection<Peer> peers = new HashSet<>();
-               private final AtomicBoolean finished = new AtomicBoolean(false);
 
                public ListPeersDialog() throws IOException {
-                       super(threadPool, connectionSupplier.get());
-               }
-
-               @Override
-               protected boolean isFinished() {
-                       return finished.get();
-               }
-
-               @Override
-               protected Collection<Peer> getResult() {
-                       return peers;
+                       super(threadPool, connectionSupplier.get(), Collections.<Peer>emptyList());
                }
 
                @Override
@@ -85,7 +75,7 @@ public class ListPeersCommandImpl implements ListPeersCommand {
 
                @Override
                protected void consumeEndListPeers(EndListPeers endListPeers) {
-                       finished.set(true);
+                       setResult(peers);
                }
 
        }
index 9c9e64e..fa6bd65 100644 (file)
@@ -4,8 +4,6 @@ import java.io.IOException;
 import java.util.Optional;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
-import java.util.concurrent.atomic.AtomicBoolean;
-import java.util.concurrent.atomic.AtomicReference;
 import java.util.function.Supplier;
 
 import net.pterodactylus.fcp.LoadPlugin;
@@ -90,32 +88,18 @@ public class LoadPluginCommandImpl implements LoadPluginCommand {
 
        private class LoadPluginDialog extends FcpDialog<Optional<PluginInfo>> {
 
-               private final AtomicBoolean finished = new AtomicBoolean();
-               private final AtomicReference<PluginInfo> pluginInfo = new AtomicReference<>();
-
                public LoadPluginDialog() throws IOException {
-                       super(threadPool, connectionSupplier.get());
-               }
-
-               @Override
-               protected boolean isFinished() {
-                       return finished.get();
-               }
-
-               @Override
-               protected Optional<PluginInfo> getResult() {
-                       return Optional.ofNullable(pluginInfo.get());
+                       super(threadPool, connectionSupplier.get(), Optional.<PluginInfo>empty());
                }
 
                @Override
                protected void consumePluginInfo(PluginInfo pluginInfo) {
-                       this.pluginInfo.set(pluginInfo);
-                       finished.set(true);
+                       setResult(Optional.ofNullable(pluginInfo));
                }
 
                @Override
                protected void consumeProtocolError(ProtocolError protocolError) {
-                       finished.set(true);
+                       finish();
                }
 
        }
index 6733d25..4e3b373 100644 (file)
@@ -3,8 +3,6 @@ package net.pterodactylus.fcp.quelaton;
 import java.io.IOException;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
-import java.util.concurrent.atomic.AtomicBoolean;
-import java.util.concurrent.atomic.AtomicReference;
 import java.util.function.Supplier;
 
 import net.pterodactylus.fcp.ConfigData;
@@ -54,27 +52,13 @@ public class ModifyConfigCommandImpl implements ModifyConfigCommand {
 
        private class ModifyConfigDialog extends FcpDialog<ConfigData> {
 
-               private final AtomicBoolean finished = new AtomicBoolean();
-               private final AtomicReference<ConfigData> configData = new AtomicReference<>();
-
                public ModifyConfigDialog() throws IOException {
-                       super(threadPool, connectionSupplier.get());
-               }
-
-               @Override
-               protected boolean isFinished() {
-                       return finished.get();
-               }
-
-               @Override
-               protected ConfigData getResult() {
-                       return configData.get();
+                       super(threadPool, connectionSupplier.get(), null);
                }
 
                @Override
                protected void consumeConfigData(ConfigData configData) {
-                       this.configData.set(configData);
-                       finished.set(true);
+                       setResult(configData);
                }
 
        }
index 9fff845..d5f5840 100644 (file)
@@ -4,7 +4,6 @@ import java.io.IOException;
 import java.util.Optional;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
-import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicReference;
 import java.util.function.Supplier;
 
@@ -135,32 +134,18 @@ public class ModifyPeerCommandImpl implements ModifyPeerCommand {
 
        private class ModifyPeerDialog extends FcpDialog<Optional<Peer>> {
 
-               private final AtomicBoolean finished = new AtomicBoolean();
-               private final AtomicReference<Peer> peer = new AtomicReference<>();
-
                public ModifyPeerDialog() throws IOException {
-                       super(threadPool, connectionSupplier.get());
-               }
-
-               @Override
-               protected boolean isFinished() {
-                       return finished.get();
-               }
-
-               @Override
-               protected Optional<Peer> getResult() {
-                       return Optional.ofNullable(peer.get());
+                       super(threadPool, connectionSupplier.get(), Optional.<Peer>empty());
                }
 
                @Override
                protected void consumePeer(Peer peer) {
-                       this.peer.set(peer);
-                       finished.set(true);
+                       setResult(Optional.ofNullable(peer));
                }
 
                @Override
                protected void consumeUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) {
-                       finished.set(true);
+                       finish();
                }
 
        }
index a3b9aa1..8421240 100644 (file)
@@ -4,7 +4,6 @@ import java.io.IOException;
 import java.nio.charset.StandardCharsets;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
-import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicReference;
 import java.util.function.Supplier;
 
@@ -82,32 +81,18 @@ public class ModifyPeerNoteCommandImpl implements ModifyPeerNoteCommand {
 
        private class ModifyPeerNoteDialog extends FcpDialog<Boolean> {
 
-               private final AtomicBoolean finished = new AtomicBoolean();
-               private final AtomicBoolean successful = new AtomicBoolean();
-
                public ModifyPeerNoteDialog() throws IOException {
-                       super(threadPool, connectionSupplier.get());
-               }
-
-               @Override
-               protected boolean isFinished() {
-                       return finished.get();
-               }
-
-               @Override
-               protected Boolean getResult() {
-                       return successful.get();
+                       super(threadPool, connectionSupplier.get(), false);
                }
 
                @Override
                protected void consumePeerNote(PeerNote peerNote) {
-                       successful.set(true);
-                       finished.set(true);
+                       setResult(true);
                }
 
                @Override
                protected void consumeUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) {
-                       finished.set(true);
+                       finish();
                }
 
        }
index a05963c..7e53055 100644 (file)
@@ -4,8 +4,6 @@ import java.io.IOException;
 import java.util.Optional;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
-import java.util.concurrent.atomic.AtomicBoolean;
-import java.util.concurrent.atomic.AtomicReference;
 import java.util.function.Supplier;
 
 import net.pterodactylus.fcp.PluginInfo;
@@ -69,32 +67,18 @@ public class ReloadPluginCommandImpl implements ReloadPluginCommand {
 
        private class ReloadPluginDialog extends FcpDialog<Optional<PluginInfo>> {
 
-               private final AtomicBoolean finished = new AtomicBoolean();
-               private final AtomicReference<PluginInfo> pluginInfo = new AtomicReference<>();
-
                public ReloadPluginDialog() throws IOException {
-                       super(threadPool, connectionSupplier.get());
-               }
-
-               @Override
-               protected boolean isFinished() {
-                       return finished.get();
-               }
-
-               @Override
-               protected Optional<PluginInfo> getResult() {
-                       return Optional.ofNullable(pluginInfo.get());
+                       super(threadPool, connectionSupplier.get(), Optional.<PluginInfo>empty());
                }
 
                @Override
                protected void consumePluginInfo(PluginInfo pluginInfo) {
-                       this.pluginInfo.set(pluginInfo);
-                       finished.set(true);
+                       setResult(Optional.ofNullable(pluginInfo));
                }
 
                @Override
                protected void consumeProtocolError(ProtocolError protocolError) {
-                       finished.set(true);
+                       finish();
                }
 
        }
index e5083d7..b0113b8 100644 (file)
@@ -3,7 +3,6 @@ package net.pterodactylus.fcp.quelaton;
 import java.io.IOException;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
-import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicReference;
 import java.util.function.Supplier;
 
@@ -64,31 +63,18 @@ public class RemovePeerCommandImpl implements RemovePeerCommand {
 
        private class RemovePeerDialog extends FcpDialog<Boolean> {
 
-               private final AtomicBoolean finished = new AtomicBoolean();
-               private final AtomicBoolean removed = new AtomicBoolean();
-
                public RemovePeerDialog() throws IOException {
-                       super(threadPool, connectionSupplier.get());
-               }
-
-               @Override
-               protected boolean isFinished() {
-                       return finished.get() || removed.get();
-               }
-
-               @Override
-               protected Boolean getResult() {
-                       return removed.get();
+                       super(threadPool, connectionSupplier.get(), false);
                }
 
                @Override
                protected void consumePeerRemoved(PeerRemoved peerRemoved) {
-                       removed.set(true);
+                       setResult(true);
                }
 
                @Override
                protected void consumeUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) {
-                       finished.set(true);
+                       finish();
                }
 
        }
index 2135e2e..1e9ed56 100644 (file)
@@ -3,7 +3,6 @@ package net.pterodactylus.fcp.quelaton;
 import java.io.IOException;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
-import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.function.Supplier;
 
 import net.pterodactylus.fcp.PluginRemoved;
@@ -61,32 +60,18 @@ public class RemovePluginCommandImpl implements RemovePluginCommand {
 
        private class RemovePluginDialog extends FcpDialog<Boolean> {
 
-               private final AtomicBoolean finished = new AtomicBoolean();
-               private final AtomicBoolean pluginRemoved = new AtomicBoolean();
-
                public RemovePluginDialog() throws IOException {
-                       super(threadPool, connectionSupplier.get());
-               }
-
-               @Override
-               protected boolean isFinished() {
-                       return finished.get();
-               }
-
-               @Override
-               protected Boolean getResult() {
-                       return pluginRemoved.get();
+                       super(threadPool, connectionSupplier.get(), false);
                }
 
                @Override
                protected void consumePluginRemoved(PluginRemoved pluginRemoved) {
-                       this.pluginRemoved.set(true);
-                       finished.set(true);
+                       setResult(true);
                }
 
                @Override
                protected void consumeProtocolError(ProtocolError protocolError) {
-                       finished.set(true);
+                       finish();
                }
 
        }
index f314247..9977163 100644 (file)
@@ -4,7 +4,6 @@ import java.io.IOException;
 import java.util.Optional;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
-import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.function.Supplier;
 
 import net.pterodactylus.fcp.IdentifierCollision;
@@ -58,32 +57,18 @@ public class SubscribeUskCommandImpl implements SubscribeUskCommand {
 
        private class SubscribeUskDialog extends FcpDialog<Boolean> {
 
-               private final AtomicBoolean finished = new AtomicBoolean();
-               private final AtomicBoolean success = new AtomicBoolean();
-
                public SubscribeUskDialog() throws IOException {
-                       super(threadPool, connectionSupplier.get());
-               }
-
-               @Override
-               protected boolean isFinished() {
-                       return finished.get();
-               }
-
-               @Override
-               protected Boolean getResult() {
-                       return success.get();
+                       super(threadPool, connectionSupplier.get(), false);
                }
 
                @Override
                protected void consumeSubscribedUSK(SubscribedUSK subscribedUSK) {
-                       success.set(true);
-                       finished.set(true);
+                       setResult(true);
                }
 
                @Override
                protected void consumeIdentifierCollision(IdentifierCollision identifierCollision) {
-                       finished.set(true);
+                       finish();
                }
 
        }
index 3755a05..288903f 100644 (file)
@@ -46,12 +46,8 @@ public class UnsubscribeUskCommandImpl implements UnsubscribeUskCommand {
        private class UnsubscribeUskDialog extends FcpDialog<Void> {
 
                public UnsubscribeUskDialog() throws IOException {
-                       super(threadPool, connectionSupplier.get());
-               }
-
-               @Override
-               protected boolean isFinished() {
-                       return true;
+                       super(threadPool, connectionSupplier.get(), null);
+                       finish();
                }
 
        }
index ee04e6f..7651840 100644 (file)
@@ -77,12 +77,11 @@ public class FcpDialogTest {
        }
 
        private FcpDialog createBasicDialog() {
-               return new FcpDialog(executorService, fcpConnection) {
-                               @Override
-                               protected boolean isFinished() {
-                                       return true;
-                               }
-                       };
+               return new FcpDialog(executorService, fcpConnection, true) {
+                       {
+                               finish();
+                       }
+               };
        }
 
        @Test
@@ -329,24 +328,22 @@ public class FcpDialogTest {
                        private final AtomicBoolean gotPutFailed = new AtomicBoolean();
                        private final AtomicBoolean gotGetFailed = new AtomicBoolean();
 
-                       @Override
-                       protected boolean isFinished() {
-                               return gotPutFailed.get() && gotGetFailed.get();
-                       }
-
-                       @Override
-                       protected Boolean getResult() {
-                               return isFinished();
+                       private void checkIfBothFailedsWereReceived() {
+                               if (gotPutFailed.get() && gotGetFailed.get()) {
+                                       setResult(true);
+                               }
                        }
 
                        @Override
                        protected void consumePutFailed(PutFailed putFailed) {
                                gotPutFailed.set(true);
+                               checkIfBothFailedsWereReceived();
                        }
 
                        @Override
                        protected void consumeGetFailed(GetFailed getFailed) {
                                gotGetFailed.set(true);
+                               checkIfBothFailedsWereReceived();
                        }
                };
                Future<?> result = testFcpDialog.send(fcpMessage);
@@ -383,210 +380,205 @@ public class FcpDialogTest {
 
        private static class TestFcpDialog extends FcpDialog<Boolean> {
 
-               private final AtomicReference<String> gotMessage = new AtomicReference<>();
                private final AtomicReference<String> expectedMessage = new AtomicReference<>();
 
                public TestFcpDialog(ExecutorService executorService, FcpConnection fcpConnection) {
-                       super(executorService, fcpConnection);
+                       super(executorService, fcpConnection, false);
                }
 
                public void setExpectedMessage(String expectedMessage) {
                        this.expectedMessage.set(expectedMessage);
                }
 
-               @Override
-               protected boolean isFinished() {
-                       return getResult();
-               }
-
-               @Override
-               protected Boolean getResult() {
-                       return expectedMessage.get().equals(gotMessage.get());
+               private void verifyReceivedMessage(String message) {
+                       if (expectedMessage.get().equals(message)) {
+                               setResult(true);
+                       }
                }
 
                @Override
                protected void consumeNodeHello(NodeHello nodeHello) {
-                       gotMessage.set(nodeHello.getName());
+                       verifyReceivedMessage(nodeHello.getName());
                }
 
                @Override
                protected void consumeSSKKeypair(SSKKeypair sskKeypair) {
-                       gotMessage.set(sskKeypair.getName());
+                       verifyReceivedMessage(sskKeypair.getName());
                }
 
                @Override
                protected void consumePeer(Peer peer) {
-                       gotMessage.set(peer.getName());
+                       verifyReceivedMessage(peer.getName());
                }
 
                @Override
                protected void consumeEndListPeers(EndListPeers endListPeers) {
-                       gotMessage.set(endListPeers.getName());
+                       verifyReceivedMessage(endListPeers.getName());
                }
 
                @Override
                protected void consumePeerNote(PeerNote peerNote) {
-                       gotMessage.set(peerNote.getName());
+                       verifyReceivedMessage(peerNote.getName());
                }
 
                @Override
                protected void consumeEndListPeerNotes(EndListPeerNotes endListPeerNotes) {
-                       gotMessage.set(endListPeerNotes.getName());
+                       verifyReceivedMessage(endListPeerNotes.getName());
                }
 
                @Override
                protected void consumePeerRemoved(PeerRemoved peerRemoved) {
-                       gotMessage.set(peerRemoved.getName());
+                       verifyReceivedMessage(peerRemoved.getName());
                }
 
                @Override
                protected void consumeNodeData(NodeData nodeData) {
-                       gotMessage.set(nodeData.getName());
+                       verifyReceivedMessage(nodeData.getName());
                }
 
                @Override
                protected void consumeTestDDAReply(TestDDAReply testDDAReply) {
-                       gotMessage.set(testDDAReply.getName());
+                       verifyReceivedMessage(testDDAReply.getName());
                }
 
                @Override
                protected void consumeTestDDAComplete(TestDDAComplete testDDAComplete) {
-                       gotMessage.set(testDDAComplete.getName());
+                       verifyReceivedMessage(testDDAComplete.getName());
                }
 
                @Override
                protected void consumePersistentGet(PersistentGet persistentGet) {
-                       gotMessage.set(persistentGet.getName());
+                       verifyReceivedMessage(persistentGet.getName());
                }
 
                @Override
                protected void consumePersistentPut(PersistentPut persistentPut) {
-                       gotMessage.set(persistentPut.getName());
+                       verifyReceivedMessage(persistentPut.getName());
                }
 
                @Override
                protected void consumeEndListPersistentRequests(EndListPersistentRequests endListPersistentRequests) {
-                       gotMessage.set(endListPersistentRequests.getName());
+                       verifyReceivedMessage(endListPersistentRequests.getName());
                }
 
                @Override
                protected void consumeURIGenerated(URIGenerated uriGenerated) {
-                       gotMessage.set(uriGenerated.getName());
+                       verifyReceivedMessage(uriGenerated.getName());
                }
 
                @Override
                protected void consumeDataFound(DataFound dataFound) {
-                       gotMessage.set(dataFound.getName());
+                       verifyReceivedMessage(dataFound.getName());
                }
 
                @Override
                protected void consumeAllData(AllData allData) {
-                       gotMessage.set(allData.getName());
+                       verifyReceivedMessage(allData.getName());
                }
 
                @Override
                protected void consumeSimpleProgress(SimpleProgress simpleProgress) {
-                       gotMessage.set(simpleProgress.getName());
+                       verifyReceivedMessage(simpleProgress.getName());
                }
 
                @Override
                protected void consumeStartedCompression(StartedCompression startedCompression) {
-                       gotMessage.set(startedCompression.getName());
+                       verifyReceivedMessage(startedCompression.getName());
                }
 
                @Override
                protected void consumeFinishedCompression(FinishedCompression finishedCompression) {
-                       gotMessage.set(finishedCompression.getName());
+                       verifyReceivedMessage(finishedCompression.getName());
                }
 
                @Override
                protected void consumeUnknownPeerNoteType(UnknownPeerNoteType unknownPeerNoteType) {
-                       gotMessage.set(unknownPeerNoteType.getName());
+                       verifyReceivedMessage(unknownPeerNoteType.getName());
                }
 
                @Override
                protected void consumeUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) {
-                       gotMessage.set(unknownNodeIdentifier.getName());
+                       verifyReceivedMessage(unknownNodeIdentifier.getName());
                }
 
                @Override
                protected void consumeConfigData(ConfigData configData) {
-                       gotMessage.set(configData.getName());
+                       verifyReceivedMessage(configData.getName());
                }
 
                @Override
                protected void consumeGetFailed(GetFailed getFailed) {
-                       gotMessage.set(getFailed.getName());
+                       verifyReceivedMessage(getFailed.getName());
                }
 
                @Override
                protected void consumePutFailed(PutFailed putFailed) {
-                       gotMessage.set(putFailed.getName());
+                       verifyReceivedMessage(putFailed.getName());
                }
 
                @Override
                protected void consumeIdentifierCollision(IdentifierCollision identifierCollision) {
-                       gotMessage.set(identifierCollision.getName());
+                       verifyReceivedMessage(identifierCollision.getName());
                }
 
                @Override
                protected void consumePersistentPutDir(PersistentPutDir persistentPutDir) {
-                       gotMessage.set(persistentPutDir.getName());
+                       verifyReceivedMessage(persistentPutDir.getName());
                }
 
                @Override
                protected void consumePersistentRequestRemoved(PersistentRequestRemoved persistentRequestRemoved) {
-                       gotMessage.set(persistentRequestRemoved.getName());
+                       verifyReceivedMessage(persistentRequestRemoved.getName());
                }
 
                @Override
                protected void consumeSubscribedUSKUpdate(SubscribedUSKUpdate subscribedUSKUpdate) {
-                       gotMessage.set(subscribedUSKUpdate.getName());
+                       verifyReceivedMessage(subscribedUSKUpdate.getName());
                }
 
                @Override
                protected void consumePluginInfo(PluginInfo pluginInfo) {
-                       gotMessage.set(pluginInfo.getName());
+                       verifyReceivedMessage(pluginInfo.getName());
                }
 
                @Override
                protected void consumeFCPPluginReply(FCPPluginReply fcpPluginReply) {
-                       gotMessage.set(fcpPluginReply.getName());
+                       verifyReceivedMessage(fcpPluginReply.getName());
                }
 
                @Override
                protected void consumePersistentRequestModified(PersistentRequestModified persistentRequestModified) {
-                       gotMessage.set(persistentRequestModified.getName());
+                       verifyReceivedMessage(persistentRequestModified.getName());
                }
 
                @Override
                protected void consumePutSuccessful(PutSuccessful putSuccessful) {
-                       gotMessage.set(putSuccessful.getName());
+                       verifyReceivedMessage(putSuccessful.getName());
                }
 
                @Override
                protected void consumePutFetchable(PutFetchable putFetchable) {
-                       gotMessage.set(putFetchable.getName());
+                       verifyReceivedMessage(putFetchable.getName());
                }
 
                @Override
                protected void consumeSentFeed(SentFeed sentFeed) {
-                       gotMessage.set(sentFeed.getName());
+                       verifyReceivedMessage(sentFeed.getName());
                }
 
                @Override
                protected void consumeReceivedBookmarkFeed(ReceivedBookmarkFeed receivedBookmarkFeed) {
-                       gotMessage.set(receivedBookmarkFeed.getName());
+                       verifyReceivedMessage(receivedBookmarkFeed.getName());
                }
 
                @Override
                protected void consumeProtocolError(ProtocolError protocolError) {
-                       gotMessage.set(protocolError.getName());
+                       verifyReceivedMessage(protocolError.getName());
                }
 
                @Override
                protected void consumeUnknownMessage(FcpMessage fcpMessage) {
-                       gotMessage.set(fcpMessage.getName());
+                       verifyReceivedMessage(fcpMessage.getName());
                }
 
        }