Refactor FCP dialog
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / quelaton / ListPeersCommandImpl.java
1 package net.pterodactylus.fcp.quelaton;
2
3 import java.io.IOException;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.HashSet;
7 import java.util.concurrent.ExecutionException;
8 import java.util.concurrent.ExecutorService;
9 import java.util.concurrent.atomic.AtomicBoolean;
10 import java.util.function.Supplier;
11
12 import net.pterodactylus.fcp.EndListPeers;
13 import net.pterodactylus.fcp.ListPeers;
14 import net.pterodactylus.fcp.Peer;
15
16 import com.google.common.util.concurrent.ListenableFuture;
17 import com.google.common.util.concurrent.ListeningExecutorService;
18 import com.google.common.util.concurrent.MoreExecutors;
19
20 /**
21  * Default {@link ListPeersCommand} implementation based on {@link FcpDialog}.
22  *
23  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
24  */
25 public class ListPeersCommandImpl implements ListPeersCommand {
26
27         private final ListeningExecutorService threadPool;
28         private final ConnectionSupplier connectionSupplier;
29         private final Supplier<String> identifierGenerator;
30         private final AtomicBoolean includeMetadata = new AtomicBoolean(false);
31         private final AtomicBoolean includeVolatile = new AtomicBoolean(false);
32
33         public ListPeersCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier, Supplier<String> identifierGenerator) {
34                 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
35                 this.connectionSupplier = connectionSupplier;
36                 this.identifierGenerator = identifierGenerator;
37         }
38
39         @Override
40         public ListPeersCommand includeMetadata() {
41                 includeMetadata.set(true);
42                 return this;
43         }
44
45         @Override
46         public ListPeersCommand includeVolatile() {
47                 includeVolatile.set(true);
48                 return this;
49         }
50
51         @Override
52         public ListenableFuture<Collection<Peer>> execute() {
53                 return threadPool.submit(this::executeDialog);
54         }
55
56         private Collection<Peer> executeDialog() throws InterruptedException, ExecutionException, IOException {
57                 ListPeers listPeers = new ListPeers(identifierGenerator.get(), includeMetadata.get(), includeVolatile.get());
58                 try (ListPeersDialog listPeersDialog = new ListPeersDialog()) {
59                         return listPeersDialog.send(listPeers).get();
60                 }
61         }
62
63         private class ListPeersDialog extends FcpDialog<Collection<Peer>> {
64
65                 private final Collection<Peer> peers = new HashSet<>();
66
67                 public ListPeersDialog() throws IOException {
68                         super(threadPool, connectionSupplier.get(), Collections.<Peer>emptyList());
69                 }
70
71                 @Override
72                 protected void consumePeer(Peer peer) {
73                         peers.add(peer);
74                 }
75
76                 @Override
77                 protected void consumeEndListPeers(EndListPeers endListPeers) {
78                         setResult(peers);
79                 }
80
81         }
82
83 }