summaryrefslogtreecommitdiffstats
path: root/vespajlib
diff options
context:
space:
mode:
authorJon Marius Venstad <jonmv@gmail.com>2022-04-08 11:57:13 +0200
committerJon Marius Venstad <jonmv@gmail.com>2022-04-08 11:57:13 +0200
commit06db1231f64976bd5b7b3bf9d851f179ddfaeeaa (patch)
tree5f0b5aae2207701d9fcb2e69e45e119855f87f89 /vespajlib
parent55d1186ed64c35b27f7a6acae2821b81c648c377 (diff)
Unwrap recursion
Diffstat (limited to 'vespajlib')
-rw-r--r--vespajlib/src/main/java/ai/vespa/http/HttpURL.java20
1 files changed, 11 insertions, 9 deletions
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 <em>decoded</em> 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<String> 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<String> 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<String> keys) {
- Node node = without(keys::contains, head);
+ Node node = without(keys::contains);
return node == head ? this : new Query(node, validator);
}