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