Skip to content

Commit 29b061b

Browse files
committed
Refactor the main method
1 parent 3046b11 commit 29b061b

1 file changed

Lines changed: 44 additions & 22 deletions

File tree

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

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@
3232

3333
package net.thauvin.erik.httpstatus;
3434

35-
import java.util.Map;
36-
import java.util.Objects;
37-
import java.util.ResourceBundle;
35+
import java.util.*;
3836
import java.util.concurrent.ConcurrentSkipListMap;
3937

4038
/**
@@ -139,31 +137,55 @@ public static String getReasonPhrase(int statusCode) {
139137
return getReasonPhrase(Integer.toString(statusCode));
140138
}
141139

140+
private static boolean isStatusCodeClass(String code) {
141+
return code.matches("[1-5]xx");
142+
}
143+
142144
/**
143145
* Prints the reason phrase for the given status code(s).
144146
*
145147
* @param args The status code(s) or response class(es), prints all if none
146148
*/
147-
@SuppressWarnings("PMD.SystemPrintln")
148149
public static void main(String... args) {
149-
if (args.length >= 1) {
150-
for (var arg : args) {
151-
if (arg.matches("[1-5]xx")) { // e.g.: 2xx
152-
var reasonClass = StatusCodeClass.fromFirstDigit(arg.substring(0, 1));
153-
if (reasonClass.isPresent()) {
154-
var reasons = getReasonClass(reasonClass.get());
155-
reasons.forEach((k, v) -> System.out.println(k + ": " + v));
156-
}
157-
} else { // e.g.: 404
158-
var value = REASON_PHRASES.get(arg);
159-
if (value != null) {
160-
System.out.println(arg + ": " + value);
161-
}
162-
}
163-
}
164-
} else { // Print all
165-
REASON_PHRASES.keySet().forEach(k -> System.out.println(k + ": " + REASON_PHRASES.get(k)));
166-
System.out.println("Total: " + REASON_PHRASES.size());
150+
if (args.length == 0) {
151+
printAllReasonPhrases();
152+
return;
153+
}
154+
155+
Arrays.stream(args).forEach(Reasons::processStatusCode);
156+
}
157+
158+
@SuppressWarnings("PMD.SystemPrintln")
159+
private static void printAllReasonPhrases() {
160+
REASON_PHRASES.forEach(Reasons::printReasonPhrase);
161+
System.out.println("Total: " + REASON_PHRASES.size());
162+
}
163+
164+
private static void printReasonClassPhrases(Map<String, String> reasons) {
165+
reasons.forEach(Reasons::printReasonPhrase);
166+
}
167+
168+
@SuppressWarnings("PMD.SystemPrintln")
169+
private static void printReasonPhrase(String code, String phrase) {
170+
System.out.println(code + ": " + phrase);
171+
}
172+
173+
private static void printSingleStatusCode(String code) {
174+
Optional.ofNullable(REASON_PHRASES.get(code))
175+
.ifPresent(phrase -> printReasonPhrase(code, phrase));
176+
}
177+
178+
private static void processStatusCode(String code) {
179+
if (isStatusCodeClass(code)) {
180+
processStatusCodeClass(code);
181+
} else {
182+
printSingleStatusCode(code);
167183
}
168184
}
185+
186+
private static void processStatusCodeClass(String code) {
187+
var firstDigit = code.substring(0, 1);
188+
StatusCodeClass.fromFirstDigit(firstDigit)
189+
.ifPresent(reasonClass -> printReasonClassPhrases(getReasonClass(reasonClass)));
190+
}
169191
}

0 commit comments

Comments
 (0)