c2095b3ce31b05d45db543591c82ff7574129d20
[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         private final AtomicBoolean withForceWriteFlag = new AtomicBoolean();
30         private final AtomicBoolean withShortDescription = new AtomicBoolean();
31         private final AtomicBoolean withLongDescription = new AtomicBoolean();
32         private final AtomicBoolean withDataTypes = new AtomicBoolean();
33
34         public GetConfigCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
35                 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
36                 this.connectionSupplier = connectionSupplier;
37         }
38
39         @Override
40         public GetConfigCommand withCurrent() {
41                 withCurrent.set(true);
42                 return this;
43         }
44
45         @Override
46         public GetConfigCommand withDefaults() {
47                 withDefaults.set(true);
48                 return this;
49         }
50
51         @Override
52         public GetConfigCommand withSortOrder() {
53                 withSortOrder.set(true);
54                 return this;
55         }
56
57         @Override
58         public GetConfigCommand withExpertFlag() {
59                 withExpertFlag.set(true);
60                 return this;
61         }
62
63         @Override
64         public GetConfigCommand withForceWriteFlag() {
65                 withForceWriteFlag.set(true);
66                 return this;
67         }
68
69         @Override
70         public GetConfigCommand withShortDescription() {
71                 withShortDescription.set(true);
72                 return this;
73         }
74
75         @Override
76         public GetConfigCommand withLongDescription() {
77                 withLongDescription.set(true);
78                 return this;
79         }
80
81         @Override
82         public GetConfigCommand withDataTypes() {
83                 withDataTypes.set(true);
84                 return this;
85         }
86
87         @Override
88         public ListenableFuture<ConfigData> execute() {
89                 return threadPool.submit(this::executeDialog);
90         }
91
92         private ConfigData executeDialog() throws IOException, ExecutionException, InterruptedException {
93                 GetConfig getConfig = new GetConfig(new RandomIdentifierGenerator().generate());
94                 getConfig.setWithCurrent(withCurrent.get());
95                 getConfig.setWithDefaults(withDefaults.get());
96                 getConfig.setWithSortOrder(withSortOrder.get());
97                 getConfig.setWithExpertFlag(withExpertFlag.get());
98                 getConfig.setWithForceWriteFlag(withForceWriteFlag.get());
99                 getConfig.setWithShortDescription(withShortDescription.get());
100                 getConfig.setWithLongDescription(withLongDescription.get());
101                 getConfig.setWithDataTypes(withDataTypes.get());
102                 try (GetConfigDialog getConfigDialog = new GetConfigDialog()) {
103                         return getConfigDialog.send(getConfig).get();
104                 }
105         }
106
107         private class GetConfigDialog extends FcpDialog<ConfigData> {
108
109                 private final AtomicReference<ConfigData> configData = new AtomicReference<>();
110
111                 public GetConfigDialog() throws IOException {
112                         super(threadPool, connectionSupplier.get());
113                 }
114
115                 @Override
116                 protected boolean isFinished() {
117                         return configData.get() != null;
118                 }
119
120                 @Override
121                 protected ConfigData getResult() {
122                         return configData.get();
123                 }
124
125                 @Override
126                 protected void consumeConfigData(ConfigData configData) {
127                         this.configData.set(configData);
128                 }
129
130         }
131
132 }