|
| 1 | +package com.ibrahimatay; |
| 2 | + |
| 3 | +public class JEP361SwitchExpressions { |
| 4 | + public static void main(String[] args) { |
| 5 | + // Switch Expressions (JEP 361) |
| 6 | + // https://javaalmanac.io/features/switch/ |
| 7 | + |
| 8 | + // JEP 361: Switch Expressions |
| 9 | + // https://openjdk.org/jeps/361 |
| 10 | + |
| 11 | + System.out.printf("%1$s HTTP status code refers to a %2$s%n", 100, getHTTPCodeDesc(100)); |
| 12 | + System.out.printf("%1$s HTTP status code refers to a %2$s%n", 200, getHTTPCodeDesc(200)); |
| 13 | + System.out.printf("%1$s HTTP status code refers to a %2$s%n", 403, getHTTPCodeDesc(403)); |
| 14 | + System.out.printf("%1$s HTTP status code refers to a %2$s%n", 0, getHTTPCodeDesc(0)); |
| 15 | + |
| 16 | + /* |
| 17 | + 100 HTTP status code refers to a Continue |
| 18 | + 200 HTTP status code refers to a OK |
| 19 | + 403 HTTP status code refers to a Client Error |
| 20 | + 0 HTTP status code refers to a Unknown error |
| 21 | + */ |
| 22 | + } |
| 23 | + |
| 24 | + public static String getHTTPCodeDesc(int code){ |
| 25 | + return switch(code) { |
| 26 | + case 100 -> "Continue"; |
| 27 | + case 200 -> "OK"; |
| 28 | + case 301 -> "Moved Permanently"; |
| 29 | + case 302 -> "Found"; |
| 30 | + case 400 -> "Bad Request"; |
| 31 | + case 500 -> "Internal Server Error"; |
| 32 | + case 502 -> "Bad Gateway"; |
| 33 | + default -> { |
| 34 | + if(code > 100 && code < 200 ) yield "Informational"; |
| 35 | + if(code > 200 && code < 300) yield "Successful"; |
| 36 | + if(code > 302 && code < 400) yield "Redirection"; |
| 37 | + if(code > 400 && code < 500) yield "Client Error"; |
| 38 | + if(code > 502 && code < 600 ) yield "Server Error"; |
| 39 | + yield "Unknown error"; |
| 40 | + } |
| 41 | + }; |
| 42 | + } |
| 43 | +} |
0 commit comments