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;
8 import java.util.function.Supplier;
10 import net.pterodactylus.fcp.ConfigData;
11 import net.pterodactylus.fcp.GetConfig;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.ListeningExecutorService;
15 import com.google.common.util.concurrent.MoreExecutors;
18 * Default {@link GetConfigCommand} implementation based on {@link FcpDialog}.
20 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
22 public class GetConfigCommandImpl implements GetConfigCommand {
24 private final ListeningExecutorService threadPool;
25 private final ConnectionSupplier connectionSupplier;
26 private final Supplier<String> identifierGenerator;
27 private final AtomicBoolean withCurrent = new AtomicBoolean();
28 private final AtomicBoolean withDefaults = new AtomicBoolean();
29 private final AtomicBoolean withSortOrder = new AtomicBoolean();
30 private final AtomicBoolean withExpertFlag = new AtomicBoolean();
31 private final AtomicBoolean withForceWriteFlag = new AtomicBoolean();
32 private final AtomicBoolean withShortDescription = new AtomicBoolean();
33 private final AtomicBoolean withLongDescription = new AtomicBoolean();
34 private final AtomicBoolean withDataTypes = new AtomicBoolean();
36 public GetConfigCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier, Supplier<String> identifierGenerator) {
37 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
38 this.connectionSupplier = connectionSupplier;
39 this.identifierGenerator = identifierGenerator;
43 public GetConfigCommand withCurrent() {
44 withCurrent.set(true);
49 public GetConfigCommand withDefaults() {
50 withDefaults.set(true);
55 public GetConfigCommand withSortOrder() {
56 withSortOrder.set(true);
61 public GetConfigCommand withExpertFlag() {
62 withExpertFlag.set(true);
67 public GetConfigCommand withForceWriteFlag() {
68 withForceWriteFlag.set(true);
73 public GetConfigCommand withShortDescription() {
74 withShortDescription.set(true);
79 public GetConfigCommand withLongDescription() {
80 withLongDescription.set(true);
85 public GetConfigCommand withDataTypes() {
86 withDataTypes.set(true);
91 public ListenableFuture<ConfigData> execute() {
92 return threadPool.submit(this::executeDialog);
95 private ConfigData executeDialog() throws IOException, ExecutionException, InterruptedException {
96 GetConfig getConfig = new GetConfig(identifierGenerator.get());
97 getConfig.setWithCurrent(withCurrent.get());
98 getConfig.setWithDefaults(withDefaults.get());
99 getConfig.setWithSortOrder(withSortOrder.get());
100 getConfig.setWithExpertFlag(withExpertFlag.get());
101 getConfig.setWithForceWriteFlag(withForceWriteFlag.get());
102 getConfig.setWithShortDescription(withShortDescription.get());
103 getConfig.setWithLongDescription(withLongDescription.get());
104 getConfig.setWithDataTypes(withDataTypes.get());
105 try (GetConfigDialog getConfigDialog = new GetConfigDialog()) {
106 return getConfigDialog.send(getConfig).get();
110 private class GetConfigDialog extends FcpDialog<ConfigData> {
112 private final AtomicReference<ConfigData> configData = new AtomicReference<>();
114 public GetConfigDialog() throws IOException {
115 super(threadPool, connectionSupplier.get());
119 protected boolean isFinished() {
120 return configData.get() != null;
124 protected ConfigData getResult() {
125 return configData.get();
129 protected void consumeConfigData(ConfigData configData) {
130 this.configData.set(configData);