diff --git a/lib/src/main/java/org/czeal/rfc3986/Authority.java b/lib/src/main/java/org/czeal/rfc3986/Authority.java index 441f28a..cf22d21 100644 --- a/lib/src/main/java/org/czeal/rfc3986/Authority.java +++ b/lib/src/main/java/org/czeal/rfc3986/Authority.java @@ -353,7 +353,7 @@ public String toString() * The comparison is based on the values of {@code userinfo}, {@code host}, * and {@code port} components. * - * @param other + * @param obj * The object to be compared for equality with this {@link Authority} * object. * @@ -362,14 +362,24 @@ public String toString() * object. */ @Override - public boolean equals(Object other) + public boolean equals(Object obj) { - if (!(other instanceof Authority)) + if (this == obj) + { + return true; + } + + if (obj == null || getClass() != obj.getClass()) { return false; } - return compareTo((Authority)other) == 0; + Authority other = (Authority)obj; + + // Compare all components for equality. + return Objects.equals(this.userinfo, other.userinfo) && + Objects.equals(this.host, other.host) && + this.port == other.port; } diff --git a/lib/src/main/java/org/czeal/rfc3986/Host.java b/lib/src/main/java/org/czeal/rfc3986/Host.java index 6af4e59..34b5e0e 100644 --- a/lib/src/main/java/org/czeal/rfc3986/Host.java +++ b/lib/src/main/java/org/czeal/rfc3986/Host.java @@ -260,7 +260,7 @@ public String toString() * Compares this {@link Host} object with the specified object for equality. * The comparison is based on the type and value of this {@link Host} object. * - * @param other + * @param obj * The object to be compared for equality with this {@link Host} object. * * @return @@ -268,14 +268,23 @@ public String toString() * object. */ @Override - public boolean equals(Object other) + public boolean equals(Object obj) { - if (!(other instanceof Host)) + if (this == obj) + { + return true; + } + + if (obj == null || getClass() != obj.getClass()) { return false; } - return compareTo((Host)other) == 0; + Host other = (Host)obj; + + // Compare all components for equality. + return Objects.equals(this.type, other.type) && + Objects.equals(this.value, other.value); } diff --git a/lib/src/main/java/org/czeal/rfc3986/URIReference.java b/lib/src/main/java/org/czeal/rfc3986/URIReference.java index ba9413d..66f0bee 100644 --- a/lib/src/main/java/org/czeal/rfc3986/URIReference.java +++ b/lib/src/main/java/org/czeal/rfc3986/URIReference.java @@ -524,21 +524,33 @@ public String toString() * {@code path}, {@code query}, and {@code fragment} components. *

* - * @param other + * @param obj * The object to be compared for equality with this {@link URIReference}. * * @return * {@code true} if the specified object is equal to this {@link URIReference}. */ @Override - public boolean equals(Object other) + public boolean equals(Object obj) { - if (!(other instanceof URIReference)) + if (this == obj) + { + return true; + } + + if (obj == null || getClass() != obj.getClass()) { return false; } - return compareTo((URIReference)other) == 0; + URIReference other = (URIReference)obj; + + // Compare all components for equality. + return Objects.equals(this.scheme, other.scheme) && + Objects.equals(this.authority, other.authority) && + Objects.equals(this.path, other.path) && + Objects.equals(this.query, other.query) && + Objects.equals(this.fragment, other.fragment); } diff --git a/lib/src/test/java/org/czeal/rfc3986/AuthorityTest.java b/lib/src/test/java/org/czeal/rfc3986/AuthorityTest.java index 41cd375..de46b24 100644 --- a/lib/src/test/java/org/czeal/rfc3986/AuthorityTest.java +++ b/lib/src/test/java/org/czeal/rfc3986/AuthorityTest.java @@ -19,6 +19,7 @@ import static org.czeal.rfc3986.TestUtils.assertThrowsIAE; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.czeal.rfc3986.HostType.IPV4; import static org.czeal.rfc3986.HostType.IPV6; import static org.czeal.rfc3986.HostType.IPVFUTURE; @@ -134,4 +135,11 @@ public void test_toString() assertEquals("%65%78%61%6D%70%6C%65%2E%63%6F%6D", Authority.parse("%65%78%61%6D%70%6C%65%2E%63%6F%6D").toString()); assertEquals("", Authority.parse("").toString()); } + + + @Test + public void test_equals() + { + assertFalse(Authority.parse("FB").equals(Authority.parse("Ea"))); + } } diff --git a/lib/src/test/java/org/czeal/rfc3986/HostTest.java b/lib/src/test/java/org/czeal/rfc3986/HostTest.java index a3a195e..c043c97 100644 --- a/lib/src/test/java/org/czeal/rfc3986/HostTest.java +++ b/lib/src/test/java/org/czeal/rfc3986/HostTest.java @@ -19,6 +19,7 @@ import static org.czeal.rfc3986.TestUtils.assertThrowsIAE; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.czeal.rfc3986.HostType.IPV4; import static org.czeal.rfc3986.HostType.IPV6; import static org.czeal.rfc3986.HostType.IPVFUTURE; @@ -85,4 +86,11 @@ public void test_getValue() assertEquals("", Host.parse("").getValue()); assertEquals((String)null, Host.parse(null).getValue()); } + + + @Test + public void test_equals() + { + assertFalse(Host.parse("FB").equals(Host.parse("Ea"))); + } } diff --git a/lib/src/test/java/org/czeal/rfc3986/URIReferenceTest.java b/lib/src/test/java/org/czeal/rfc3986/URIReferenceTest.java index 8e0ae8a..143cd94 100644 --- a/lib/src/test/java/org/czeal/rfc3986/URIReferenceTest.java +++ b/lib/src/test/java/org/czeal/rfc3986/URIReferenceTest.java @@ -20,6 +20,7 @@ import static org.czeal.rfc3986.TestUtils.assertThrowsISE; import static org.czeal.rfc3986.TestUtils.assertThrowsNPE; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.czeal.rfc3986.HostType.IPV4; import static org.czeal.rfc3986.HostType.IPV6; @@ -858,6 +859,7 @@ public void test_equals() assertTrue(URIReference.parse("http:a").equals(URIReference.parse("http:a"))); assertTrue(URIReference.parse("//").equals(URIReference.parse("//"))); assertTrue(URIReference.parse("").equals(URIReference.parse(""))); + assertFalse(URIReference.parse("http://example.com/1ot").equals(URIReference.parse("http://example.com/1pU"))); }