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.function.Supplier;
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 Supplier<String> identifierGenerator;
26 private final AtomicBoolean withCurrent = new AtomicBoolean();
27 private final AtomicBoolean withDefaults = new AtomicBoolean();
28 private final AtomicBoolean withSortOrder = new AtomicBoolean();
29 private final AtomicBoolean withExpertFlag = new AtomicBoolean();
30 private final AtomicBoolean withForceWriteFlag = new AtomicBoolean();
31 private final AtomicBoolean withShortDescription = new AtomicBoolean();
32 private final AtomicBoolean withLongDescription = new AtomicBoolean();
33 private final AtomicBoolean withDataTypes = new AtomicBoolean();
35 public GetConfigCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier, Supplier<String> identifierGenerator) {
36 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
37 this.connectionSupplier = connectionSupplier;
38 this.identifierGenerator = identifierGenerator;
42 public GetConfigCommand withCurrent() {
43 withCurrent.set(true);
48 public GetConfigCommand withDefaults() {
49 withDefaults.set(true);
54 public GetConfigCommand withSortOrder() {
55 withSortOrder.set(true);
60 public GetConfigCommand withExpertFlag() {
61 withExpertFlag.set(true);
66 public GetConfigCommand withForceWriteFlag() {
67 withForceWriteFlag.set(true);
72 public GetConfigCommand withShortDescription() {
73 withShortDescription.set(true);
78 public GetConfigCommand withLongDescription() {
79 withLongDescription.set(true);
84 public GetConfigCommand withDataTypes() {
85 withDataTypes.set(true);
90 public ListenableFuture<ConfigData> execute() {
91 return threadPool.submit(this::executeDialog);
94 private ConfigData executeDialog() throws IOException, ExecutionException, InterruptedException {
95 GetConfig getConfig = new GetConfig(identifierGenerator.get());
96 getConfig.setWithCurrent(withCurrent.get());
97 getConfig.setWithDefaults(withDefaults.get());
98 getConfig.setWithSortOrder(withSortOrder.get());
99 getConfig.setWithExpertFlag(withExpertFlag.get());
100 getConfig.setWithForceWriteFlag(withForceWriteFlag.get());
101 getConfig.setWithShortDescription(withShortDescription.get());
102 getConfig.setWithLongDescription(withLongDescription.get());
103 getConfig.setWithDataTypes(withDataTypes.get());
104 try (GetConfigDialog getConfigDialog = new GetConfigDialog()) {
105 return getConfigDialog.send(getConfig).get();
109 private class GetConfigDialog extends FcpDialog<ConfigData> {
111 public GetConfigDialog() throws IOException {
112 super(threadPool, connectionSupplier.get(), null);
116 protected void consumeConfigData(ConfigData configData) {
117 setResult(configData);