Skip to content

Commit 48a8f7e

Browse files
committed
[JAVA23][JAVA24] - Adding feature about Java 23 and 24.
Removing String Template
1 parent 0a77522 commit 48a8f7e

File tree

16 files changed

+193
-114
lines changed

16 files changed

+193
-114
lines changed

README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,9 @@ Here I will include some news about each version of Java, perhaps the most impor
5151
- [x] Java 22
5252
- Stream Gatherers
5353
- Statements before super(...)
54-
54+
- [x] Java 23
55+
- Primitive Types in Patterns, instanceof, and switch (Preview)
56+
- [x] Java 24
57+
- Stream Gatherers final version
58+
- [ ] Java 25
59+
- WIP

java-improvements/pom.xml

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
<version>1.0-SNAPSHOT</version>
1616

1717
<properties>
18-
<maven.compiler.source>22</maven.compiler.source>
19-
<maven.compiler.target>22</maven.compiler.target>
18+
<maven.compiler.source>24</maven.compiler.source>
19+
<maven.compiler.target>24</maven.compiler.target>
2020
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2121
</properties>
2222
<build>
@@ -25,8 +25,8 @@
2525
<groupId>org.apache.maven.plugins</groupId>
2626
<artifactId>maven-compiler-plugin</artifactId>
2727
<configuration>
28-
<source>22</source>
29-
<target>22</target>
28+
<source>24</source>
29+
<target>24</target>
3030
<compilerArgs>--enable-preview</compilerArgs>
3131
</configuration>
3232
</plugin>

java-improvements/src/main/java/com/bomfim/improvements/StringJson.java

+19-19
Original file line numberDiff line numberDiff line change
@@ -65,24 +65,24 @@ public String writeJsonTextBlock(PokemonRecord pokemon) {
6565
pokemon.species());
6666
}
6767

68-
/**
69-
* Using String template: JEP 430: String Templates (Preview)
70-
* From Java 21
71-
*
72-
* @param pokemon pokemon to display in Json
73-
* @return json.
74-
*/
75-
public String writePokemonStringTemplate(PokemonRecord pokemon) {
76-
return STR. """
77-
{
78-
"id": \{ pokemon.id() },
79-
"name": "\{ pokemon.name() }",
80-
"type": [
81-
"\{ String.join("\",\"", pokemon.type()) }"
82-
],
83-
"species": "\{ pokemon.species() }",
84-
}
85-
""" ;
86-
}
68+
// /**
69+
// * Using String template: JEP 430: String Templates (Preview)
70+
// * From Java 21
71+
// *
72+
// * @param pokemon pokemon to display in Json
73+
// * @return json.
74+
// */
75+
// public String writePokemonStringTemplate(PokemonRecord pokemon) {
76+
// return STR. """
77+
// {
78+
// "id": \{ pokemon.id() },
79+
// "name": "\{ pokemon.name() }",
80+
// "type": [
81+
// "\{ String.join("\",\"", pokemon.type()) }"
82+
// ],
83+
// "species": "\{ pokemon.species() }",
84+
// }
85+
// """ ;
86+
// }
8787

8888
}

java-improvements/src/main/java/com/bomfim/improvements/StringPoem.java

+26-26
Original file line numberDiff line numberDiff line change
@@ -81,31 +81,31 @@ public String writePoemTextBlock(String author) {
8181
.concat(author);
8282
}
8383

84-
/**
85-
* Using String template: JEP 430: String Templates (Preview)
86-
* From Java 21
87-
*
88-
* @param author author from the poem.
89-
* @return poem.
90-
*/
91-
public String writePoemStringTemplate(String author) {
92-
return STR."""
93-
Over hill, over dale,
94-
Thorough bush, thorough brier,
95-
Over park, over pale,
96-
Thorough flood, thorough fire!
97-
I do wander everywhere,
98-
Swifter than the moon's sphere;
99-
And I serve the Fairy Queen,
100-
To dew her orbs upon the green;
101-
The cowslips tall her pensioners be;
102-
In their gold coats spots you see;
103-
Those be rubies, fairy favours;
104-
In those freckles live their savours;
105-
I must go seek some dewdrops here,
106-
And hang a pearl in every cowslip's ear.
107-
- \{author}
108-
""";
109-
}
84+
// /**
85+
// * Using String template: JEP 430: String Templates (Preview)
86+
// * From Java 21
87+
// *
88+
// * @param author author from the poem.
89+
// * @return poem.
90+
// */
91+
// public String writePoemStringTemplate(String author) {
92+
// return STR."""
93+
// Over hill, over dale,
94+
// Thorough bush, thorough brier,
95+
// Over park, over pale,
96+
// Thorough flood, thorough fire!
97+
// I do wander everywhere,
98+
// Swifter than the moon's sphere;
99+
// And I serve the Fairy Queen,
100+
// To dew her orbs upon the green;
101+
// The cowslips tall her pensioners be;
102+
// In their gold coats spots you see;
103+
// Those be rubies, fairy favours;
104+
// In those freckles live their savours;
105+
// I must go seek some dewdrops here,
106+
// And hang a pearl in every cowslip's ear.
107+
// - \{author}
108+
// """;
109+
// }
110110

