3232
3333package 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 ;
3639import 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}
0 commit comments