-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from trishagee/java21
Java 21
- Loading branch information
Showing
5 changed files
with
98 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
java-samples/src/main/java/com/jetbrains/code/jdk21/Point.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
} |
28 changes: 28 additions & 0 deletions
28
java-samples/src/main/java/com/jetbrains/code/jdk21/RecordPatterns.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
9 changes: 9 additions & 0 deletions
9
java-samples/src/main/java/com/jetbrains/code/jdk21/StringTemplate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
java-samples/src/test/com/jetbrains/code/jdk21/RecordPatternsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |