Refactor FCP dialog
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / quelaton / ModifyPeerNoteCommandImpl.java
index 6e6dcc2..8421240 100644 (file)
@@ -4,8 +4,8 @@ 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;
 
 import net.pterodactylus.fcp.FreenetBase64;
 import net.pterodactylus.fcp.ModifyPeerNote;
@@ -26,15 +26,16 @@ import com.google.common.util.concurrent.MoreExecutors;
 public class ModifyPeerNoteCommandImpl implements ModifyPeerNoteCommand {
 
        private static final FreenetBase64 BASE_64 = new FreenetBase64();
-       private static final RandomIdentifierGenerator RANDOM_IDENTIFIER_GENERATOR = new RandomIdentifierGenerator();
        private final ListeningExecutorService threadPool;
        private final ConnectionSupplier connectionSupplier;
+       private final Supplier<String> identifierGenerator;
        private final AtomicReference<String> nodeIdentifier = new AtomicReference<>();
        private final AtomicReference<String> darknetComment = new AtomicReference<>();
 
-       public ModifyPeerNoteCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
+       public ModifyPeerNoteCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier, Supplier<String> identifierGenerator) {
                this.threadPool = MoreExecutors.listeningDecorator(threadPool);
                this.connectionSupplier = connectionSupplier;
+               this.identifierGenerator = identifierGenerator;
        }
 
        @Override
@@ -70,7 +71,7 @@ public class ModifyPeerNoteCommandImpl implements ModifyPeerNoteCommand {
 
        private Boolean executeDialog() throws IOException, ExecutionException, InterruptedException {
                ModifyPeerNote modifyPeerNote =
-                       new ModifyPeerNote(RANDOM_IDENTIFIER_GENERATOR.generate(), nodeIdentifier.get());
+                       new ModifyPeerNote(identifierGenerator.get(), nodeIdentifier.get());
                modifyPeerNote.setPeerNoteType(PeerNoteType.PRIVATE_DARKNET_COMMENT);
                modifyPeerNote.setNoteText(BASE_64.encode(darknetComment.get().getBytes(StandardCharsets.UTF_8)));
                try (ModifyPeerNoteDialog modifyPeerNoteDialog = new ModifyPeerNoteDialog()) {
@@ -80,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();
                }
 
        }