From 7054562baba8c8e956bf2222d92a167466e36885 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 6 Sep 2024 14:05:22 +0200 Subject: [PATCH] =?utf8?q?=E2=99=BB=EF=B8=8F=20Use=20Stream=20API=20to=20f?= =?utf8?q?ilter=20returned=20peers?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../net/pterodactylus/fcp/highlevel/FcpClient.java | 75 +++++++++------------- 1 file changed, 31 insertions(+), 44 deletions(-) diff --git a/src/main/java/net/pterodactylus/fcp/highlevel/FcpClient.java b/src/main/java/net/pterodactylus/fcp/highlevel/FcpClient.java index 55d7d08..1dd6e9f 100644 --- a/src/main/java/net/pterodactylus/fcp/highlevel/FcpClient.java +++ b/src/main/java/net/pterodactylus/fcp/highlevel/FcpClient.java @@ -17,25 +17,7 @@ package net.pterodactylus.fcp.highlevel; -import static com.google.common.collect.FluentIterable.from; -import static java.util.stream.Collectors.toMap; - -import java.io.Closeable; -import java.io.IOException; -import java.io.InputStream; -import java.net.InetAddress; -import java.net.URL; -import java.net.UnknownHostException; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.atomic.AtomicReference; - +import com.google.common.base.Predicate; import net.pterodactylus.fcp.AddPeer; import net.pterodactylus.fcp.AddPeer.Trust; import net.pterodactylus.fcp.AddPeer.Visibility; @@ -79,7 +61,25 @@ import net.pterodactylus.fcp.SSKKeypair; import net.pterodactylus.fcp.SimpleProgress; import net.pterodactylus.fcp.WatchGlobal; -import com.google.common.base.Predicate; +import java.io.Closeable; +import java.io.IOException; +import java.io.InputStream; +import java.net.InetAddress; +import java.net.URL; +import java.net.UnknownHostException; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.atomic.AtomicReference; + +import static com.google.common.collect.FluentIterable.from; +import static java.util.stream.Collectors.toList; +import static java.util.stream.Collectors.toMap; /** * High-level FCP client that hides the details of the underlying FCP @@ -502,14 +502,10 @@ public class FcpClient implements Closeable { * if an FCP error occurs */ public Collection getDarknetPeers(boolean withMetadata, boolean withVolatile) throws IOException, FcpException { - Collection allPeers = getPeers(withMetadata, withVolatile); - Collection darknetPeers = new HashSet(); - for (Peer peer : allPeers) { - if (!peer.isOpennet() && !peer.isSeed()) { - darknetPeers.add(peer); - } - } - return darknetPeers; + return getPeers(withMetadata, withVolatile).stream() + .filter(peer -> !peer.isOpennet()) + .filter(peer -> !peer.isSeed()) + .collect(toList()); } /** @@ -526,14 +522,10 @@ public class FcpClient implements Closeable { * if an FCP error occurs */ public Collection getOpennetPeers(boolean withMetadata, boolean withVolatile) throws IOException, FcpException { - Collection allPeers = getPeers(withMetadata, withVolatile); - Collection opennetPeers = new HashSet(); - for (Peer peer : allPeers) { - if (peer.isOpennet() && !peer.isSeed()) { - opennetPeers.add(peer); - } - } - return opennetPeers; + return getPeers(withMetadata, withVolatile).stream() + .filter(Peer::isOpennet) + .filter(peer -> !peer.isSeed()) + .collect(toList()); } /** @@ -550,14 +542,9 @@ public class FcpClient implements Closeable { * if an FCP error occurs */ public Collection getSeedPeers(boolean withMetadata, boolean withVolatile) throws IOException, FcpException { - Collection allPeers = getPeers(withMetadata, withVolatile); - Collection seedPeers = new HashSet(); - for (Peer peer : allPeers) { - if (peer.isSeed()) { - seedPeers.add(peer); - } - } - return seedPeers; + return getPeers(withMetadata, withVolatile).stream() + .filter(Peer::isSeed) + .collect(toList()); } /** -- 2.7.4