Refactor FCP dialog
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / quelaton / GetConfigCommandImpl.java
index 2876661..1a5971c 100644 (file)
@@ -3,7 +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.AtomicReference;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.Supplier;
 
 import net.pterodactylus.fcp.ConfigData;
 import net.pterodactylus.fcp.GetConfig;
@@ -21,10 +22,68 @@ public class GetConfigCommandImpl implements GetConfigCommand {
 
        private final ListeningExecutorService threadPool;
        private final ConnectionSupplier connectionSupplier;
-
-       public GetConfigCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
+       private final Supplier<String> identifierGenerator;
+       private final AtomicBoolean withCurrent = new AtomicBoolean();
+       private final AtomicBoolean withDefaults = new AtomicBoolean();
+       private final AtomicBoolean withSortOrder = new AtomicBoolean();
+       private final AtomicBoolean withExpertFlag = new AtomicBoolean();
+       private final AtomicBoolean withForceWriteFlag = new AtomicBoolean();
+       private final AtomicBoolean withShortDescription = new AtomicBoolean();
+       private final AtomicBoolean withLongDescription = new AtomicBoolean();
+       private final AtomicBoolean withDataTypes = new AtomicBoolean();
+
+       public GetConfigCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier, Supplier<String> identifierGenerator) {
                this.threadPool = MoreExecutors.listeningDecorator(threadPool);
                this.connectionSupplier = connectionSupplier;
+               this.identifierGenerator = identifierGenerator;
+       }
+
+       @Override
+       public GetConfigCommand withCurrent() {
+               withCurrent.set(true);
+               return this;
+       }
+
+       @Override
+       public GetConfigCommand withDefaults() {
+               withDefaults.set(true);
+               return this;
+       }
+
+       @Override
+       public GetConfigCommand withSortOrder() {
+               withSortOrder.set(true);
+               return this;
+       }
+
+       @Override
+       public GetConfigCommand withExpertFlag() {
+               withExpertFlag.set(true);
+               return this;
+       }
+
+       @Override
+       public GetConfigCommand withForceWriteFlag() {
+               withForceWriteFlag.set(true);
+               return this;
+       }
+
+       @Override
+       public GetConfigCommand withShortDescription() {
+               withShortDescription.set(true);
+               return this;
+       }
+
+       @Override
+       public GetConfigCommand withLongDescription() {
+               withLongDescription.set(true);
+               return this;
+       }
+
+       @Override
+       public GetConfigCommand withDataTypes() {
+               withDataTypes.set(true);
+               return this;
        }
 
        @Override
@@ -33,7 +92,15 @@ public class GetConfigCommandImpl implements GetConfigCommand {
        }
 
        private ConfigData executeDialog() throws IOException, ExecutionException, InterruptedException {
-               GetConfig getConfig = new GetConfig(new RandomIdentifierGenerator().generate());
+               GetConfig getConfig = new GetConfig(identifierGenerator.get());
+               getConfig.setWithCurrent(withCurrent.get());
+               getConfig.setWithDefaults(withDefaults.get());
+               getConfig.setWithSortOrder(withSortOrder.get());
+               getConfig.setWithExpertFlag(withExpertFlag.get());
+               getConfig.setWithForceWriteFlag(withForceWriteFlag.get());
+               getConfig.setWithShortDescription(withShortDescription.get());
+               getConfig.setWithLongDescription(withLongDescription.get());
+               getConfig.setWithDataTypes(withDataTypes.get());
                try (GetConfigDialog getConfigDialog = new GetConfigDialog()) {
                        return getConfigDialog.send(getConfig).get();
                }
@@ -41,25 +108,13 @@ public class GetConfigCommandImpl implements GetConfigCommand {
 
        private class GetConfigDialog extends FcpDialog<ConfigData> {
 
-               private final AtomicReference<ConfigData> configData = new AtomicReference<>();
-
                public GetConfigDialog() throws IOException {
-                       super(threadPool, connectionSupplier.get());
-               }
-
-               @Override
-               protected boolean isFinished() {
-                       return configData.get() != null;
-               }
-
-               @Override
-               protected ConfigData getResult() {
-                       return configData.get();
+                       super(threadPool, connectionSupplier.get(), null);
                }
 
                @Override
                protected void consumeConfigData(ConfigData configData) {
-                       this.configData.set(configData);
+                       setResult(configData);
                }
 
        }