Skip to content

Commit 95f8296

Browse files
committed
Refactor getReasonPhrase method to be more generic
1 parent 1efd124 commit 95f8296

3 files changed

Lines changed: 38 additions & 50 deletions

File tree

src/main/java/net/thauvin/erik/httpstatus/Reasons.java

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@
3232

3333
package net.thauvin.erik.httpstatus;
3434

35-
import java.util.*;
35+
import java.util.Arrays;
36+
import java.util.Map;
37+
import java.util.Optional;
38+
import java.util.ResourceBundle;
3639
import java.util.concurrent.ConcurrentSkipListMap;
3740

3841
/**
@@ -81,8 +84,9 @@ private Reasons() {
8184
*/
8285
public static Map<String, String> getReasonClass(StatusCodeClass reasonClass) {
8386
var reasons = new ConcurrentSkipListMap<String, String>();
87+
var firstDigit = String.valueOf(reasonClass.getFirstDigit());
8488
REASON_PHRASES.keySet().forEach(k -> {
85-
if (k.startsWith(reasonClass.getFirstDigit())) {
89+
if (k.startsWith(firstDigit)) {
8690
reasons.put(k, REASON_PHRASES.get(k));
8791
}
8892
});
@@ -92,49 +96,28 @@ public static Map<String, String> getReasonClass(StatusCodeClass reasonClass) {
9296
/**
9397
* Returns the reason phrase for the specified status code.
9498
*
99+
* @param <T> The type of the status code ({@link String} or {@link Integer})
95100
* @param statusCode The status code for which the reason phrase is to be retrieved
96-
* @return The reason phrase, or {@code null}
101+
* @return The reason phrase or null if not match is found
97102
*/
98-
public static String getReasonPhrase(String statusCode) {
99-
return REASON_PHRASES.get(statusCode);
100-
}
101-
102-
103-
/**
104-
* Returns the reason phrase for the specified status code. If no reason phrase is found, the default reason is
105-
* returned.
106-
*
107-
* @param statusCode The status code for which the reason phrase is to be retrieved
108-
* @param defaultReason The default reason phrase to return if no reason phrase is found for the given status code
109-
* @return The reason phrase, or the default reason if no match is found
110-
* @since 2.0.0
111-
*/
112-
public static String getReasonPhrase(String statusCode, String defaultReason) {
113-
var reason = getReasonPhrase(statusCode);
114-
return Objects.requireNonNullElse(reason, defaultReason);
115-
}
116-
117-
/**
118-
* Returns the reason phrase for the specified status code. If no reason phrase is found, the default reason is
119-
* returned.
120-
*
121-
* @param statusCode The status code for which the reason phrase is to be retrieved
122-
* @param defaultReason The default reason phrase to return if no reason phrase is found for the given status code
123-
* @return The reason phrase, or the default reason if no match is found
124-
* @since 2.0.0
125-
*/
126-
public static String getReasonPhrase(int statusCode, String defaultReason) {
127-
return getReasonPhrase(Integer.toString(statusCode), defaultReason);
103+
public static <T> String getReasonPhrase(T statusCode) {
104+
return REASON_PHRASES.get(toStatusCodeString(statusCode));
128105
}
129106

130107
/**
131108
* Returns the reason phrase for the specified status code.
132109
*
133-
* @param statusCode The status code for which the reason phrase is to be retrieved
134-
* @return The reason phrase, or {@code null}
110+
* @param <T> The type of the status code ({@link String} or {@link Integer})
111+
* @param statusCode The status code for which the reason phrase is to be retrieved
112+
* @param defaultReason The default reason phrase to return if no reason phrase is found
113+
* @return The reason phrase, or the default reason if no match is found, or null if no default provided
135114
*/
136-
public static String getReasonPhrase(int statusCode) {
137-
return getReasonPhrase(Integer.toString(statusCode));
115+
public static <T> String getReasonPhrase(T statusCode, String defaultReason) {
116+
var reason = REASON_PHRASES.get(toStatusCodeString(statusCode));
117+
if (reason == null) {
118+
return defaultReason;
119+
}
120+
return reason;
138121
}
139122

140123
private static boolean isStatusCodeClass(String code) {
@@ -185,7 +168,13 @@ private static void processStatusCode(String code) {
185168

186169
private static void processStatusCodeClass(String code) {
187170
var firstDigit = code.substring(0, 1);
188-
StatusCodeClass.fromFirstDigit(firstDigit)
171+
StatusCodeClass.fromFirstDigit(Integer.parseInt(firstDigit))
189172
.ifPresent(reasonClass -> printReasonClassPhrases(getReasonClass(reasonClass)));
190173
}
174+
175+
private static <T> String toStatusCodeString(T statusCode) {
176+
return (statusCode instanceof Integer)
177+
? Integer.toString((Integer) statusCode)
178+
: String.valueOf(statusCode);
179+
}
191180
}

src/main/resources/net/thauvin/erik/httpstatus/reasons.properties

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3030
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3131
#
32-
3332
100=Continue
3433
101=Switching Protocols
3534
102=Processing

src/test/java/net/thauvin/erik/httpstatus/ReasonsTests.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ private static Stream<String> provideInformationalReasonKeys() {
131131
return reasons.keySet().stream();
132132
}
133133

134+
private static Stream<String> provideRedirectionReasonKeys() {
135+
var reasons = Reasons.getReasonClass(StatusCodeClass.REDIRECTION);
136+
return reasons.keySet().stream();
137+
}
138+
134139
private static Stream<String> provideServerErrorReasonKeys() {
135140
var reasons = Reasons.getReasonClass(StatusCodeClass.SERVER_ERROR);
136141
return reasons.keySet().stream();
@@ -141,11 +146,6 @@ private static Stream<String> provideSuccessfulReasonKeys() {
141146
return reasons.keySet().stream();
142147
}
143148

144-
private static Stream<String> provideRedirectionReasonKeys() {
145-
var reasons = Reasons.getReasonClass(StatusCodeClass.REDIRECTION);
146-
return reasons.keySet().stream();
147-
}
148-
149149
@ParameterizedTest
150150
@MethodSource("provideClientErrorReasonKeys")
151151
void reasonClassClientError(String key) {
@@ -158,6 +158,12 @@ void reasonClassInformational(String key) {
158158
assertThat(key).startsWith("1");
159159
}
160160

161+
@ParameterizedTest
162+
@MethodSource("provideRedirectionReasonKeys")
163+
void reasonClassRedirection(String key) {
164+
assertThat(key).startsWith("3");
165+
}
166+
161167
@ParameterizedTest
162168
@MethodSource("provideServerErrorReasonKeys")
163169
void reasonClassServerError(String key) {
@@ -169,11 +175,5 @@ void reasonClassServerError(String key) {
169175
void reasonClassSuccessful(String key) {
170176
assertThat(key).startsWith("2");
171177
}
172-
173-
@ParameterizedTest
174-
@MethodSource("provideRedirectionReasonKeys")
175-
void reasonClassRedirection(String key) {
176-
assertThat(key).startsWith("3");
177-
}
178178
}
179179
}

0 commit comments

Comments
 (0)