From 861a8e19a44b491c1623fa93872766a14d20b3a2 Mon Sep 17 00:00:00 2001 From: LiamGve Date: Wed, 22 Sep 2021 08:33:17 +0100 Subject: [PATCH] =?UTF-8?q?BAEL-5069=20code=20for=20article=20on=20new=20f?= =?UTF-8?q?eatures=20in=20the=20Java=2016=20incremental=E2=80=A6=20(#11176?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- core-java-modules/core-java-16/pom.xml | 35 ++++++++++++++- .../com/baeldung/features/HelloWorld.java | 7 +++ .../com/baeldung/features/OuterClass.java | 9 ++++ .../com/baeldung/features/VectorExample.java | 23 ++++++++++ .../com/baeldung/features/model/Book.java | 44 +++++++++++++++++++ .../com/baeldung/features/record/Book.java | 4 ++ .../features/sealed/JungleAnimal.java | 4 ++ .../com/baeldung/features/sealed/Monkey.java | 4 ++ .../com/baeldung/features/sealed/Sealed.java | 13 ++++++ .../com/baeldung/features/sealed/Snake.java | 4 ++ .../src/main/java/module-info.java | 4 ++ .../features/DayPeriodSupportUnitTest.java | 24 ++++++++++ .../baeldung/features/HelloWorldUnitTest.java | 28 ++++++++++++ .../InstanceOfPatternMatchingUnitTest.java | 18 ++++++++ .../features/StreamToListUnitTest.java | 20 +++++++++ .../features/VectorExampleUnitTest.java | 21 +++++++++ 16 files changed, 261 insertions(+), 1 deletion(-) create mode 100644 core-java-modules/core-java-16/src/main/java/com/baeldung/features/HelloWorld.java create mode 100644 core-java-modules/core-java-16/src/main/java/com/baeldung/features/OuterClass.java create mode 100644 core-java-modules/core-java-16/src/main/java/com/baeldung/features/VectorExample.java create mode 100644 core-java-modules/core-java-16/src/main/java/com/baeldung/features/model/Book.java create mode 100644 core-java-modules/core-java-16/src/main/java/com/baeldung/features/record/Book.java create mode 100644 core-java-modules/core-java-16/src/main/java/com/baeldung/features/sealed/JungleAnimal.java create mode 100644 core-java-modules/core-java-16/src/main/java/com/baeldung/features/sealed/Monkey.java create mode 100644 core-java-modules/core-java-16/src/main/java/com/baeldung/features/sealed/Sealed.java create mode 100644 core-java-modules/core-java-16/src/main/java/com/baeldung/features/sealed/Snake.java create mode 100644 core-java-modules/core-java-16/src/main/java/module-info.java create mode 100644 core-java-modules/core-java-16/src/test/java/com/baeldung/features/DayPeriodSupportUnitTest.java create mode 100644 core-java-modules/core-java-16/src/test/java/com/baeldung/features/HelloWorldUnitTest.java create mode 100644 core-java-modules/core-java-16/src/test/java/com/baeldung/features/InstanceOfPatternMatchingUnitTest.java create mode 100644 core-java-modules/core-java-16/src/test/java/com/baeldung/features/StreamToListUnitTest.java create mode 100644 core-java-modules/core-java-16/src/test/java/com/baeldung/features/VectorExampleUnitTest.java diff --git a/core-java-modules/core-java-16/pom.xml b/core-java-modules/core-java-16/pom.xml index 5a78d9a46f..5d10325f03 100644 --- a/core-java-modules/core-java-16/pom.xml +++ b/core-java-modules/core-java-16/pom.xml @@ -28,6 +28,18 @@ commons-lang3 3.12.0 + + org.junit.jupiter + junit-jupiter-engine + ${junit-jupiter.version} + test + + + org.junit.jupiter + junit-jupiter-api + ${junit-jupiter.version} + test + @@ -37,17 +49,38 @@ maven-compiler-plugin ${maven-compiler-plugin.version} + ${maven.compiler.release} + --enable-preview ${maven.compiler.source.version} ${maven.compiler.target.version} + + org.apache.maven.plugins + maven-surefire-plugin + ${surefire.plugin.version} + + --enable-preview + 1 + + + + org.apache.maven.surefire + surefire-api + ${surefire.plugin.version} + + + 16 16 - 3.6.1 + 16 + 3.8.1 + 3.0.0-M5 + 3.17.2 \ No newline at end of file diff --git a/core-java-modules/core-java-16/src/main/java/com/baeldung/features/HelloWorld.java b/core-java-modules/core-java-16/src/main/java/com/baeldung/features/HelloWorld.java new file mode 100644 index 0000000000..7d72c4b71c --- /dev/null +++ b/core-java-modules/core-java-16/src/main/java/com/baeldung/features/HelloWorld.java @@ -0,0 +1,7 @@ +package com.baeldung.features; + +interface HelloWorld { + default String hello() { + return "world"; + } +} diff --git a/core-java-modules/core-java-16/src/main/java/com/baeldung/features/OuterClass.java b/core-java-modules/core-java-16/src/main/java/com/baeldung/features/OuterClass.java new file mode 100644 index 0000000000..6e08674bff --- /dev/null +++ b/core-java-modules/core-java-16/src/main/java/com/baeldung/features/OuterClass.java @@ -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"); + } +} diff --git a/core-java-modules/core-java-16/src/main/java/com/baeldung/features/VectorExample.java b/core-java-modules/core-java-16/src/main/java/com/baeldung/features/VectorExample.java new file mode 100644 index 0000000000..1f25290270 --- /dev/null +++ b/core-java-modules/core-java-16/src/main/java/com/baeldung/features/VectorExample.java @@ -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; + } +} diff --git a/core-java-modules/core-java-16/src/main/java/com/baeldung/features/model/Book.java b/core-java-modules/core-java-16/src/main/java/com/baeldung/features/model/Book.java new file mode 100644 index 0000000000..c532e41649 --- /dev/null +++ b/core-java-modules/core-java-16/src/main/java/com/baeldung/features/model/Book.java @@ -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); + } +} diff --git a/core-java-modules/core-java-16/src/main/java/com/baeldung/features/record/Book.java b/core-java-modules/core-java-16/src/main/java/com/baeldung/features/record/Book.java new file mode 100644 index 0000000000..3bddf143be --- /dev/null +++ b/core-java-modules/core-java-16/src/main/java/com/baeldung/features/record/Book.java @@ -0,0 +1,4 @@ +package com.baeldung.features.record; + +public record Book(String title, String author, String isbn) { +} diff --git a/core-java-modules/core-java-16/src/main/java/com/baeldung/features/sealed/JungleAnimal.java b/core-java-modules/core-java-16/src/main/java/com/baeldung/features/sealed/JungleAnimal.java new file mode 100644 index 0000000000..1853f8449c --- /dev/null +++ b/core-java-modules/core-java-16/src/main/java/com/baeldung/features/sealed/JungleAnimal.java @@ -0,0 +1,4 @@ +package com.baeldung.features.sealed; + +public sealed interface JungleAnimal permits Monkey, Snake { +} diff --git a/core-java-modules/core-java-16/src/main/java/com/baeldung/features/sealed/Monkey.java b/core-java-modules/core-java-16/src/main/java/com/baeldung/features/sealed/Monkey.java new file mode 100644 index 0000000000..aeddfdddda --- /dev/null +++ b/core-java-modules/core-java-16/src/main/java/com/baeldung/features/sealed/Monkey.java @@ -0,0 +1,4 @@ +package com.baeldung.features.sealed; + +public final class Monkey implements JungleAnimal { +} diff --git a/core-java-modules/core-java-16/src/main/java/com/baeldung/features/sealed/Sealed.java b/core-java-modules/core-java-16/src/main/java/com/baeldung/features/sealed/Sealed.java new file mode 100644 index 0000000000..1ee4ff6e57 --- /dev/null +++ b/core-java-modules/core-java-16/src/main/java/com/baeldung/features/sealed/Sealed.java @@ -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 + } + } +} diff --git a/core-java-modules/core-java-16/src/main/java/com/baeldung/features/sealed/Snake.java b/core-java-modules/core-java-16/src/main/java/com/baeldung/features/sealed/Snake.java new file mode 100644 index 0000000000..c35575f5e0 --- /dev/null +++ b/core-java-modules/core-java-16/src/main/java/com/baeldung/features/sealed/Snake.java @@ -0,0 +1,4 @@ +package com.baeldung.features.sealed; + +public non-sealed class Snake implements JungleAnimal { +} diff --git a/core-java-modules/core-java-16/src/main/java/module-info.java b/core-java-modules/core-java-16/src/main/java/module-info.java new file mode 100644 index 0000000000..ee983a6832 --- /dev/null +++ b/core-java-modules/core-java-16/src/main/java/module-info.java @@ -0,0 +1,4 @@ +module core.java { + requires jdk.incubator.vector; + requires org.apache.commons.lang3; +} \ No newline at end of file diff --git a/core-java-modules/core-java-16/src/test/java/com/baeldung/features/DayPeriodSupportUnitTest.java b/core-java-modules/core-java-16/src/test/java/com/baeldung/features/DayPeriodSupportUnitTest.java new file mode 100644 index 0000000000..6ae6232f00 --- /dev/null +++ b/core-java-modules/core-java-16/src/test/java/com/baeldung/features/DayPeriodSupportUnitTest.java @@ -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"); + } +} diff --git a/core-java-modules/core-java-16/src/test/java/com/baeldung/features/HelloWorldUnitTest.java b/core-java-modules/core-java-16/src/test/java/com/baeldung/features/HelloWorldUnitTest.java new file mode 100644 index 0000000000..d421f13dd1 --- /dev/null +++ b/core-java-modules/core-java-16/src/test/java/com/baeldung/features/HelloWorldUnitTest.java @@ -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"); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-16/src/test/java/com/baeldung/features/InstanceOfPatternMatchingUnitTest.java b/core-java-modules/core-java-16/src/test/java/com/baeldung/features/InstanceOfPatternMatchingUnitTest.java new file mode 100644 index 0000000000..7243957332 --- /dev/null +++ b/core-java-modules/core-java-16/src/test/java/com/baeldung/features/InstanceOfPatternMatchingUnitTest.java @@ -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); + } + } +} diff --git a/core-java-modules/core-java-16/src/test/java/com/baeldung/features/StreamToListUnitTest.java b/core-java-modules/core-java-16/src/test/java/com/baeldung/features/StreamToListUnitTest.java new file mode 100644 index 0000000000..00d090a240 --- /dev/null +++ b/core-java-modules/core-java-16/src/test/java/com/baeldung/features/StreamToListUnitTest.java @@ -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 integersAsString = Arrays.asList("1", "2", "3"); + List ints = integersAsString.stream().map(Integer::parseInt).collect(Collectors.toList()); + List intsEquivalent = integersAsString.stream().map(Integer::parseInt).toList(); + assertThat(ints).isEqualTo(intsEquivalent); + } +} diff --git a/core-java-modules/core-java-16/src/test/java/com/baeldung/features/VectorExampleUnitTest.java b/core-java-modules/core-java-16/src/test/java/com/baeldung/features/VectorExampleUnitTest.java new file mode 100644 index 0000000000..981f106ec7 --- /dev/null +++ b/core-java-modules/core-java-16/src/test/java/com/baeldung/features/VectorExampleUnitTest.java @@ -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); + } +} \ No newline at end of file