Refactor FCP dialog
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / quelaton / RemovePeerCommandImpl.java
index 9578477..b0113b8 100644 (file)
@@ -3,8 +3,8 @@ 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.PeerRemoved;
 import net.pterodactylus.fcp.RemovePeer;
@@ -23,11 +23,13 @@ public class RemovePeerCommandImpl implements RemovePeerCommand {
 
        private final ListeningExecutorService threadPool;
        private final ConnectionSupplier connectionSupplier;
+       private final Supplier<String> identifierGenerator;
        private final AtomicReference<String> nodeIdentifier = new AtomicReference<>();
 
-       public RemovePeerCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
+       public RemovePeerCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier, Supplier<String> identifierGenerator) {
                this.threadPool = MoreExecutors.listeningDecorator(threadPool);
                this.connectionSupplier = connectionSupplier;
+               this.identifierGenerator = identifierGenerator;
        }
 
        @Override
@@ -36,12 +38,24 @@ public class RemovePeerCommandImpl implements RemovePeerCommand {
                return this::execute;
        }
 
+       @Override
+       public Executable<Boolean> byIdentity(String nodeIdentity) {
+               nodeIdentifier.set(nodeIdentity);
+               return this::execute;
+       }
+
+       @Override
+       public Executable<Boolean> byHostAndPort(String host, int port) {
+               nodeIdentifier.set(String.format("%s:%d", host, port));
+               return this::execute;
+       }
+
        private ListenableFuture<Boolean> execute() {
                return threadPool.submit(this::executeDialog);
        }
 
        private boolean executeDialog() throws IOException, ExecutionException, InterruptedException {
-               RemovePeer removePeer = new RemovePeer(new RandomIdentifierGenerator().generate(), nodeIdentifier.get());
+               RemovePeer removePeer = new RemovePeer(identifierGenerator.get(), nodeIdentifier.get());
                try (RemovePeerDialog removePeerDialog = new RemovePeerDialog()) {
                        return removePeerDialog.send(removePeer).get();
                }
@@ -49,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();
                }
 
        }