6e91c009966be0a47c7c2e866ddd84c966adbc0e
[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
28         public GetConfigCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
29                 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
30                 this.connectionSupplier = connectionSupplier;
31         }
32
33         @Override
34         public GetConfigCommand withCurrent() {
35                 withCurrent.set(true);
36                 return this;
37         }
38
39         @Override
40         public GetConfigCommand withDefaults() {
41                 withDefaults.set(true);
42                 return this;
43         }
44
45         @Override
46         public ListenableFuture<ConfigData> execute() {
47                 return threadPool.submit(this::executeDialog);
48         }
49
50         private ConfigData executeDialog() throws IOException, ExecutionException, InterruptedException {
51                 GetConfig getConfig = new GetConfig(new RandomIdentifierGenerator().generate());
52                 getConfig.setWithCurrent(withCurrent.get());
53                 getConfig.setWithDefaults(withDefaults.get());
54                 try (GetConfigDialog getConfigDialog = new GetConfigDialog()) {
55                         return getConfigDialog.send(getConfig).get();
56                 }
57         }
58
59         private class GetConfigDialog extends FcpDialog<ConfigData> {
60
61                 private final AtomicReference<ConfigData> configData = new AtomicReference<>();
62
63                 public GetConfigDialog() throws IOException {
64                         super(threadPool, connectionSupplier.get());
65                 }
66
67                 @Override
68                 protected boolean isFinished() {
69                         return configData.get() != null;
70                 }
71
72                 @Override
73                 protected ConfigData getResult() {
74                         return configData.get();
75                 }
76
77                 @Override
78                 protected void consumeConfigData(ConfigData configData) {
79                         this.configData.set(configData);
80                 }
81
82         }
83
84 }