1 package net.pterodactylus.fcp.quelaton;
3 import java.io.IOException;
4 import java.util.concurrent.ExecutionException;
5 import java.util.concurrent.ExecutorService;
6 import java.util.concurrent.atomic.AtomicBoolean;
7 import java.util.concurrent.atomic.AtomicReference;
9 import net.pterodactylus.fcp.ConfigData;
10 import net.pterodactylus.fcp.GetConfig;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import com.google.common.util.concurrent.ListeningExecutorService;
14 import com.google.common.util.concurrent.MoreExecutors;
17 * Default {@link GetConfigCommand} implementation based on {@link FcpDialog}.
19 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
21 public class GetConfigCommandImpl implements GetConfigCommand {
23 private final ListeningExecutorService threadPool;
24 private final ConnectionSupplier connectionSupplier;
25 private final AtomicBoolean withCurrent = new AtomicBoolean();
26 private final AtomicBoolean withDefaults = new AtomicBoolean();
27 private final AtomicBoolean withSortOrder = new AtomicBoolean();
28 private final AtomicBoolean withExpertFlag = new AtomicBoolean();
29 private final AtomicBoolean withForceWriteFlag = new AtomicBoolean();
30 private final AtomicBoolean withShortDescription = new AtomicBoolean();
31 private final AtomicBoolean withLongDescription = new AtomicBoolean();
33 public GetConfigCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
34 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
35 this.connectionSupplier = connectionSupplier;
39 public GetConfigCommand withCurrent() {
40 withCurrent.set(true);
45 public GetConfigCommand withDefaults() {
46 withDefaults.set(true);
51 public GetConfigCommand withSortOrder() {
52 withSortOrder.set(true);
57 public GetConfigCommand withExpertFlag() {
58 withExpertFlag.set(true);
63 public GetConfigCommand withForceWriteFlag() {
64 withForceWriteFlag.set(true);
69 public GetConfigCommand withShortDescription() {
70 withShortDescription.set(true);
75 public GetConfigCommand withLongDescription() {
76 withLongDescription.set(true);
81 public ListenableFuture<ConfigData> execute() {
82 return threadPool.submit(this::executeDialog);
85 private ConfigData executeDialog() throws IOException, ExecutionException, InterruptedException {
86 GetConfig getConfig = new GetConfig(new RandomIdentifierGenerator().generate());
87 getConfig.setWithCurrent(withCurrent.get());
88 getConfig.setWithDefaults(withDefaults.get());
89 getConfig.setWithSortOrder(withSortOrder.get());
90 getConfig.setWithExpertFlag(withExpertFlag.get());
91 getConfig.setWithForceWriteFlag(withForceWriteFlag.get());
92 getConfig.setWithShortDescription(withShortDescription.get());
93 getConfig.setWithLongDescription(withLongDescription.get());
94 try (GetConfigDialog getConfigDialog = new GetConfigDialog()) {
95 return getConfigDialog.send(getConfig).get();
99 private class GetConfigDialog extends FcpDialog<ConfigData> {
101 private final AtomicReference<ConfigData> configData = new AtomicReference<>();
103 public GetConfigDialog() throws IOException {
104 super(threadPool, connectionSupplier.get());
108 protected boolean isFinished() {
109 return configData.get() != null;
113 protected ConfigData getResult() {
114 return configData.get();
118 protected void consumeConfigData(ConfigData configData) {
119 this.configData.set(configData);