Skip to content

Commit 245df47

Browse files
committed
feat(Test2): add classic if-else mapping for HTTP status codes
What - Added Test2 class with main method printing: - "Hello World" - Description of HTTP status code 404 via getHTTPCodeDescClassic(). - Implemented getHTTPCodeDescClassic(int code) using chained if-else conditions. - Handled cases: - Exact matches for 100, 200, 301, 302, 400, 500, 502. - Range checks: - 101–199 → "Informational" - 201–299 → "Successful" - 303–399 → "Redirection" - 401–499 → "Client Error" - 503–599 → "Server Error" - Default fallback → "Unknown Error". Why - Provides baseline implementation using traditional if-else branching for status code mapping. - Serves as comparison with modern switch expressions introduced in Java 12+. - Useful for environments that don’t yet support enhanced switch features. How - Used sequential if-else checks with return statements. - Evaluates exact matches before range checks. - Returns appropriate description or default error string. Logic - Inputs: integer HTTP code. - Outputs: string description. - Flow: 1. If code matches exact known values → return description. 2. Else evaluate range conditions. 3. Else return "Unknown Error". - Example: - Input: 404. - Skips exact matches. - Matches condition (code > 400 && code < 500). - Returns "Client Error". - Edge cases: - Overlapping or adjacent ranges are handled carefully to avoid double matches. - Range excludes exact cases already handled above. - Complexity / performance: O(1) in practice since number of conditions is constant. - Concurrency / thread-safety: Stateless method; safe in all contexts. - Error handling: Default return ensures all codes have coverage. Real-life applications - Simple utilities for translating HTTP codes into human-readable text. - Logging frameworks, debugging tools, or client libraries. - Educational example to contrast procedural if-else vs functional switch expressions. Notes - For large mappings, using Map<Integer,String> or switch expression is cleaner and more maintainable. - Output for 404 in this implementation: "Client Error". - This is the most backward-compatible approach, working in all Java versions. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 5510b7b commit 245df47

File tree

1 file changed

+21
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)