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