BAEL-5069 code for article on new features in the Java 16 incremental… (#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 <liamgarvie@Liams-MacBook-Pro.local>
This commit is contained in:
parent
2086ac5fc3
commit
861a8e19a4
|
@ -28,6 +28,18 @@
|
|||
<artifactId>commons-lang3</artifactId>
|
||||
<version>3.12.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -37,17 +49,38 @@
|
|||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<release>${maven.compiler.release}</release>
|
||||
<compilerArgs>--enable-preview</compilerArgs>
|
||||
<source>${maven.compiler.source.version}</source>
|
||||
<target>${maven.compiler.target.version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${surefire.plugin.version}</version>
|
||||
<configuration>
|
||||
<argLine>--enable-preview</argLine>
|
||||
<forkCount>1</forkCount>
|
||||
</configuration>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.maven.surefire</groupId>
|
||||
<artifactId>surefire-api</artifactId>
|
||||
<version>${surefire.plugin.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source.version>16</maven.compiler.source.version>
|
||||
<maven.compiler.target.version>16</maven.compiler.target.version>
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
<maven.compiler.release>16</maven.compiler.release>
|
||||
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
|
||||
<surefire.plugin.version>3.0.0-M5</surefire.plugin.version>
|
||||
<assertj.version>3.17.2</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,7 @@
|
|||
package com.baeldung.features;
|
||||
|
||||
interface HelloWorld {
|
||||
default String hello() {
|
||||
return "world";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package com.baeldung.features;
|
||||
|
||||
import com.baeldung.features.record.Book;
|
||||
|
||||
public class OuterClass {
|
||||
class InnerClass {
|
||||
Book book = new Book("Title", "author", "isbn");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.features;
|
||||
|
||||
import jdk.incubator.vector.IntVector;
|
||||
|
||||
public class VectorExample {
|
||||
public int[] scalarComputation(int[] a, int[] b) {
|
||||
var c = new int[a.length];
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
c[i] = a[i] * b[i];
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
public int[] vectorComputation(int[] a, int[] b) {
|
||||
var c = new int[a.length];
|
||||
|
||||
var vectorA = IntVector.fromArray(IntVector.SPECIES_128, a, 0);
|
||||
var vectorB = IntVector.fromArray(IntVector.SPECIES_128, b, 0);
|
||||
var vectorC = vectorA.mul(vectorB);
|
||||
vectorC.intoArray(c, 0);
|
||||
return c;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package com.baeldung.features.model;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public final class Book {
|
||||
private final String title;
|
||||
private final String author;
|
||||
private final String isbn;
|
||||
|
||||
public Book(String title, String author, String isbn) {
|
||||
this.title = title;
|
||||
this.author = author;
|
||||
this.isbn = isbn;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public String getIsbn() {
|
||||
return isbn;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
Book book = (Book) o;
|
||||
return Objects.equals(title, book.title) && Objects.equals(author, book.author) && Objects.equals(isbn, book.isbn);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(title, author, isbn);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
package com.baeldung.features.record;
|
||||
|
||||
public record Book(String title, String author, String isbn) {
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
package com.baeldung.features.sealed;
|
||||
|
||||
public sealed interface JungleAnimal permits Monkey, Snake {
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
package com.baeldung.features.sealed;
|
||||
|
||||
public final class Monkey implements JungleAnimal {
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.features.sealed;
|
||||
|
||||
public class Sealed {
|
||||
public static void main(String... args) {
|
||||
JungleAnimal j = new Monkey();
|
||||
|
||||
if (j instanceof Monkey m) {
|
||||
// do logic
|
||||
} else if (j instanceof Snake s) {
|
||||
// do logic
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
package com.baeldung.features.sealed;
|
||||
|
||||
public non-sealed class Snake implements JungleAnimal {
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
module core.java {
|
||||
requires jdk.incubator.vector;
|
||||
requires org.apache.commons.lang3;
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.baeldung.features;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.LocalTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class DayPeriodSupportUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenASpecificTime_whenFormattingUsingTheBSymbol_thenExpectVerbosePeriodOfDay() {
|
||||
LocalTime date = LocalTime.parse("15:25:08.690791");
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("h B");
|
||||
assertThat(date.format(formatter)).isEqualTo("3 in the afternoon");
|
||||
|
||||
formatter = DateTimeFormatter.ofPattern("h BBBB");
|
||||
assertThat(date.format(formatter)).isEqualTo("3 in the afternoon");
|
||||
|
||||
formatter = DateTimeFormatter.ofPattern("h BBBBB");
|
||||
assertThat(date.format(formatter)).isEqualTo("3 in the afternoon");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package com.baeldung.features;
|
||||
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Proxy;
|
||||
|
||||
import static java.lang.ClassLoader.getSystemClassLoader;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class HelloWorldUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenAnInterfaceWithDefaulMethod_whenCreatingProxyInstance_thenCanInvokeDefaultMethod() throws Exception {
|
||||
Object proxy = Proxy.newProxyInstance(getSystemClassLoader(), new Class<?>[] { HelloWorld.class },
|
||||
(prox, method, args) -> {
|
||||
if (method.isDefault()) {
|
||||
return InvocationHandler.invokeDefault(prox, method, args);
|
||||
}
|
||||
return method.invoke(prox, args);
|
||||
}
|
||||
);
|
||||
Method method = proxy.getClass().getMethod("hello");
|
||||
assertThat(method.invoke(proxy)).isEqualTo("world");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.features;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class InstanceOfPatternMatchingUnitTest {
|
||||
|
||||
@Test
|
||||
void givenTheNewPatternMatchingAbility_whenComparingAgainstTheTradiationalApproach_thenBothVariablesAreEqual() {
|
||||
Object obj = "TEST";
|
||||
|
||||
if (obj instanceof String a) {
|
||||
String b = (String) obj;
|
||||
assertThat(a).isEqualTo(b);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.baeldung.features;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class StreamToListUnitTest {
|
||||
|
||||
@Test
|
||||
void givenAStream_whenCreatingANewListFromStream_thenCollectorsOrInbuiltFunctionAreEquivalent() {
|
||||
List<String> integersAsString = Arrays.asList("1", "2", "3");
|
||||
List<Integer> ints = integersAsString.stream().map(Integer::parseInt).collect(Collectors.toList());
|
||||
List<Integer> intsEquivalent = integersAsString.stream().map(Integer::parseInt).toList();
|
||||
assertThat(ints).isEqualTo(intsEquivalent);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.features;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
|
||||
class VectorExampleUnitTest {
|
||||
|
||||
@Test
|
||||
void givenAScalarComputation_whenCalculatingUsingVectorAPI_thenResultIsSameAsPreviousMethodOfComputation() {
|
||||
VectorExample objectUnderTest = new VectorExample();
|
||||
|
||||
int[] a = {1, 2, 3, 4};
|
||||
int[] b = {5, 6, 7, 8};
|
||||
|
||||
int[] result = objectUnderTest.scalarComputation(a, b);
|
||||
int[] result2 = objectUnderTest.vectorComputation(a, b);
|
||||
|
||||
assertArrayEquals(result, result2);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue