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();
28 public GetConfigCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
29 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
30 this.connectionSupplier = connectionSupplier;
34 public GetConfigCommand withCurrent() {
35 withCurrent.set(true);
40 public GetConfigCommand withDefaults() {
41 withDefaults.set(true);
46 public ListenableFuture<ConfigData> execute() {
47 return threadPool.submit(this::executeDialog);
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();
59 private class GetConfigDialog extends FcpDialog<ConfigData> {
61 private final AtomicReference<ConfigData> configData = new AtomicReference<>();
63 public GetConfigDialog() throws IOException {
64 super(threadPool, connectionSupplier.get());
68 protected boolean isFinished() {
69 return configData.get() != null;
73 protected ConfigData getResult() {
74 return configData.get();
78 protected void consumeConfigData(ConfigData configData) {
79 this.configData.set(configData);