|
| 1 | +import java.util.List; |
| 2 | +import java.util.Locale; |
| 3 | + |
| 4 | +/** |
| 5 | + * Inspired by examples in https://openjdk.org/jeps/427 |
| 6 | + */ |
| 7 | +public class SwitchPatternPreview4OK { |
| 8 | + public static void main(String[] args) { |
| 9 | + |
| 10 | + System.out.println(formatterPatternSwitch(null)); |
| 11 | + System.out.println(formatterPatternSwitch(123)); |
| 12 | + System.out.println(formatterPatternSwitch(999L)); |
| 13 | + System.out.println(formatterPatternSwitch(12.34)); |
| 14 | + System.out.println(formatterPatternSwitch("foo")); |
| 15 | + System.out.println(formatterPatternSwitch(List.of(123, "foo", 999L, 12.34))); |
| 16 | + |
| 17 | + System.out.println(testCircle(new Rectangle())); |
| 18 | + System.out.println(testCircle(new Circle(5))); |
| 19 | + System.out.println(testCircle(new Circle(6))); |
| 20 | + |
| 21 | + System.out.println(testSealedCoverage(new A())); |
| 22 | + System.out.println(testSealedCoverage(new B())); |
| 23 | + System.out.println(testSealedCoverage(new C(5))); |
| 24 | + |
| 25 | +// constantLabelMustAppearBeforePattern(-1); |
| 26 | +// constantLabelMustAppearBeforePattern(0); |
| 27 | +// constantLabelMustAppearBeforePattern(42); |
| 28 | +// constantLabelMustAppearBeforePattern(-99); |
| 29 | +// constantLabelMustAppearBeforePattern(Integer.valueOf(123)); |
| 30 | +// constantLabelMustAppearBeforePattern(null); |
| 31 | + |
| 32 | + // TODO: Activate when https://github.com/eclipse-jdt/eclipse.jdt.core/issues/1466 is fixed. |
| 33 | + /* |
| 34 | + constantLabelMustAppearBeforePatternInteger(-1); |
| 35 | + constantLabelMustAppearBeforePatternInteger(0); |
| 36 | + constantLabelMustAppearBeforePatternInteger(42); |
| 37 | + constantLabelMustAppearBeforePatternInteger(-99); |
| 38 | + constantLabelMustAppearBeforePatternInteger(Integer.valueOf(123)); |
| 39 | + constantLabelMustAppearBeforePatternInteger(null); |
| 40 | + */ |
| 41 | + |
| 42 | + System.out.println(testGenericSealedExhaustive(new E<Integer>())); |
| 43 | + } |
| 44 | + |
| 45 | + static String formatterPatternSwitch(Object o) { |
| 46 | + return switch (o) { |
| 47 | + case null -> "null"; |
| 48 | + case Integer i -> String.format("int %d", i); |
| 49 | + case Long l -> String.format("long %d", l); |
| 50 | + case Double d -> String.format(Locale.ENGLISH, "double %f", d); |
| 51 | + case String s -> String.format("String %s", s); |
| 52 | + default -> o.toString(); |
| 53 | + }; |
| 54 | + } |
| 55 | + |
| 56 | + static class Shape {} |
| 57 | + static class Rectangle extends Shape {} |
| 58 | + static class Circle extends Shape { |
| 59 | + private final double radius; |
| 60 | + public Circle(double radius) { this.radius = radius; } |
| 61 | + double calculateArea() { return Math.PI * radius * radius; } |
| 62 | + } |
| 63 | + |
| 64 | + static String testCircle(Shape s) { |
| 65 | + return switch (s) { |
| 66 | + case Circle c when (c.calculateArea() > 100) -> "Large circle"; |
| 67 | + case Circle c -> "Small circle"; |
| 68 | + default -> "Non-circle"; |
| 69 | + }; |
| 70 | + } |
| 71 | + |
| 72 | + sealed interface S permits A, B, C {} |
| 73 | + final static class A implements S {} |
| 74 | + final static class B implements S {} |
| 75 | + static record C(int i) implements S {} // Implicitly final |
| 76 | + |
| 77 | + static String testSealedCoverage(S s) { |
| 78 | + return switch (s) { |
| 79 | + case A a -> "Sealed sub-class A"; |
| 80 | + case B b -> "Sealed sub-class B"; |
| 81 | + case C c -> "Sealed sub-record C"; |
| 82 | + }; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * According to an example from JEP 420, this should work, but it does not, neither with Javac nor ECJ. |
| 87 | + * |
| 88 | + * See: |
| 89 | + * https://openjdk.java.net/jeps/420#1b--Dominance-of-pattern-labels |
| 90 | + * https://bugs.openjdk.java.net/browse/JDK-8273326 |
| 91 | + * https://bugs.eclipse.org/bugs/show_bug.cgi?id=579355 |
| 92 | + * |
| 93 | + * TODO: reactivate when implemented or move to preview 5 with Java 21, Eclipse 4.28. |
| 94 | + */ |
| 95 | +/* |
| 96 | + static String constantLabelMustAppearBeforePattern(Object o) { |
| 97 | + switch (o) { |
| 98 | + case null -> System.out.println("value unavailable: " + o); |
| 99 | + case -1, 1 -> System.out.println("special case:" + o); |
| 100 | + case Integer value when value > 0 -> System.out.println("positive integer: " + o); |
| 101 | + case Integer i -> System.out.println("other integer: " + o); |
| 102 | + default -> System.out.println("non-integer: " + o); |
| 103 | + } |
| 104 | + return o == null ? "null" : o.toString(); |
| 105 | + } |
| 106 | +*/ |
| 107 | + |
| 108 | + /** |
| 109 | + * This used to work in preview 4 (Java 20), but fails during runtime with |
| 110 | + * java.lang.IndexOutOfBoundsException: Index 4 out of bounds for length 4 |
| 111 | + * in ECJ 3.36.0-SNAPSHOT with Java 21. |
| 112 | + * See: |
| 113 | + * https://github.com/eclipse-jdt/eclipse.jdt.core/issues/1466. |
| 114 | + * |
| 115 | + * TODO: Activate when https://github.com/eclipse-jdt/eclipse.jdt.core/issues/1466 is fixed. |
| 116 | + */ |
| 117 | + static String constantLabelMustAppearBeforePatternInteger(Integer i) { |
| 118 | + switch (i) { |
| 119 | + case null -> System.out.println("value unavailable: " + i); |
| 120 | + case -1, 1 -> System.out.println("absolute value 1: " + i); |
| 121 | + case Integer value when value > 0 -> System.out.println("positive integer: " + i); |
| 122 | + default -> System.out.println("other integer: " + i); |
| 123 | + } |
| 124 | + return i == null ? "null" : i.toString(); |
| 125 | + } |
| 126 | + |
| 127 | + static void nullCanAppearAfterConstantLabel(Integer i) { |
| 128 | + switch (i) { |
| 129 | + case -1, 1 -> System.out.println("absolute value 1: " + i); |
| 130 | + case null -> System.out.println("value unavailable: " + i); |
| 131 | + case Integer value when value > 0 -> System.out.println("positive integer: " + i); |
| 132 | + default -> System.out.println("other integer: " + i); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * According to an example from JEP 420, this should work with preview 2 (Java 18), and it does with Javac, |
| 138 | + * but not with ECJ for Java 18, 19 and 20. |
| 139 | + * |
| 140 | + * See: |
| 141 | + * https://openjdk.java.net/jeps/420#2--Exhaustiveness-of-switch-expressions-and-statements |
| 142 | + * https://bugs.eclipse.org/bugs/show_bug.cgi?id=579360 |
| 143 | + * https://github.com/eclipse-jdt/eclipse.jdt.core/issues/587 |
| 144 | + * |
| 145 | + * TODO: reactivate when implemented or move to preview 5 with Java 21. |
| 146 | + */ |
| 147 | + sealed interface I<T> permits D, E {} |
| 148 | + final static class D<X> implements I<String> {} |
| 149 | + final static class E<Y> implements I<Y> {} |
| 150 | + |
| 151 | + static int testGenericSealedExhaustive(I<Integer> i) { |
| 152 | + return switch (i) { |
| 153 | + // Exhaustive as no D case possible! |
| 154 | + case E<Integer> bi -> 42; |
| 155 | + }; |
| 156 | + } |
| 157 | +} |
0 commit comments