111111
}

java-improvements/src/test/java/com/bomfim/improvements/PokemonRecordPatternTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ void printPokeball2OldNPE() {
7979
var pokeballEmpty = new Pokeball("Ultra Ball", null);
8080
assertThatThrownBy(() -> pokemonRecordPattern.printPokeball2OldNPE(pokeballEmpty))
8181
.isInstanceOf(NullPointerException.class)
82-
.hasMessage("Cannot invoke \"com.bomfim.strings.RecordPokemon$PokemonRecord.name()\" because \"pokeball.pokemon\" is null");
82+
.hasMessage("Cannot invoke \"com.bomfim.improvements.RecordPokemon$PokemonRecord.name()\" because \"pokeball.pokemon\" is null");
8383
}
8484

8585
@Test

java-improvements/src/test/java/com/bomfim/improvements/StringJsonTest.java

+14-14
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,18 @@ void writeJsonTextBlock() {
6363
""");
6464
}
6565

66-
@Test
67-
void writePokemonStringTemplate() {
68-
Assertions.assertThat(stringJson.writePokemonStringTemplate(pokemon))
69-
.isEqualTo("""
70-
{
71-
"id": 1,
72-
"name": "Bulbasaur",
73-
"type": [
74-
"Grass","Poison"
75-
],
76-
"species": "Seed Pokémon",
77-
}
78-
""");
79-
}
66+
// @Test
67+
// void writePokemonStringTemplate() {
68+
// Assertions.assertThat(stringJson.writePokemonStringTemplate(pokemon))
69+
// .isEqualTo("""
70+
// {
71+
// "id": 1,
72+
// "name": "Bulbasaur",
73+
// "type": [
74+
// "Grass","Poison"
75+
// ],
76+
// "species": "Seed Pokémon",
77+
// }
78+
// """);
79+
// }
8080
}

java-improvements/src/test/java/com/bomfim/improvements/StringPoemTest.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ void writePoemTextBlock() {
3030
.contains("Shakespeare");
3131
}
3232

33-
@org.junit.jupiter.api.Test
34-
void writePoemStringTemplate() {
35-
Assertions.assertThat(stringPoem.writePoemStringTemplate("Shakespeare"))
36-
.contains("Shakespeare");
37-
}
33+
// @org.junit.jupiter.api.Test
34+
// void writePoemStringTemplate() {
35+
// Assertions.assertThat(stringPoem.writePoemStringTemplate("Shakespeare"))
36+
// .contains("Shakespeare");
37+
// }
3838
}

java-news-module/src/main/java/com/bomfim/java21/JsonStringTemplate.java

+24-19
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,32 @@
22

33
import java.util.List;
44

