Refactor FCP dialog
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / quelaton / ListPeersCommandImpl.java
index c084cb5..c628749 100644 (file)
@@ -2,10 +2,12 @@ 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;
-import java.util.concurrent.Future;
 import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.Supplier;
 
 import net.pterodactylus.fcp.EndListPeers;
 import net.pterodactylus.fcp.ListPeers;
@@ -16,7 +18,7 @@ import com.google.common.util.concurrent.ListeningExecutorService;
 import com.google.common.util.concurrent.MoreExecutors;
 
 /**
- * Default {@link ListPeersCommand} implementation based on {@link FcpReplySequence}.
+ * Default {@link ListPeersCommand} implementation based on {@link FcpDialog}.
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
@@ -24,12 +26,14 @@ public class ListPeersCommandImpl implements ListPeersCommand {
 
        private final ListeningExecutorService threadPool;
        private final ConnectionSupplier connectionSupplier;
+       private final Supplier<String> identifierGenerator;
        private final AtomicBoolean includeMetadata = new AtomicBoolean(false);
        private final AtomicBoolean includeVolatile = new AtomicBoolean(false);
 
-       public ListPeersCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
+       public ListPeersCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier, Supplier<String> identifierGenerator) {
                this.threadPool = MoreExecutors.listeningDecorator(threadPool);
                this.connectionSupplier = connectionSupplier;
+               this.identifierGenerator = identifierGenerator;
        }
 
        @Override
@@ -46,28 +50,22 @@ public class ListPeersCommandImpl implements ListPeersCommand {
 
        @Override
        public ListenableFuture<Collection<Peer>> execute() {
-               String identifier = new RandomIdentifierGenerator().generate();
-               ListPeers listPeers = new ListPeers(identifier, includeMetadata.get(), includeVolatile.get());
-               return threadPool.submit(() -> new ListPeersReplySequence().send(listPeers).get());
+               return threadPool.submit(this::executeDialog);
        }
 
-       private class ListPeersReplySequence extends FcpReplySequence<Collection<Peer>> {
-
-               private final Collection<Peer> peers = new HashSet<>();
-               private final AtomicBoolean finished = new AtomicBoolean(false);
-
-               public ListPeersReplySequence() throws IOException {
-                       super(threadPool, connectionSupplier.get());
+       private Collection<Peer> executeDialog() throws InterruptedException, ExecutionException, IOException {
+               ListPeers listPeers = new ListPeers(identifierGenerator.get(), includeMetadata.get(), includeVolatile.get());
+               try (ListPeersDialog listPeersDialog = new ListPeersDialog()) {
+                       return listPeersDialog.send(listPeers).get();
                }
+       }
 
-               @Override
-               protected boolean isFinished() {
-                       return finished.get();
-               }
+       private class ListPeersDialog extends FcpDialog<Collection<Peer>> {
 
-               @Override
-               protected Collection<Peer> getResult() {
-                       return peers;
+               private final Collection<Peer> peers = new HashSet<>();
+
+               public ListPeersDialog() throws IOException {
+                       super(threadPool, connectionSupplier.get(), Collections.<Peer>emptyList());
                }
 
                @Override
@@ -77,7 +75,7 @@ public class ListPeersCommandImpl implements ListPeersCommand {
 
                @Override
                protected void consumeEndListPeers(EndListPeers endListPeers) {
-                       finished.set(true);
+                       setResult(peers);
                }
 
        }