Skip to content

Commit

Permalink
Merge pull request #29 from trishagee/java21
Browse files Browse the repository at this point in the history
Java 21
  • Loading branch information
helenjoscott authored Mar 5, 2024
2 parents d3ba0e3 + 234a083 commit b383e98
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,35 +1,39 @@
package com.jetbrains.code.jdk.preview;
package com.jetbrains.code.jdk21;

import java.util.List;

/**
* <ul>
* <li>#PreviewFeature First Preview #JDK17</li>
* <li>#StandardFeature #JDK21</li>
* </ul>
* See: <a href="https://openjdk.org/jeps/441">JEP 441</a>
*/
@SuppressWarnings("unused")
public class PatternMatchingForSwitch {
static String exampleJEP406OriginalCode(Object o) {
// Prior to Java 21
static String exampleJEP411OriginalCode(Object obj) {
String formatted = "unknown";
if (o instanceof Integer i) {
if (obj instanceof Integer i) {
formatted = String.format("int %d", i);
} else if (o instanceof Long l) {
} else if (obj instanceof Long l) {
formatted = String.format("long %d", l);
} else if (o instanceof Double d) {
} else if (obj instanceof Double d) {
formatted = String.format("double %f", d);
} else if (o instanceof String s) {
} else if (obj instanceof String s) {
formatted = String.format("String %s", s);
}
return formatted;
}

static String exampleJEP406PatternMatchingForSwitch(Object o) {
return switch (o) {
// As of Java 21
static String formatterPatternSwitch(Object obj) {
return switch (obj) {
case Integer i -> String.format("int %d", i);
case Long l -> String.format("long %d", l);
case Double d -> String.format("double %f", d);
case String s -> String.format("String %s", s);
default -> o.toString();
default -> obj.toString();
};
}

Expand Down Expand Up @@ -65,4 +69,20 @@ static void testFooBar(String s) {
default -> System.out.println("Ok");
}
}

// As of Java 21
static void testStringNew(String response) {
switch (response) {
case null -> {}
case "y", "Y" -> System.out.println("You got it");
case "n", "N" -> System.out.println("Shame");
case String s
when s.equalsIgnoreCase("YES") -> System.out.println("You got it");
case String s
when s.equalsIgnoreCase("NO") -> System.out.println("Shame");
case String s -> {
System.out.println("Sorry?");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.jetbrains.code.jdk21;

record Point(int x, int y) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.jetbrains.code.jdk21;

/**
* <ul>
* <li>#StandardFeature #JDK21</li>
* </ul>
* See: <a href="https://openjdk.org/jeps/440">JEP 440</a>
*/
public class RecordPatterns {

// Not sure why, but the inspection doesn't seem to trigger for this code
static int sum(Object obj) {
if (obj instanceof Point p) {
int x = p.x();
int y = p.y();
return (x + y);
}
return 0;
}

static int sumUpdated(Object obj) {
if (obj instanceof Point(int x, int y)) {
return (x+y);
}
return 0;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.jetbrains.code.jdk21;

@SuppressWarnings("unused")
public class StringTemplate {
private void templateCanBeUsed() {
String name = "Bob";
String greeting = "Hello, " + name + ". You are " + 29 + " years old.";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.jetbrains.code.jdk21;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

class RecordPatternsTest {
@Test
@DisplayName("Should sum point values")
void shouldSum() {
// when
int sum = RecordPatterns.sum(new Point(1, 2));

// then
Assertions.assertEquals(3, sum);
}

@Test
@DisplayName("Should sum points with JDK21")
void shouldSumPointsWithJdk21() {
// when
int sum = RecordPatterns.sumUpdated(new Point(1, 2));

// then
Assertions.assertEquals(3, sum);
}

}

0 comments on commit b383e98

Please sign in to comment.