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();
30 public GetConfigCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
31 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
32 this.connectionSupplier = connectionSupplier;
36 public GetConfigCommand withCurrent() {
37 withCurrent.set(true);
42 public GetConfigCommand withDefaults() {
43 withDefaults.set(true);
48 public GetConfigCommand withSortOrder() {
49 withSortOrder.set(true);
54 public GetConfigCommand withExpertFlag() {
55 withExpertFlag.set(true);
60 public ListenableFuture<ConfigData> execute() {
61 return threadPool.submit(this::executeDialog);
64 private ConfigData executeDialog() throws IOException, ExecutionException, InterruptedException {
65 GetConfig getConfig = new GetConfig(new RandomIdentifierGenerator().generate());
66 getConfig.setWithCurrent(withCurrent.get());
67 getConfig.setWithDefaults(withDefaults.get());
68 getConfig.setWithSortOrder(withSortOrder.get());
69 getConfig.setWithExpertFlag(withExpertFlag.get());
70 try (GetConfigDialog getConfigDialog = new GetConfigDialog()) {
71 return getConfigDialog.send(getConfig).get();
75 private class GetConfigDialog extends FcpDialog<ConfigData> {
77 private final AtomicReference<ConfigData> configData = new AtomicReference<>();
79 public GetConfigDialog() throws IOException {
80 super(threadPool, connectionSupplier.get());
84 protected boolean isFinished() {
85 return configData.get() != null;
89 protected ConfigData getResult() {
90 return configData.get();
94 protected void consumeConfigData(ConfigData configData) {
95 this.configData.set(configData);