Refactor FCP dialog
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / quelaton / ListPeerNotesCommandImpl.java
index 0cd0483..95e885f 100644 (file)
@@ -4,8 +4,8 @@ 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.EndListPeerNotes;
 import net.pterodactylus.fcp.ListPeerNotes;
@@ -25,11 +25,13 @@ public class ListPeerNotesCommandImpl implements ListPeerNotesCommand {
 
        private final ListeningExecutorService threadPool;
        private final ConnectionSupplier connectionSupplier;
+       private final Supplier<String> identifierGenerator;
        private final AtomicReference<String> nodeIdentifier = new AtomicReference<>();
 
-       public ListPeerNotesCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
+       public ListPeerNotesCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier, Supplier<String> identifierGenerator) {
                this.threadPool = MoreExecutors.listeningDecorator(threadPool);
                this.connectionSupplier = connectionSupplier;
+               this.identifierGenerator = identifierGenerator;
        }
 
        @Override
@@ -55,8 +57,7 @@ public class ListPeerNotesCommandImpl implements ListPeerNotesCommand {
        }
 
        private Optional<PeerNote> executeDialog() throws IOException, ExecutionException, InterruptedException {
-               ListPeerNotes listPeerNotes =
-                       new ListPeerNotes(new RandomIdentifierGenerator().generate(), nodeIdentifier.get());
+               ListPeerNotes listPeerNotes = new ListPeerNotes(identifierGenerator.get(), nodeIdentifier.get());
                try (ListPeerNotesDialog listPeerNotesDialog = new ListPeerNotesDialog()) {
                        return listPeerNotesDialog.send(listPeerNotes).get();
                }
@@ -64,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();
                }
 
        }