Skip to content

Commit 6a62526

Browse files
committed
feat(Test): add demo mapping HTTP codes to descriptions with switch expression
What - Added Test class with main method printing: - "Hello World" - Description of HTTP status code 404 via getHTTPCodeDescWithSwitch(). - Implemented getHTTPCodeDescWithSwitch(int code) using modern switch expression. - Cases: - Exact matches: 100, 200, 301, 302, 400, 500, 502. - Ranges via pattern + when clauses (preview feature / requires Java 21+): - 100–199 → "Informational" - 200–299 → "Successful" - 303–399 → "Redirection" - 401–499 → "Client Error" - 503–599 → "Server Error" - default → "Unknown Error". Why - Demonstrates switch expressions for clean mapping of HTTP status codes to human-readable messages. - Shows advanced feature: pattern matching with when clauses for ranges. - Provides concise alternative to large if-else chains or verbose traditional switch blocks. How - Used switch expression to return values directly as strings. - Exact status codes mapped to standard HTTP reason phrases. - Pattern-based cases handle broader ranges for grouped categories. - Default branch ensures all other codes yield "Unknown Error". Logic - Inputs: integer HTTP code. - Outputs: string description. - Flow: 1. Match exact codes first. 2. If no match, test range-based cases. 3. If no match, fall back to default. - Example: - Input: 404. - Matches case int i when i > 400 && i < 500. - Returns "Client Error". - Edge cases: - Requires preview features in Java 21+ (pattern matching for switch). - In earlier Java versions, range-based cases must be expressed with if-else or separate logic. - Complexity / performance: O(1) branching; very efficient. - Concurrency / thread-safety: stateless, thread-safe. - Error handling: Default case ensures coverage for all unmatched inputs. Real-life applications - Converting numeric codes to readable labels (HTTP statuses, error codes). - Replacing lookup tables or verbose branching in APIs or web frameworks. - Useful for logging, debugging, and API responses. Notes - case int i when ... syntax is only valid with preview features in Java 21+; not available in Java 12–20. - For backward compatibility, ranges can be implemented using if-else inside default branch. - Switch expressions eliminate need for break and directly yield results. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 4eef884 commit 6a62526

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
public class Test {
2+
public static void main(String[] args) {
3+
System.out.println("Hello World");
4+
System.out.println(getHTTPCodeDescWithSwitch(404));
5+
}
6+
public static String getHTTPCodeDescWithSwitch(int code) {
7+
return switch (code) {
8+
case 100 -> "Continue";
9+
case 200 -> "Ok";
10+
case 301 -> "Moved Permanently";
11+
case 302 -> "Found";
12+
case 400 -> "Bad Request";
13+
case 500 -> "Internal Error";
14+
case 502 -> "Bad Gateway";
15+
16+
17+
case int i when i > 100 && i < 200 -> "Informational";
18+
case int i when i > 200 && i < 300 -> "Successful";
19+
case int i when i > 302 && i < 400 -> "Redirection";
20+
case int i when i > 400 && i < 500 -> "Client Error";
21+
case int i when i > 502 && i < 600 -> "Server Error";
22+
default -> "Unknown Error";
23+
};
24+
}
25+
}

0 commit comments

Comments
 (0)