Use a single identifier generator in all commands
[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.HashSet;
6 import java.util.concurrent.ExecutionException;
7 import java.util.concurrent.ExecutorService;
8 import java.util.concurrent.atomic.AtomicBoolean;
9 import java.util.function.Supplier;
10
11 import net.pterodactylus.fcp.EndListPeers;
12 import net.pterodactylus.fcp.ListPeers;
13 import net.pterodactylus.fcp.Peer;
14
15 import com.google.common.util.concurrent.ListenableFuture;
16 import com.google.common.util.concurrent.ListeningExecutorService;
17 import com.google.common.util.concurrent.MoreExecutors;
18
19 /**
20  * Default {@link ListPeersCommand} implementation based on {@link FcpDialog}.
21  *
22  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
23  */
24 public class ListPeersCommandImpl implements ListPeersCommand {
25
26         private final ListeningExecutorService threadPool;
27         private final ConnectionSupplier connectionSupplier;
28         private final Supplier<String> identifierGenerator;
29         private final AtomicBoolean includeMetadata = new AtomicBoolean(false);
30         private final AtomicBoolean includeVolatile = new AtomicBoolean(false);
31
32         public ListPeersCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier, Supplier<String> identifierGenerator) {
33                 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
34                 this.connectionSupplier = connectionSupplier;
35                 this.identifierGenerator = identifierGenerator;
36         }
37
38         @Override
39         public ListPeersCommand includeMetadata() {
40                 includeMetadata.set(true);
41                 return this;
42         }
43
44         @Override
45         public ListPeersCommand includeVolatile() {
46                 includeVolatile.set(true);
47                 return this;
48         }
49
50         @Override
51         public ListenableFuture<Collection<Peer>> execute() {
52                 return threadPool.submit(this::executeDialog);
53         }
54
55         private Collection<Peer> executeDialog() throws InterruptedException, ExecutionException, IOException {
56                 ListPeers listPeers = new ListPeers(identifierGenerator.get(), includeMetadata.get(), includeVolatile.get());
57                 try (ListPeersDialog listPeersDialog = new ListPeersDialog()) {
58                         return listPeersDialog.send(listPeers).get();
59                 }
60         }
61
62         private class ListPeersDialog extends FcpDialog<Collection<Peer>> {
63
64                 private final Collection<Peer> peers = new HashSet<>();
65                 private final AtomicBoolean finished = new AtomicBoolean(false);
66
67                 public ListPeersDialog() throws IOException {
68                         super(threadPool, connectionSupplier.get());
69                 }
70
71                 @Override
72                 protected boolean isFinished() {
73                         return finished.get();
74                 }
75
76                 @Override
77                 protected Collection<Peer> getResult() {
78                         return peers;
79                 }
80
81                 @Override
82                 protected void consumePeer(Peer peer) {
83                         peers.add(peer);
84                 }
85
86                 @Override
87                 protected void consumeEndListPeers(EndListPeers endListPeers) {
88                         finished.set(true);
89                 }
90
91         }
92
93 }