Use a single identifier generator in all commands
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / quelaton / ModifyPeerCommandImpl.java
index 6877245..9fff845 100644 (file)
@@ -6,6 +6,7 @@ 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.ModifyPeer;
 import net.pterodactylus.fcp.Peer;
@@ -24,15 +25,18 @@ public class ModifyPeerCommandImpl implements ModifyPeerCommand {
 
        private final ListeningExecutorService threadPool;
        private final ConnectionSupplier connectionSupplier;
+       private final Supplier<String> identifierGenerator;
        private final AtomicReference<String> nodeIdentifier = new AtomicReference<>();
        private final AtomicReference<Boolean> enabled = new AtomicReference<>();
        private final AtomicReference<Boolean> allowLocalAddresses = new AtomicReference<>();
        private final AtomicReference<Boolean> burstOnly = new AtomicReference<>();
        private final AtomicReference<Boolean> listenOnly = new AtomicReference<>();
+       private final AtomicReference<Boolean> ignoreSource = new AtomicReference<>();
 
-       public ModifyPeerCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
+       public ModifyPeerCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier, Supplier<String> identifierGenerator) {
                this.threadPool = MoreExecutors.listeningDecorator(threadPool);
                this.connectionSupplier = connectionSupplier;
+               this.identifierGenerator = identifierGenerator;
        }
 
        @Override
@@ -84,6 +88,18 @@ public class ModifyPeerCommandImpl implements ModifyPeerCommand {
        }
 
        @Override
+       public ModifyPeerCommand ignoreSource() {
+               ignoreSource.set(true);
+               return this;
+       }
+
+       @Override
+       public ModifyPeerCommand useSource() {
+               ignoreSource.set(false);
+               return this;
+       }
+
+       @Override
        public Executable<Optional<Peer>> byName(String name) {
                nodeIdentifier.set(name);
                return this::execute;
@@ -102,15 +118,16 @@ public class ModifyPeerCommandImpl implements ModifyPeerCommand {
        }
 
        private ListenableFuture<Optional<Peer>> execute() {
-               return threadPool.submit(this::executeSequence);
+               return threadPool.submit(this::executeDialog);
        }
 
-       private Optional<Peer> executeSequence() throws IOException, ExecutionException, InterruptedException {
-               ModifyPeer modifyPeer = new ModifyPeer(new RandomIdentifierGenerator().generate(), nodeIdentifier.get());
+       private Optional<Peer> executeDialog() throws IOException, ExecutionException, InterruptedException {
+               ModifyPeer modifyPeer = new ModifyPeer(identifierGenerator.get(), nodeIdentifier.get());
                Optional.ofNullable(enabled.get()).ifPresent(enabled -> modifyPeer.setEnabled(enabled));
                Optional.ofNullable(allowLocalAddresses.get()).ifPresent(allowed -> modifyPeer.setAllowLocalAddresses(allowed));
                Optional.ofNullable(burstOnly.get()).ifPresent(burstOnly -> modifyPeer.setBurstOnly(burstOnly));
                Optional.ofNullable(listenOnly.get()).ifPresent(listenOnly -> modifyPeer.setListenOnly(listenOnly));
+               Optional.ofNullable(ignoreSource.get()).ifPresent(ignoreSource -> modifyPeer.setIgnoreSource(ignoreSource));
                try (ModifyPeerDialog modifyPeerDialog = new ModifyPeerDialog()) {
                        return modifyPeerDialog.send(modifyPeer).get();
                }