From 06db1231f64976bd5b7b3bf9d851f179ddfaeeaa Mon Sep 17 00:00:00 2001 From: Jon Marius Venstad Date: Fri, 8 Apr 2022 11:57:13 +0200 Subject: Unwrap recursion --- vespajlib/src/main/java/ai/vespa/http/HttpURL.java | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'vespajlib') diff --git a/vespajlib/src/main/java/ai/vespa/http/HttpURL.java b/vespajlib/src/main/java/ai/vespa/http/HttpURL.java index 5527d877a17..9641ea2a8fd 100644 --- a/vespajlib/src/main/java/ai/vespa/http/HttpURL.java +++ b/vespajlib/src/main/java/ai/vespa/http/HttpURL.java @@ -8,7 +8,6 @@ import java.net.URISyntaxException; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.Deque; import java.util.LinkedHashMap; import java.util.List; @@ -466,16 +465,19 @@ public class HttpURL { /** Returns a copy of this without any key-value pair with the decoded key. */ public Query remove(String key) { - Node node = without(key::equals, head); + Node node = without(key::equals); return node == head ? this : new Query(node, validator); } - private static Node without(Predicate filter, Node node) { - if (node == null) return node; // null does not contain match filter - Node child = without(filter, node.next); // get a child that does not match filter - if (filter.test(node.key)) return child; // if we match the filter, unlink us - if (child == node.next) return node; // if our next didn't change, return unchanged - return new Node(child, node.key, node.value); // if our next has changed, we must change too + private Node without(Predicate filter) { + Node head = null; + boolean changed = false; + for (Node node : nodes()) { + if (filter.test(node.key)) changed = true; // skip node if filter applies + else head = changed ? new Node(head, node.key, node.value) // if our tail has changed, so must we + : node; // otherwise, return us unchanged + } + return head; } /** Returns a copy of this with all given mappings appended to this. {@code null} values, but not lists of values, are allowed. */ @@ -501,7 +503,7 @@ public class HttpURL { /** Returns a copy of this with all given keys removed. */ public Query remove(Collection keys) { - Node node = without(keys::contains, head); + Node node = without(keys::contains); return node == head ? this : new Query(node, validator); } -- cgit v1.2.3