From c45bbfca5379227de2d4923214076cfd8e4b527c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Prante?= Date: Wed, 3 Jan 2024 13:30:57 +0100 Subject: [PATCH] remove our immutable list from Parameter, add copy constructor to URLBuilder --- gradle.properties | 2 +- net/src/main/java/org/xbib/net/Parameter.java | 27 ++++++++----------- .../java/org/xbib/net/ParameterBuilder.java | 13 +++------ net/src/main/java/org/xbib/net/URL.java | 15 ++++++++++- .../main/java/org/xbib/net/URLBuilder.java | 20 ++++++++++++++ .../java/org/xbib/net/URLBuilderTest.java | 8 ++++++ .../test/java/org/xbib/net/URLParserTest.java | 2 +- 7 files changed, 59 insertions(+), 28 deletions(-) diff --git a/gradle.properties b/gradle.properties index 12ef6a2..934b51f 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ group = org.xbib name = net -version = 4.0.3 +version = 4.0.4 diff --git a/net/src/main/java/org/xbib/net/Parameter.java b/net/src/main/java/org/xbib/net/Parameter.java index a53887f..4d96e0b 100644 --- a/net/src/main/java/org/xbib/net/Parameter.java +++ b/net/src/main/java/org/xbib/net/Parameter.java @@ -10,7 +10,6 @@ import java.util.Map; import java.util.Objects; import java.util.Optional; import java.util.stream.Collectors; -import org.xbib.datastructures.common.ImmutableList; import org.xbib.datastructures.common.LinkedHashSetMultiMap; import org.xbib.datastructures.common.MultiMap; import org.xbib.datastructures.common.Pair; @@ -30,15 +29,11 @@ public class Parameter implements Iterable>, Comparable> list; - private final String queryString; Parameter(ParameterBuilder builder, - ImmutableList> list, String queryString) { this.builder = builder; - this.list = list; this.queryString = queryString; } @@ -64,24 +59,24 @@ public class Parameter implements Iterable>, Comparable> iterator() { - return list.iterator(); + return builder.list.iterator(); } @Override public boolean equals(Object o) { return o instanceof Parameter && builder.domain.equals(((Parameter) o).builder.domain) && - list.equals(((Parameter) o).list); + builder.list.equals(((Parameter) o).builder.list); } @Override public int hashCode() { - return Objects.hash(builder.domain, list); + return Objects.hash(builder.domain, builder.list); } @Override public int compareTo(Parameter o) { - return list.toString().compareTo(o.list.toString()); + return builder.list.toString().compareTo(o.builder.list.toString()); } @Override @@ -158,7 +153,7 @@ public class Parameter implements Iterable>, Comparable asMultiMap() throws ParameterException{ @@ -188,7 +183,7 @@ public class Parameter implements Iterable>, Comparable>, Comparable>, Comparable p.getKey().equals(key)) .map(Pair::getValue) .collect(Collectors.toList()); @@ -251,7 +246,7 @@ public class Parameter implements Iterable>, Comparable key.equals(p.getKey())); } return false; @@ -269,7 +264,7 @@ public class Parameter implements Iterable>, Comparable optional = list.stream() + Optional optional = builder.list.stream() .filter(p -> key.equals(p.getKey())) .map(Pair::getValue) .findFirst(); @@ -286,7 +281,7 @@ public class Parameter implements Iterable>, Comparable sb.append(" ").append(key).append(" -> ").append(value)); return sb.toString(); } diff --git a/net/src/main/java/org/xbib/net/ParameterBuilder.java b/net/src/main/java/org/xbib/net/ParameterBuilder.java index 26129db..1dc9d6b 100644 --- a/net/src/main/java/org/xbib/net/ParameterBuilder.java +++ b/net/src/main/java/org/xbib/net/ParameterBuilder.java @@ -1,7 +1,6 @@ package org.xbib.net; import java.io.UncheckedIOException; -import java.lang.reflect.Array; import java.nio.charset.Charset; import java.nio.charset.CodingErrorAction; import java.nio.charset.MalformedInputException; @@ -16,23 +15,19 @@ import java.util.Locale; import java.util.Map; import java.util.Objects; -import org.xbib.datastructures.common.ImmutableList; import org.xbib.datastructures.common.MultiMap; import org.xbib.datastructures.common.Pair; import org.xbib.datastructures.common.PairValidator; public class ParameterBuilder implements PairValidator { - @SuppressWarnings("unchecked") - private static final Pair[] EMPTY_PAIR = (Pair[]) Array.newInstance(Pair.class, 0); - private static final char AMPERSAND_CHAR = '&'; private static final char EQUAL_CHAR = '='; private static final Integer MAX_PARAMS_IN_QUERY_STRING = 1024; - private final List> list; + final List> list; protected Parameter.Domain domain; @@ -72,8 +67,8 @@ public class ParameterBuilder implements PairValidator { } ParameterBuilder(ParameterBuilder builder) { - this.list = builder.list; - this.parameterMap = builder.parameterMap; + this.list = new ArrayList<>(builder.list); + this.parameterMap = new HashMap<>(builder.parameterMap); this.domain = builder.domain; this.limit = builder.limit; this.charset = builder.charset; @@ -266,7 +261,7 @@ public class ParameterBuilder implements PairValidator { throw new UncheckedIOException(e); } } - Parameter parameter = new Parameter(this, ImmutableList.of(list, EMPTY_PAIR), queryString); + Parameter parameter = new Parameter(this, queryString); if (parameterValidator != null) { return parameterValidator.apply(parameter); } else { diff --git a/net/src/main/java/org/xbib/net/URL.java b/net/src/main/java/org/xbib/net/URL.java index 77b3932..6a50edb 100755 --- a/net/src/main/java/org/xbib/net/URL.java +++ b/net/src/main/java/org/xbib/net/URL.java @@ -67,7 +67,7 @@ public class URL implements Comparable { static final String DOUBLE_SLASH = "//"; - private final transient URLBuilder builder; + final transient URLBuilder builder; private final transient Scheme scheme; @@ -111,6 +111,10 @@ public class URL implements Comparable { return new URLBuilder(); } + public static URLBuilder builder(URL url) { + return new URLBuilder(url.builder); + } + public static URLBuilder http() { return new URLBuilder().scheme(Scheme.HTTP); } @@ -144,6 +148,15 @@ public class URL implements Comparable { return NULL_URL; } + /** + * Copy URL. + * @param url the url + * @return a new URL object + */ + public static URL from(URL url) { + return builder(url).build(); + } + public static URL from(String input) { return from(input, StandardCharsets.UTF_8, CodingErrorAction.REPORT, true, false); } diff --git a/net/src/main/java/org/xbib/net/URLBuilder.java b/net/src/main/java/org/xbib/net/URLBuilder.java index d65792d..0283d55 100644 --- a/net/src/main/java/org/xbib/net/URLBuilder.java +++ b/net/src/main/java/org/xbib/net/URLBuilder.java @@ -66,6 +66,26 @@ public class URLBuilder { charset(StandardCharsets.UTF_8, CodingErrorAction.REPLACE); } + URLBuilder(URLBuilder builder) { + regNameEncoder = builder.regNameEncoder; + percentEncoder = builder.percentEncoder; + percentDecoder = builder.percentDecoder; + queryParams = Parameter.builder(builder.queryParams); + pathSegments = builder.pathSegments; + charset = builder.charset; + codingErrorAction = builder.codingErrorAction; + scheme = builder.scheme; + schemeSpecificPart = builder.schemeSpecificPart; + userInfo = builder.userInfo; + host = builder.host; + hostAddress = builder.hostAddress; + protocolVersion = builder.protocolVersion; + port = builder.port; + query = builder.query; + fragment = builder.fragment; + fatalResolveErrorsEnabled = builder.fatalResolveErrorsEnabled; + } + /** * Set the character set of the URL. Default is UTF-8. * diff --git a/net/src/test/java/org/xbib/net/URLBuilderTest.java b/net/src/test/java/org/xbib/net/URLBuilderTest.java index 4f4adfd..b17287a 100644 --- a/net/src/test/java/org/xbib/net/URLBuilderTest.java +++ b/net/src/test/java/org/xbib/net/URLBuilderTest.java @@ -276,6 +276,14 @@ class URLBuilderTest { assertEquals("/foobar?a%25=b%25&c=%20d%20", url.relativeReference()); assertEquals("https://google.com:8008/foobar?a%25=b%25&c=%20d%20", url.toExternalForm()); assertEquals("https://google.com:8008/foobar?a%=b%&c= d ", url.toString()); + + // test inheritance of params added by mutator + URL url1 = URL.from("http://example.com"); + url1.mutator().queryParams.add("a", "b"); + assertEquals("http://example.com?a=b", url1.toString()); + URL url2 = URL.from(url1); // copy + url2.mutator().queryParams.add("c", "d"); + assertEquals("http://example.com?a=b&c=d", url2.toString()); } @Test diff --git a/net/src/test/java/org/xbib/net/URLParserTest.java b/net/src/test/java/org/xbib/net/URLParserTest.java index bbc9d7a..d85a2af 100644 --- a/net/src/test/java/org/xbib/net/URLParserTest.java +++ b/net/src/test/java/org/xbib/net/URLParserTest.java @@ -18,7 +18,7 @@ class URLParserTest { @Test void testNull() { - assertEquals(URL.nullUrl(), URL.from(null)); + assertEquals(URL.nullUrl(), URL.from((String) null)); } @Test