Refactor FCP dialog
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / quelaton / GenerateKeypairCommandImpl.java
index 6f5df33..5a96450 100644 (file)
@@ -1,14 +1,17 @@
 package net.pterodactylus.fcp.quelaton;
 
 import java.io.IOException;
+import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Future;
-import java.util.concurrent.atomic.AtomicReference;
 
 import net.pterodactylus.fcp.FcpKeyPair;
 import net.pterodactylus.fcp.GenerateSSK;
 import net.pterodactylus.fcp.SSKKeypair;
 
+import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.ListeningExecutorService;
+import com.google.common.util.concurrent.MoreExecutors;
+
 /**
  * Implementation of the {@link GenerateKeypairCommand}.
  *
@@ -16,40 +19,34 @@ import net.pterodactylus.fcp.SSKKeypair;
  */
 class GenerateKeypairCommandImpl implements GenerateKeypairCommand {
 
-       private final ExecutorService threadPool;
+       private final ListeningExecutorService threadPool;
        private final ConnectionSupplier connectionSupplier;
 
        GenerateKeypairCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
-               this.threadPool = threadPool;
+               this.threadPool = MoreExecutors.listeningDecorator(threadPool);
                this.connectionSupplier = connectionSupplier;
        }
 
        @Override
-       public Future<FcpKeyPair> execute() {
-               return threadPool.submit(() -> new FcpKeyPairReplySequence().send(new GenerateSSK()).get());
+       public ListenableFuture<FcpKeyPair> execute() {
+               return threadPool.submit(this::executeDialog);
        }
 
-       private class FcpKeyPairReplySequence extends FcpReplySequence<FcpKeyPair> {
-
-               private AtomicReference<FcpKeyPair> keyPair = new AtomicReference<>();
-
-               public FcpKeyPairReplySequence() throws IOException {
-                       super(GenerateKeypairCommandImpl.this.threadPool, GenerateKeypairCommandImpl.this.connectionSupplier.get());
+       private FcpKeyPair executeDialog() throws InterruptedException, ExecutionException, IOException {
+               try (FcpKeyPairDialog fcpKeyPairDialog = new FcpKeyPairDialog()) {
+                       return fcpKeyPairDialog.send(new GenerateSSK()).get();
                }
+       }
 
-               @Override
-               protected boolean isFinished() {
-                       return keyPair.get() != null;
-               }
+       private class FcpKeyPairDialog extends FcpDialog<FcpKeyPair> {
 
-               @Override
-               protected FcpKeyPair getResult() {
-                       return keyPair.get();
+               public FcpKeyPairDialog() throws IOException {
+                       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()));
                }
 
        }