5+
/***
6+
* Remove the String Templates preview feature: https://bugs.openjdk.org/browse/JDK-8329949
7+
*/
8+
9+
510
public class JsonStringTemplate {
611

7-
/**
8-
* Using String template: JEP 430: String Templates (Preview)
9-
* From Java 21
10-
*
11-
* @param pokemon pokemon to display in Json
12-
* @return json.
13-
*/
14-
public String writePokemonStringTemplate(PokemonRecord pokemon) {
15-
return STR. """
16-
{
17-
"id": \{ pokemon.id() },
18-
"name": "\{ pokemon.name() }",
19-
"type": [
20-
"\{ String.join("\",\"", pokemon.type()) }"
21-
],
22-
"species": "\{ pokemon.species() }",
23-
}
24-
""" ;
25-
}
12+
// /**
13+
// * Using String template: JEP 430: String Templates (Preview)
14+
// * From Java 21
15+
// *
16+
// * @param pokemon pokemon to display in Json
17+
// * @return json.
18+
// */
19+
// public String writePokemonStringTemplate(PokemonRecord pokemon) {
20+
// return STR. """
21+
// {
22+
// "id": \{ pokemon.id() },
23+
// "name": "\{ pokemon.name() }",
24+
// "type": [
25+
// "\{ String.join("\",\"", pokemon.type()) }"
26+
// ],
27+
// "species": "\{ pokemon.species() }",
28+
// }
29+
// """ ;
30+
// }
2631

2732
public record PokemonRecord(Integer id, String name, List<String> type, String species) {
2833
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.bomfim.java23;
2+
3+
/**
4+
* JEP 455: Primitive Types in Patterns, instanceof, and switch (Preview)
5+
*
6+
* https://openjdk.org/jeps/455
7+
*/
8+
public class PrimitiveInstanceOf {
9+
10+
public String getAgeRange(int age) {
11+
return switch (age) {
12+
case 0 -> throw new IllegalArgumentException("Wrong Age");
13+
case int i when i <= 20 -> "KID";
14+
case int i when i >= 21 && i<=50 -> "ADULT";
15+
case int i when i >= 50 -> "OLDER";
16+
default -> "N/A";
17+
};
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.bomfim.java24;
2+
3+
import java.util.List;
4+
import java.util.stream.Gatherer;
5+
6+
/**
7+
* JEP 485: Stream Gatherers
8+
*/
9+
public class StreamGatherFinal {
10+
11+
public List<Integer> getValuesUntilOddNumber(List<Integer> values) {
12+
return values
13+
.stream()
14+
.gather(Gatherer.of((Gatherer.Integrator<Void, Integer, Integer>) (_, value, downstream) -> {
15+
if (value % 2 == 0) {
16+
downstream.push(value);
17+
return true;
18+
}
19+
return false;
20+
}))
21+
.toList();
22+
}
23+
24+
}

java-news-module/src/test/java/com/bomfim/java18/FinalizeDeprecatedTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ void shouldExecuteMethodWithFinalize() {
3434

3535
assertThat(outContent.toString()).contains("Finally",
3636
"error",
37-
"Thread:Thread[#1,main,5,main]")
37+
"Thread:Thread[#3,main,5,main]")
3838
.doesNotContain("The end");
3939

4040
//Executed by another thread.
4141
//The use of finalization can lead to problems with security, performance, and reliability. See JEP 421 for discussion and alternatives.
4242
assertThat(outContent.toString()).contains("Finalize Deprecated",
43-
"Thread:Thread[#5,Finalizer,8,system]");
43+
"Thread:Thread[#7,Finalizer,8,system]");
4444

4545
}
4646
}
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
11
package com.bomfim.java21;
22

3-
import org.assertj.core.api.Assertions;
4-
import org.junit.jupiter.api.Test;
5-
6-
import java.util.List;
7-
83
class JsonStringTemplateTest {
94

10-
@Test
11-
void writePokemonStringTemplate() {
12-
var pokemon = new JsonStringTemplate.PokemonRecord(1, "Bulbasaur", List.of("Grass", "Poison"), "Seed Pokémon");
13-
Assertions.assertThat(new JsonStringTemplate().writePokemonStringTemplate(pokemon))
14-
.isEqualTo("""
15-
{
16-
"id": 1,
17-
"name": "Bulbasaur",
18-
"type": [
19-
"Grass","Poison"
20-
],
21-
"species": "Seed Pokémon",
22-
}
23-
""");
24-
}
5+
// @Test
6+
// void writePokemonStringTemplate() {
7+
// var pokemon = new JsonStringTemplate.PokemonRecord(1, "Bulbasaur", List.of("Grass", "Poison"), "Seed Pokémon");
8+
// Assertions.assertThat(new JsonStringTemplate().writePokemonStringTemplate(pokemon))
9+
// .isEqualTo("""
10+
// {
11+
// "id": 1,
12+
// "name": "Bulbasaur",
13+
// "type": [
14+
// "Grass","Poison"
15+
// ],
16+
// "species": "Seed Pokémon",
17+
// }
18+
// """);
19+
// }
2520
}

java-news-module/src/test/java/com/bomfim/java22/StreamGatherersTest.java java-news-module/src/test/java/com/bomfim/java22/StreamGatherersTestFinal.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import java.util.List;
77

8-
class StreamGatherersTest {
8+
class StreamGatherersTestFinal {
99

1010
@Test
1111
void getMaxSumOfConsecutiveElements() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.bomfim.java23;
2+
3+
import org.assertj.core.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
class PrimitiveInstanceOfTest {
7+
8+
@Test
9+
void testAgeRange() {
10+
var primitiveInstanceOf = new PrimitiveInstanceOf();
11+
Assertions.assertThat(primitiveInstanceOf.getAgeRange(13)).isEqualTo("KID");
12+
Assertions.assertThat(primitiveInstanceOf.getAgeRange(21)).isEqualTo("ADULT");
13+
Assertions.assertThat(primitiveInstanceOf.getAgeRange(51)).isEqualTo("OLDER");
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.bomfim.java24;
2+
3+
import org.assertj.core.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.util.List;
7+
8+
class StreamGatherTest {
9+
10+
@Test
11+
void testGetValuesUntilOddNumber() {
12+
StreamGatherFinal streamGatherFinal = new StreamGatherFinal();
13+
Assertions.assertThat(streamGatherFinal.getValuesUntilOddNumber(List.of(1, 2, 3, 4, 5))).isEmpty();
14+
Assertions.assertThat(streamGatherFinal.getValuesUntilOddNumber(List.of(2, 4, 5, 6))).containsExactly(2, 4);
15+
}
16+
}

0 commit comments

Comments
 (0)