File tree Expand file tree Collapse file tree 4 files changed +31
-6
lines changed
main/java/pl/mperor/lab/java/sealed
test/java/pl/mperor/lab/java Expand file tree Collapse file tree 4 files changed +31
-6
lines changed Original file line number Diff line number Diff line change 11package pl .mperor .lab .java .sealed ;
22
3- public sealed interface Sealed permits AlsoSealed , NonSealed , Final {
3+ public sealed interface Sealed permits AlsoSealed , Final , NonSealed {
44
55 default String sealedMethod () {
66 return "sealed" ;
Original file line number Diff line number Diff line change @@ -35,12 +35,18 @@ private String getTypeOfDayByNumber(Integer dayOfWeek) {
3535 }
3636
3737 @ Test
38- public void testPatternMatchingInstanceOf () {
38+ public void testPatternMatchingInstanceOfAkaSmartCasting () {
3939 Object o = "Hello String!" ;
4040 if (o instanceof String s ) {
4141 Assertions .assertNotNull (s );
4242 Assertions .assertInstanceOf (String .class , s );
4343 }
44+ Assertions .assertTrue (nonEmptyString ("Hello World!" ));
45+ Assertions .assertFalse (nonEmptyString (null ));
46+ }
47+
48+ private static boolean nonEmptyString (Object obj ) {
49+ return (obj instanceof String str ) && !str .isEmpty ();
4450 }
4551
4652 @ Test
Original file line number Diff line number Diff line change 1010public class Java15 {
1111
1212 @ Test
13- public void testSealedClasses () {
13+ public void testSealedClassesAndInterfaces () {
1414 Assertions .assertTrue (Sealed .class .isSealed ());
1515 Assertions .assertFalse (NonSealed .class .isSealed ());
1616 Assertions .assertFalse (Final .class .isSealed ());
@@ -20,14 +20,35 @@ public void testSealedClasses() {
2020 Assertions .assertEquals ("final" , switchSealed (new Final ()));
2121 Assertions .assertEquals ("non-sealed" , switchSealed (new NonSealed ()));
2222 Assertions .assertEquals ("sealed" , switchSealed (new AlsoSealed ()));
23+
24+ SealedClient client = new SealedClient (new NonSealedChild ());
25+ Assertions .assertEquals ("non-sealed" , client .call ());
2326 }
2427
25- private String switchSealed (Sealed sealed ) {
28+ private static String switchSealed (Sealed sealed ) {
2629 return switch (sealed ) {
2730 case AlsoSealed a -> a .alsoSealedMethod ();
2831 case Final f -> f .finalMethod ();
2932 case NonSealed ns -> ns .nonSealedMethod ();
3033 };
3134 }
3235
36+ private static class NonSealedChild extends NonSealed {
37+ }
38+
39+ // not permitted
40+ // private static class SealedChild implements Sealed {
41+ // }
42+ public static class SealedClient {
43+ private Sealed s ;
44+
45+ public SealedClient (Sealed s ) {
46+ this .s = s ;
47+ }
48+
49+ public String call () {
50+ return switchSealed (s );
51+ }
52+ }
53+
3354}
Original file line number Diff line number Diff line change 33import org .junit .jupiter .api .Assertions ;
44import org .junit .jupiter .api .Disabled ;
55import org .junit .jupiter .api .Test ;
6- import org .junit .jupiter .api .condition .DisabledIf ;
7- import org .junit .jupiter .api .condition .EnabledIfSystemProperty ;
86import pl .mperor .lab .TestUtils ;
97
108import java .io .IOException ;
You can’t perform that action at this time.
0 commit comments