Add method to include expert flag in the config data
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / quelaton / GetConfigCommandImpl.java
1 package net.pterodactylus.fcp.quelaton;
2
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;
8
9 import net.pterodactylus.fcp.ConfigData;
10 import net.pterodactylus.fcp.GetConfig;
11
12 import com.google.common.util.concurrent.ListenableFuture;
13 import com.google.common.util.concurrent.ListeningExecutorService;
14 import com.google.common.util.concurrent.MoreExecutors;
15
16 /**
17  * Default {@link GetConfigCommand} implementation based on {@link FcpDialog}.
18  *
19  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
20  */
21 public class GetConfigCommandImpl implements GetConfigCommand {
22
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
30         public GetConfigCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
31                 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
32                 this.connectionSupplier = connectionSupplier;
33         }
34
35         @Override
36         public GetConfigCommand withCurrent() {
37                 withCurrent.set(true);
38                 return this;
39         }
40
41         @Override
42         public GetConfigCommand withDefaults() {
43                 withDefaults.set(true);
44                 return this;
45         }
46
47         @Override
48         public GetConfigCommand withSortOrder() {
49                 withSortOrder.set(true);
50                 return this;
51         }
52
53         @Override
54         public GetConfigCommand withExpertFlag() {
55                 withExpertFlag.set(true);
56                 return this;
57         }
58
59         @Override
60         public ListenableFuture<ConfigData> execute() {
61                 return threadPool.submit(this::executeDialog);
62         }
63
64         private ConfigData executeDialog() throws IOException, ExecutionException, InterruptedException {
65                 GetConfig getConfig = new GetConfig(new RandomIdentifierGenerator().generate());
66                 getConfig.setWithCurrent(withCurrent.get());
67                 getConfig.setWithDefaults(withDefaults.get());
68                 getConfig.setWithSortOrder(withSortOrder.get());
69                 getConfig.setWithExpertFlag(withExpertFlag.get());
70                 try (GetConfigDialog getConfigDialog = new GetConfigDialog()) {
71                         return getConfigDialog.send(getConfig).get();
72                 }
73         }
74
75         private class GetConfigDialog extends FcpDialog<ConfigData> {
76
77                 private final AtomicReference<ConfigData> configData = new AtomicReference<>();
78
79                 public GetConfigDialog() throws IOException {
80                         super(threadPool, connectionSupplier.get());
81                 }
82
83                 @Override
84                 protected boolean isFinished() {
85                         return configData.get() != null;
86                 }
87
88                 @Override
89                 protected ConfigData getResult() {
90                         return configData.get();
91                 }
92
93                 @Override
94                 protected void consumeConfigData(ConfigData configData) {
95                         this.configData.set(configData);
96                 }
97
98         }
99
100 }