Use a single identifier generator in all commands
[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 import java.util.function.Supplier;
9
10 import net.pterodactylus.fcp.ConfigData;
11 import net.pterodactylus.fcp.GetConfig;
12
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.ListeningExecutorService;
15 import com.google.common.util.concurrent.MoreExecutors;
16
17 /**
18  * Default {@link GetConfigCommand} implementation based on {@link FcpDialog}.
19  *
20  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
21  */
22 public class GetConfigCommandImpl implements GetConfigCommand {
23
24         private final ListeningExecutorService threadPool;
25         private final ConnectionSupplier connectionSupplier;
26         private final Supplier<String> identifierGenerator;
27         private final AtomicBoolean withCurrent = new AtomicBoolean();
28         private final AtomicBoolean withDefaults = new AtomicBoolean();
29         private final AtomicBoolean withSortOrder = new AtomicBoolean();
30         private final AtomicBoolean withExpertFlag = new AtomicBoolean();
31         private final AtomicBoolean withForceWriteFlag = new AtomicBoolean();
32         private final AtomicBoolean withShortDescription = new AtomicBoolean();
33         private final AtomicBoolean withLongDescription = new AtomicBoolean();
34         private final AtomicBoolean withDataTypes = new AtomicBoolean();
35
36         public GetConfigCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier, Supplier<String> identifierGenerator) {
37                 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
38                 this.connectionSupplier = connectionSupplier;
39                 this.identifierGenerator = identifierGenerator;
40         }
41
42         @Override
43         public GetConfigCommand withCurrent() {
44                 withCurrent.set(true);
45                 return this;
46         }
47
48         @Override
49         public GetConfigCommand withDefaults() {
50                 withDefaults.set(true);
51                 return this;
52         }
53
54         @Override
55         public GetConfigCommand withSortOrder() {
56                 withSortOrder.set(true);
57                 return this;
58         }
59
60         @Override
61         public GetConfigCommand withExpertFlag() {
62                 withExpertFlag.set(true);
63                 return this;
64         }
65
66         @Override
67         public GetConfigCommand withForceWriteFlag() {
68                 withForceWriteFlag.set(true);
69                 return this;
70         }
71
72         @Override
73         public GetConfigCommand withShortDescription() {
74                 withShortDescription.set(true);
75                 return this;
76         }
77
78         @Override
79         public GetConfigCommand withLongDescription() {
80                 withLongDescription.set(true);
81                 return this;
82         }
83
84         @Override
85         public GetConfigCommand withDataTypes() {
86                 withDataTypes.set(true);
87                 return this;
88         }
89
90         @Override
91         public ListenableFuture<ConfigData> execute() {
92                 return threadPool.submit(this::executeDialog);
93         }
94
95         private ConfigData executeDialog() throws IOException, ExecutionException, InterruptedException {
96                 GetConfig getConfig = new GetConfig(identifierGenerator.get());
97                 getConfig.setWithCurrent(withCurrent.get());
98                 getConfig.setWithDefaults(withDefaults.get());
99                 getConfig.setWithSortOrder(withSortOrder.get());
100                 getConfig.setWithExpertFlag(withExpertFlag.get());
101                 getConfig.setWithForceWriteFlag(withForceWriteFlag.get());
102                 getConfig.setWithShortDescription(withShortDescription.get());
103                 getConfig.setWithLongDescription(withLongDescription.get());
104                 getConfig.setWithDataTypes(withDataTypes.get());
105                 try (GetConfigDialog getConfigDialog = new GetConfigDialog()) {
106                         return getConfigDialog.send(getConfig).get();
107                 }
108         }
109
110         private class GetConfigDialog extends FcpDialog<ConfigData> {
111
112                 private final AtomicReference<ConfigData> configData = new AtomicReference<>();
113
114                 public GetConfigDialog() throws IOException {
115                         super(threadPool, connectionSupplier.get());
116                 }
117
118                 @Override
119                 protected boolean isFinished() {
120                         return configData.get() != null;
121                 }
122
123                 @Override
124                 protected ConfigData getResult() {
125                         return configData.get();
126                 }
127
128                 @Override
129                 protected void consumeConfigData(ConfigData configData) {
130                         this.configData.set(configData);
131                 }
132
133         }
134
135 }