1 package net.pterodactylus.fcp.quelaton;
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;
9 import net.pterodactylus.fcp.ConfigData;
10 import net.pterodactylus.fcp.GetConfig;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import com.google.common.util.concurrent.ListeningExecutorService;
14 import com.google.common.util.concurrent.MoreExecutors;
17 * Default {@link GetConfigCommand} implementation based on {@link FcpDialog}.
19 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
21 public class GetConfigCommandImpl implements GetConfigCommand {
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();
34 public GetConfigCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
35 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
36 this.connectionSupplier = connectionSupplier;
40 public GetConfigCommand withCurrent() {
41 withCurrent.set(true);
46 public GetConfigCommand withDefaults() {
47 withDefaults.set(true);
52 public GetConfigCommand withSortOrder() {
53 withSortOrder.set(true);
58 public GetConfigCommand withExpertFlag() {
59 withExpertFlag.set(true);
64 public GetConfigCommand withForceWriteFlag() {
65 withForceWriteFlag.set(true);
70 public GetConfigCommand withShortDescription() {
71 withShortDescription.set(true);
76 public GetConfigCommand withLongDescription() {
77 withLongDescription.set(true);
82 public GetConfigCommand withDataTypes() {
83 withDataTypes.set(true);
88 public ListenableFuture<ConfigData> execute() {
89 return threadPool.submit(this::executeDialog);
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();
107 private class GetConfigDialog extends FcpDialog<ConfigData> {
109 private final AtomicReference<ConfigData> configData = new AtomicReference<>();
111 public GetConfigDialog() throws IOException {
112 super(threadPool, connectionSupplier.get());
116 protected boolean isFinished() {
117 return configData.get() != null;
121 protected ConfigData getResult() {
122 return configData.get();
126 protected void consumeConfigData(ConfigData configData) {
127 this.configData.set(configData);