Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.

Commit eb5a959

Browse files
author
Marek Potociar
committed
Added support for equals/hashCode in our internal StatusType implementation.
- This improves compatibility with applications that try to compare our implementation class with JAX-RS Status enum. (There is still a problem of commutativity of the comparison though.) Change-Id: Iecada04df849ac31d12e4d43cdaa0b82944621da Signed-off-by: Marek Potociar <[email protected]>
1 parent 0214b7d commit eb5a959

File tree

1 file changed

+64
-6
lines changed
  • core-common/src/main/java/org/glassfish/jersey/message/internal

1 file changed

+64
-6
lines changed

core-common/src/main/java/org/glassfish/jersey/message/internal/Statuses.java

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33
*
4-
* Copyright (c) 2010-2012 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2010-2015 Oracle and/or its affiliates. All rights reserved.
55
*
66
* The contents of this file are subject to the terms of either the GNU
77
* General Public License Version 2 only ("GPL") or the Common Development
@@ -39,7 +39,7 @@
3939
*/
4040
package org.glassfish.jersey.message.internal;
4141

42-
import javax.ws.rs.core.Response.Status;
42+
import javax.ws.rs.core.Response;
4343
import javax.ws.rs.core.Response.Status.Family;
4444
import javax.ws.rs.core.Response.StatusType;
4545

@@ -53,9 +53,9 @@ public final class Statuses {
5353

5454
private static final class StatusImpl implements StatusType {
5555

56-
private int code;
57-
private String reason;
58-
private Family family;
56+
private final int code;
57+
private final String reason;
58+
private final Family family;
5959

6060
private StatusImpl(int code, String reason) {
6161
this.code = code;
@@ -82,17 +82,74 @@ public String toString() {
8282
public Family getFamily() {
8383
return family;
8484
}
85+
86+
@Override
87+
@SuppressWarnings("RedundantIfStatement")
88+
public boolean equals(final Object o) {
89+
if (this == o) {
90+
return true;
91+
}
92+
if (!(o instanceof StatusType)) {
93+
return false;
94+
}
95+
96+
final StatusType status = (StatusType) o;
97+
98+
if (code != status.getStatusCode()) {
99+
return false;
100+
}
101+
if (family != status.getFamily()) {
102+
return false;
103+
}
104+
if (reason != null ? !reason.equals(status.getReasonPhrase()) : status.getReasonPhrase() != null) {
105+
return false;
106+
}
107+
108+
return true;
109+
}
110+
111+
@Override
112+
public int hashCode() {
113+
int result = code;
114+
result = 31 * result + (reason != null ? reason.hashCode() : 0);
115+
result = 31 * result + family.hashCode();
116+
return result;
117+
}
85118
}
86119

120+
/**
121+
* Create a new status type instance.
122+
* <p>
123+
* For standard status codes listed in {@link javax.ws.rs.core.Response.Status} enum, the default reason phrase
124+
* is used. For any other status code an empty string is used as a reason phrase.
125+
* </p>
126+
*
127+
* @param code response status code.
128+
* @return new status type instance representing a given response status code.
129+
*/
87130
public static StatusType from(int code) {
88-
StatusType result = Status.fromStatusCode(code);
131+
StatusType result = Response.Status.fromStatusCode(code);
89132
return (result != null) ? result : new StatusImpl(code, "");
90133
}
91134

135+
/**
136+
* Create a new status type instance with a custom reason phrase.
137+
*
138+
* @param code response status code.
139+
* @param reason custom response status reason phrase.
140+
* @return new status type instance representing a given response status code and custom reason phrase.
141+
*/
92142
public static StatusType from(int code, String reason) {
93143
return new StatusImpl(code, reason);
94144
}
95145

146+
/**
147+
* Create a new status type instance with a custom reason phrase.
148+
*
149+
* @param status response status type.
150+
* @param reason custom response status reason phrase.
151+
* @return new status type instance representing a given response status code and custom reason phrase.
152+
*/
96153
public static StatusType from(StatusType status, String reason) {
97154
return new StatusImpl(status.getStatusCode(), reason);
98155
}
@@ -101,5 +158,6 @@ public static StatusType from(StatusType status, String reason) {
101158
* Prevents instantiation.
102159
*/
103160
private Statuses() {
161+
throw new AssertionError("Instantiation not allowed.");
104162
}
105163
}

0 commit comments

Comments
 (0)