Skip to content

Commit

Permalink
Merge pull request #27737 from sberyozkin/resteasy_reactive_statustyp…
Browse files Browse the repository at this point in the history
…e_equals

Add RESTEasy Reactive StatusType equals and hashCode methods
  • Loading branch information
sberyozkin authored Sep 7, 2022
2 parents 1aa2224 + 6d6574e commit 83b3699
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jboss.resteasy.reactive.common.jaxrs;

import java.util.Objects;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status.Family;
import javax.ws.rs.core.Response.StatusType;
Expand All @@ -13,7 +14,16 @@ public class StatusTypeImpl implements StatusType {

public StatusTypeImpl(int status, String reasonPhrase) {
this.status = status;
this.reasonPhrase = reasonPhrase != null ? reasonPhrase : DEFAULT_REASON_PHRASE;
this.reasonPhrase = reasonPhrase != null ? reasonPhrase : getDefaultReasonPhrase(status);
}

private static String getDefaultReasonPhrase(int providedStatus) {
for (Response.Status defaultStatus : Response.Status.values()) {
if (providedStatus == defaultStatus.getStatusCode()) {
return defaultStatus.getReasonPhrase() != null ? defaultStatus.getReasonPhrase() : DEFAULT_REASON_PHRASE;
}
}
return DEFAULT_REASON_PHRASE;
}

@Override
Expand All @@ -31,6 +41,20 @@ public String getReasonPhrase() {
return reasonPhrase;
}

@Override
public boolean equals(Object other) {
if (!(other instanceof StatusType)) {
return false;
}
return this.status == ((StatusType) other).getStatusCode()
&& this.reasonPhrase.equals(((StatusType) other).getReasonPhrase());
}

@Override
public int hashCode() {
return Objects.hashCode(status) + 37 * reasonPhrase.hashCode();
}

public static StatusTypeImpl valueOf(StatusType statusType) {
if (statusType instanceof StatusTypeImpl)
return (StatusTypeImpl) statusType;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.jboss.resteasy.reactive.common.jaxrs;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

import javax.ws.rs.core.Response;
import org.junit.jupiter.api.Test;

public class StatusTypeImplTest {
@Test
public void testEquals() {
Response.StatusType statusType = new StatusTypeImpl(200, null);
assertEquals(statusType, Response.Status.OK);
}

@Test
public void testNotEquals() {
Response.StatusType statusType = new StatusTypeImpl(200, "All works OK");
assertNotEquals(statusType, Response.Status.OK);
}
}

0 comments on commit 83b3699

Please sign in to comment.