From a19115d4828d45336fae5b9e407fe55f16686696 Mon Sep 17 00:00:00 2001 From: Chalard Jean Date: Thu, 13 Feb 2020 16:39:05 +0900 Subject: [PATCH] [NS D01] Remove candidates that don't satisfy the request. This is exactly equivalent to the previous version (though a bit more expensive) but is useful for followup changes. See [NS D03] to see a sample of how this will be used. Bug: 113554781 Test: FrameworksNetTests Change-Id: I39f3c248bd2f23f7b22bd89d2e1e031653fe9ddb --- .../com/android/server/connectivity/NetworkRanker.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/services/core/java/com/android/server/connectivity/NetworkRanker.java b/services/core/java/com/android/server/connectivity/NetworkRanker.java index d0aabf95d5..1ae7dc5c36 100644 --- a/services/core/java/com/android/server/connectivity/NetworkRanker.java +++ b/services/core/java/com/android/server/connectivity/NetworkRanker.java @@ -20,6 +20,7 @@ import android.annotation.NonNull; import android.annotation.Nullable; import android.net.NetworkRequest; +import java.util.ArrayList; import java.util.Collection; /** @@ -31,15 +32,15 @@ public class NetworkRanker { /** * Find the best network satisfying this request among the list of passed networks. */ - // Almost equivalent to Collections.max(nais), but allows returning null if no network - // satisfies the request. @Nullable public NetworkAgentInfo getBestNetwork(@NonNull final NetworkRequest request, @NonNull final Collection nais) { + final ArrayList candidates = new ArrayList<>(nais); + candidates.removeIf(nai -> !nai.satisfies(request)); + NetworkAgentInfo bestNetwork = null; int bestScore = Integer.MIN_VALUE; - for (final NetworkAgentInfo nai : nais) { - if (!nai.satisfies(request)) continue; + for (final NetworkAgentInfo nai : candidates) { if (nai.getCurrentScore() > bestScore) { bestNetwork = nai; bestScore = nai.getCurrentScore();