Commit 245df47
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
1 file changed
+21
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
0 commit comments