Skip to content

Commit

Permalink
Merge pull request #3 from hidebike712/bugfix/fix-equals-method
Browse files Browse the repository at this point in the history
[bugfix] fix equals() methods in some classes
  • Loading branch information
hidebike712 authored Dec 25, 2024
2 parents 6884665 + 0900b45 commit e759603
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 12 deletions.
18 changes: 14 additions & 4 deletions lib/src/main/java/org/czeal/rfc3986/Authority.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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;
}


Expand Down
17 changes: 13 additions & 4 deletions lib/src/main/java/org/czeal/rfc3986/Host.java
Original file line number Diff line number Diff line change
Expand Up @@ -260,22 +260,31 @@ 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
* {@code true} if the specified object is equal to this {@link Host}
* 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);
}


Expand Down
20 changes: 16 additions & 4 deletions lib/src/main/java/org/czeal/rfc3986/URIReference.java
Original file line number Diff line number Diff line change
Expand Up @@ -524,21 +524,33 @@ public String toString()
* {@code path}, {@code query}, and {@code fragment} components.
* </p>
*
* @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);
}


Expand Down
8 changes: 8 additions & 0 deletions lib/src/test/java/org/czeal/rfc3986/AuthorityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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")));
}
}
8 changes: 8 additions & 0 deletions lib/src/test/java/org/czeal/rfc3986/HostTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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")));
}
}
2 changes: 2 additions & 0 deletions lib/src/test/java/org/czeal/rfc3986/URIReferenceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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")));
}


Expand Down

0 comments on commit e759603

Please sign in to comment.