Skip to content

Commit 3860b75

Browse files
LiamGveLiam Garvie
andauthored
BAEL-5069 code for article on new features in the Java 16 incremental… (eugenp#11176)
* BAEL-5069 code for article on new features in the Java 16 incremental release * BAEL-5069 added some details around Vector API example and enable-preview in maven to allow package to build Co-authored-by: Liam Garvie <[email protected]>
1 parent 06f357f commit 3860b75

16 files changed

Lines changed: 261 additions & 1 deletion

File tree

core-java-modules/core-java-16/pom.xml

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@
2828
<artifactId>commons-lang3</artifactId>
2929
<version>3.12.0</version>
3030
</dependency>
31+
<dependency>
32+
<groupId>org.junit.jupiter</groupId>
33+
<artifactId>junit-jupiter-engine</artifactId>
34+
<version>${junit-jupiter.version}</version>
35+
<scope>test</scope>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.junit.jupiter</groupId>
39+
<artifactId>junit-jupiter-api</artifactId>
40+
<version>${junit-jupiter.version}</version>
41+
<scope>test</scope>
42+
</dependency>
3143
</dependencies>
3244

3345
<build>
@@ -37,17 +49,38 @@
3749
<artifactId>maven-compiler-plugin</artifactId>
3850
<version>${maven-compiler-plugin.version}</version>
3951
<configuration>
52+
<release>${maven.compiler.release}</release>
53+
<compilerArgs>--enable-preview</compilerArgs>
4054
<source>${maven.compiler.source.version}</source>
4155
<target>${maven.compiler.target.version}</target>
4256
</configuration>
4357
</plugin>
58+
<plugin>
59+
<groupId>org.apache.maven.plugins</groupId>
60+
<artifactId>maven-surefire-plugin</artifactId>
61+
<version>${surefire.plugin.version}</version>
62+
<configuration>
63+
<argLine>--enable-preview</argLine>
64+
<forkCount>1</forkCount>
65+
</configuration>
66+
<dependencies>
67+
<dependency>
68+
<groupId>org.apache.maven.surefire</groupId>
69+
<artifactId>surefire-api</artifactId>
70+
<version>${surefire.plugin.version}</version>
71+
</dependency>
72+
</dependencies>
73+
</plugin>
4474
</plugins>
4575
</build>
4676

4777
<properties>
4878
<maven.compiler.source.version>16</maven.compiler.source.version>
4979
<maven.compiler.target.version>16</maven.compiler.target.version>
50-
<assertj.version>3.6.1</assertj.version>
80+
<maven.compiler.release>16</maven.compiler.release>
81+
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
82+
<surefire.plugin.version>3.0.0-M5</surefire.plugin.version>
83+
<assertj.version>3.17.2</assertj.version>
5184
</properties>
5285

5386
</project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung.features;
2+
3+
interface HelloWorld {
4+
default String hello() {
5+
return "world";
6+
}
7+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.features;
2+
3+
import com.baeldung.features.record.Book;
4+
5+
public class OuterClass {
6+
class InnerClass {
7+
Book book = new Book("Title", "author", "isbn");
8+
}
9+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.features;
2+
3+
import jdk.incubator.vector.IntVector;
4+
5+
public class VectorExample {
6+
public int[] scalarComputation(int[] a, int[] b) {
7+
var c = new int[a.length];
8+
for (int i = 0; i < a.length; i++) {
9+
c[i] = a[i] * b[i];
10+
}
11+
return c;
12+
}
13+
14+
public int[] vectorComputation(int[] a, int[] b) {
15+
var c = new int[a.length];
16+
17+
var vectorA = IntVector.fromArray(IntVector.SPECIES_128, a, 0);
18+
var vectorB = IntVector.fromArray(IntVector.SPECIES_128, b, 0);
19+
var vectorC = vectorA.mul(vectorB);
20+
vectorC.intoArray(c, 0);
21+
return c;
22+
}
23+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.features.model;
2+
3+
import java.util.Objects;
4+
5+
public final class Book {
6+
private final String title;
7+
private final String author;
8+
private final String isbn;
9+
10+
public Book(String title, String author, String isbn) {
11+
this.title = title;
12+
this.author = author;
13+
this.isbn = isbn;
14+
}
15+
16+
public String getTitle() {
17+
return title;
18+
}
19+
20+
public String getAuthor() {
21+
return author;
22+
}
23+
24+
public String getIsbn() {
25+
return isbn;
26+
}
27+
28+
@Override
29+
public boolean equals(Object o) {
30+
if (this == o) {
31+
return true;
32+
}
33+
if (o == null || getClass() != o.getClass()) {
34+
return false;
35+
}
36+
Book book = (Book) o;
37+
return Objects.equals(title, book.title) && Objects.equals(author, book.author) && Objects.equals(isbn, book.isbn);
38+
}
39+
40+
@Override
41+
public int hashCode() {
42+
return Objects.hash(title, author, isbn);
43+
}
44+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.baeldung.features.record;
2+
3+
public record Book(String title, String author, String isbn) {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.baeldung.features.sealed;
2+
3+
public sealed interface JungleAnimal permits Monkey, Snake {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.baeldung.features.sealed;
2+
3+
public final class Monkey implements JungleAnimal {
4+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.features.sealed;
2+
3+
public class Sealed {
4+
public static void main(String... args) {
5+
JungleAnimal j = new Monkey();
6+
7+
if (j instanceof Monkey m) {
8+
// do logic
9+
} else if (j instanceof Snake s) {
10+
// do logic
11+
}
12+
}
13+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.baeldung.features.sealed;
2+
3+
public non-sealed class Snake implements JungleAnimal {
4+
}

0 commit comments

Comments
 (0)