|
32 | 32 |
|
33 | 33 | package net.thauvin.erik.httpstatus; |
34 | 34 |
|
35 | | -import java.util.Map; |
36 | | -import java.util.Objects; |
37 | | -import java.util.ResourceBundle; |
| 35 | +import java.util.*; |
38 | 36 | import java.util.concurrent.ConcurrentSkipListMap; |
39 | 37 |
|
40 | 38 | /** |
@@ -139,31 +137,55 @@ public static String getReasonPhrase(int statusCode) { |
139 | 137 | return getReasonPhrase(Integer.toString(statusCode)); |
140 | 138 | } |
141 | 139 |
|
| 140 | + private static boolean isStatusCodeClass(String code) { |
| 141 | + return code.matches("[1-5]xx"); |
| 142 | + } |
| 143 | + |
142 | 144 | /** |
143 | 145 | * Prints the reason phrase for the given status code(s). |
144 | 146 | * |
145 | 147 | * @param args The status code(s) or response class(es), prints all if none |
146 | 148 | */ |
147 | | - @SuppressWarnings("PMD.SystemPrintln") |
148 | 149 | 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); |
167 | 183 | } |
168 | 184 | } |
| 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 | + } |
169 | 191 | } |
0 commit comments