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();
32 public GetConfigCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
33 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
34 this.connectionSupplier = connectionSupplier;
38 public GetConfigCommand withCurrent() {
39 withCurrent.set(true);
44 public GetConfigCommand withDefaults() {
45 withDefaults.set(true);
50 public GetConfigCommand withSortOrder() {
51 withSortOrder.set(true);
56 public GetConfigCommand withExpertFlag() {
57 withExpertFlag.set(true);
62 public GetConfigCommand withForceWriteFlag() {
63 withForceWriteFlag.set(true);
68 public GetConfigCommand withShortDescription() {
69 withShortDescription.set(true);
74 public ListenableFuture<ConfigData> execute() {
75 return threadPool.submit(this::executeDialog);
78 private ConfigData executeDialog() throws IOException, ExecutionException, InterruptedException {
79 GetConfig getConfig = new GetConfig(new RandomIdentifierGenerator().generate());
80 getConfig.setWithCurrent(withCurrent.get());
81 getConfig.setWithDefaults(withDefaults.get());
82 getConfig.setWithSortOrder(withSortOrder.get());
83 getConfig.setWithExpertFlag(withExpertFlag.get());
84 getConfig.setWithForceWriteFlag(withForceWriteFlag.get());
85 getConfig.setWithShortDescription(withShortDescription.get());
86 try (GetConfigDialog getConfigDialog = new GetConfigDialog()) {
87 return getConfigDialog.send(getConfig).get();
91 private class GetConfigDialog extends FcpDialog<ConfigData> {
93 private final AtomicReference<ConfigData> configData = new AtomicReference<>();
95 public GetConfigDialog() throws IOException {
96 super(threadPool, connectionSupplier.get());
100 protected boolean isFinished() {
101 return configData.get() != null;
105 protected ConfigData getResult() {
106 return configData.get();
110 protected void consumeConfigData(ConfigData configData) {
111 this.configData.set(configData);