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