diff --git a/core-java-modules/core-java-17/pom.xml b/core-java-modules/core-java-17/pom.xml index f1f4edc484..e422bd62bb 100644 --- a/core-java-modules/core-java-17/pom.xml +++ b/core-java-modules/core-java-17/pom.xml @@ -23,6 +23,18 @@ ${assertj.version} test + + org.junit.jupiter + junit-jupiter-engine + ${junit-jupiter.version} + test + + + org.junit.jupiter + junit-jupiter-api + ${junit-jupiter.version} + test + @@ -38,32 +50,35 @@ ${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} - - - + + org.apache.maven.plugins + maven-surefire-plugin + ${surefire.plugin.version} + + + --enable-preview + --enable-native-access=core.java + 1 + + + + + org.apache.maven.surefire + surefire-api + ${surefire.plugin.version} + + + 17 17 - 17 - 3.8.1 - 3.0.0-M5 - 3.17.2 + 17 + 3.8.1 + 3.0.0-M5 + 3.17.2 \ No newline at end of file diff --git a/core-java-modules/core-java-17/src/main/java/com/baeldung/features/JEP356.java b/core-java-modules/core-java-17/src/main/java/com/baeldung/features/JEP356.java new file mode 100644 index 0000000000..0890b025be --- /dev/null +++ b/core-java-modules/core-java-17/src/main/java/com/baeldung/features/JEP356.java @@ -0,0 +1,20 @@ +package com.baeldung.features; + +import java.util.random.RandomGeneratorFactory; +import java.util.stream.IntStream; +import java.util.stream.Stream; + +public class JEP356 { + + public Stream getAllAlgorithms() { + return RandomGeneratorFactory.all().map(RandomGeneratorFactory::name); + } + + public IntStream getPseudoInts(String algorithm, int streamSize) { + // returns an IntStream with size @streamSize of random numbers generated using the @algorithm + // where the lower bound is 0 and the upper is 100 (exclusive) + return RandomGeneratorFactory.of(algorithm) + .create() + .ints(streamSize, 0,100); + } +} diff --git a/core-java-modules/core-java-17/src/main/java/com/baeldung/features/JEP406.java b/core-java-modules/core-java-17/src/main/java/com/baeldung/features/JEP406.java new file mode 100644 index 0000000000..2bc3a664d1 --- /dev/null +++ b/core-java-modules/core-java-17/src/main/java/com/baeldung/features/JEP406.java @@ -0,0 +1,28 @@ +package com.baeldung.features; + +import com.baeldung.features.JEP409.Circle; +import com.baeldung.features.JEP409.Shape; +import com.baeldung.features.JEP409.Triangle; + +public class JEP406 { + + static record Human (String name, int age, String profession) {} + + public String checkObject(Object obj) { + return switch (obj) { + case Human h -> "Name: %s, age: %s and profession: %s".formatted(h.name(), h.age(), h.profession()); + case Circle c -> "This is a circle"; + case Shape s -> "It is just a shape"; + case null -> "It is null"; + default -> "It is an object"; + }; + } + + public String checkShape(Shape shape) { + return switch (shape) { + case Triangle t && (t.getNumberOfSides() != 3) -> "This is a weird triangle"; + case Circle c && (c.getNumberOfSides() != 0) -> "This is a weird circle"; + default -> "Just a normal shape"; + }; + } +} diff --git a/core-java-modules/core-java-17/src/main/java/com/baeldung/features/JEP409.java b/core-java-modules/core-java-17/src/main/java/com/baeldung/features/JEP409.java new file mode 100644 index 0000000000..0fc3d6f467 --- /dev/null +++ b/core-java-modules/core-java-17/src/main/java/com/baeldung/features/JEP409.java @@ -0,0 +1,38 @@ +package com.baeldung.features; + +public class JEP409 { + + sealed interface Shape permits Rectangle, Circle, Square, Triangle { + int getNumberOfSides(); + } + + static final class Rectangle implements Shape { + @Override + public int getNumberOfSides() { + return 4; + } + } + + static final class Circle implements Shape { + @Override + public int getNumberOfSides() { + return 0; + } + } + + static final class Square implements Shape { + @Override + public int getNumberOfSides() { + return 4; + } + } + + static non-sealed class Triangle implements Shape { + + @Override + public int getNumberOfSides() { + return 3; + } + } + +} diff --git a/core-java-modules/core-java-17/src/main/java/com/baeldung/features/JEP412.java b/core-java-modules/core-java-17/src/main/java/com/baeldung/features/JEP412.java new file mode 100644 index 0000000000..8c998629c9 --- /dev/null +++ b/core-java-modules/core-java-17/src/main/java/com/baeldung/features/JEP412.java @@ -0,0 +1,56 @@ +package com.baeldung.features; + +import jdk.incubator.foreign.CLinker; +import jdk.incubator.foreign.FunctionDescriptor; +import jdk.incubator.foreign.MemoryAddress; +import jdk.incubator.foreign.SymbolLookup; + +import java.io.IOException; +import java.lang.invoke.MethodType; + +import static jdk.incubator.foreign.ResourceScope.newImplicitScope; + +public class JEP412 { + + private static final SymbolLookup libLookup; + + static { + var resource = JEP412.class.getResource("/compile_c.sh"); + try { + var process = new ProcessBuilder("sh", resource.getPath()).start(); + while (process.isAlive()) {} + } catch (IOException ex) { + throw new RuntimeException(ex); + } + + var path = JEP412.class.getResource("/print_name.so").getPath(); + System.load(path); + libLookup = SymbolLookup.loaderLookup(); + } + + public String getPrintNameFormat(String name){ + + var printMethod = libLookup.lookup("printName"); + + if (printMethod.isPresent()) { + var methodReference = CLinker.getInstance() + .downcallHandle( + printMethod.get(), + MethodType.methodType(MemoryAddress.class, MemoryAddress.class), + FunctionDescriptor.of(CLinker.C_POINTER, CLinker.C_POINTER) + ); + + try { + var nativeString = CLinker.toCString(name, newImplicitScope()); + var invokeReturn = methodReference.invoke(nativeString.address()); + var memoryAddress = (MemoryAddress) invokeReturn; + return CLinker.toJavaString(memoryAddress); + } catch (Throwable throwable) { + throw new RuntimeException(throwable); + } + } + throw new RuntimeException("printName function not found."); + } +} + + diff --git a/core-java-modules/core-java-17/src/main/java/com/baeldung/features/JEP414.java b/core-java-modules/core-java-17/src/main/java/com/baeldung/features/JEP414.java new file mode 100644 index 0000000000..aaccc4a665 --- /dev/null +++ b/core-java-modules/core-java-17/src/main/java/com/baeldung/features/JEP414.java @@ -0,0 +1,27 @@ +package com.baeldung.features; + +import jdk.incubator.vector.FloatVector; +import jdk.incubator.vector.VectorSpecies; + +public class JEP414 { + + private static final VectorSpecies SPECIES = FloatVector.SPECIES_PREFERRED; + + + public void newVectorComputation(float[] a, float[] b, float[] c) { + for (var i = 0; i < a.length; i += SPECIES.length()) { + var m = SPECIES.indexInRange(i, a.length); + var va = FloatVector.fromArray(SPECIES, a, i, m); + var vb = FloatVector.fromArray(SPECIES, b, i, m); + var vc = va.mul(vb); + vc.intoArray(c, i, m); + } + } + + public void commonVectorComputation(float[] a, float[] b, float[] c) { + for (var i = 0; i < a.length; i ++) { + c[i] = a[i] * b[i]; + } + } + +} diff --git a/core-java-modules/core-java-17/src/main/java/module-info.java b/core-java-modules/core-java-17/src/main/java/module-info.java new file mode 100644 index 0000000000..b1ce62aa68 --- /dev/null +++ b/core-java-modules/core-java-17/src/main/java/module-info.java @@ -0,0 +1,4 @@ +module core.java { + requires jdk.incubator.vector; + requires jdk.incubator.foreign; +} \ No newline at end of file diff --git a/core-java-modules/core-java-17/src/main/resources/compile_c.sh b/core-java-modules/core-java-17/src/main/resources/compile_c.sh new file mode 100755 index 0000000000..d2e0629cc5 --- /dev/null +++ b/core-java-modules/core-java-17/src/main/resources/compile_c.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )" + +gcc -c -fPIC $SCRIPTPATH/print_name.c +gcc -shared -rdynamic -o print_name.so print_name.o + +mv print_name.so $SCRIPTPATH/ +mv print_name.o $SCRIPTPATH/ \ No newline at end of file diff --git a/core-java-modules/core-java-17/src/main/resources/print_name.c b/core-java-modules/core-java-17/src/main/resources/print_name.c new file mode 100644 index 0000000000..7bda12e352 --- /dev/null +++ b/core-java-modules/core-java-17/src/main/resources/print_name.c @@ -0,0 +1,8 @@ +#include +#include + +char* printName(char *name) { + char* newString = (char*)malloc((15 + sizeof(name))*sizeof(char)); + sprintf(newString, "Your name is %s", name); + return newString; +} \ No newline at end of file diff --git a/core-java-modules/core-java-17/src/test/java/com/baeldung/features/JEP356UnitTest.java b/core-java-modules/core-java-17/src/test/java/com/baeldung/features/JEP356UnitTest.java new file mode 100644 index 0000000000..b4f61a6f24 --- /dev/null +++ b/core-java-modules/core-java-17/src/test/java/com/baeldung/features/JEP356UnitTest.java @@ -0,0 +1,19 @@ +package com.baeldung.features; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class JEP356UnitTest { + + @Test + void getPseudoInts_whenUsingAlgorithmXoroshiro128PlusPlus_shouldReturnStreamOfRandomInteger() { + var algorithm = "Xoshiro256PlusPlus"; + var streamSize = 100; + + JEP356 jep356 = new JEP356(); + + jep356.getPseudoInts(algorithm, streamSize) + .forEach(value -> assertThat(value).isLessThanOrEqualTo(99)); + } +} diff --git a/core-java-modules/core-java-17/src/test/java/com/baeldung/features/JEP406UnitTest.java b/core-java-modules/core-java-17/src/test/java/com/baeldung/features/JEP406UnitTest.java new file mode 100644 index 0000000000..87e6f3bec6 --- /dev/null +++ b/core-java-modules/core-java-17/src/test/java/com/baeldung/features/JEP406UnitTest.java @@ -0,0 +1,33 @@ +package com.baeldung.features; + +import com.baeldung.features.JEP406.Human; +import com.baeldung.features.JEP409.Square; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class JEP406UnitTest { + + @Test + void checkObject_shouldMatchWithHuman() { + var jep406 = new JEP406(); + + var human = new Human("John", 31, "Developer"); + + var checkResult = jep406.checkObject(human); + + assertEquals("Name: John, age: 31 and profession: Developer", checkResult); + } + + @Test + void checkShape_shouldMatchWithShape() { + var jep406 = new JEP406(); + + var square = new Square(); + + var checkResult = jep406.checkShape(square); + + assertEquals("Just a normal shape", checkResult); + } + +} \ No newline at end of file diff --git a/core-java-modules/core-java-17/src/test/java/com/baeldung/features/JEP409UnitTest.java b/core-java-modules/core-java-17/src/test/java/com/baeldung/features/JEP409UnitTest.java new file mode 100644 index 0000000000..41380ec2bb --- /dev/null +++ b/core-java-modules/core-java-17/src/test/java/com/baeldung/features/JEP409UnitTest.java @@ -0,0 +1,39 @@ +package com.baeldung.features; + +import com.baeldung.features.JEP409.*; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; + +class JEP409UnitTest { + + static class WeirdTriangle extends Triangle { + @Override public int getNumberOfSides() { + return 40; + } + } + + @Test + void testSealedClass_shouldInvokeRightClass() { + + Shape shape = spy(new WeirdTriangle()); + + int numberOfSides = getNumberOfSides(shape); + + assertEquals(40, numberOfSides); + verify(shape).getNumberOfSides(); + } + + int getNumberOfSides(Shape shape) { + return switch (shape) { + case WeirdTriangle t -> t.getNumberOfSides(); + case Circle c -> c.getNumberOfSides(); + case Triangle t -> t.getNumberOfSides(); + case Rectangle r -> r.getNumberOfSides(); + case Square s -> s.getNumberOfSides(); + }; + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-17/src/test/java/com/baeldung/features/JEP412UnitTest.java b/core-java-modules/core-java-17/src/test/java/com/baeldung/features/JEP412UnitTest.java new file mode 100644 index 0000000000..66e7d952c7 --- /dev/null +++ b/core-java-modules/core-java-17/src/test/java/com/baeldung/features/JEP412UnitTest.java @@ -0,0 +1,17 @@ +package com.baeldung.features; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class JEP412UnitTest { + + @Test + void getPrintNameFormat_whenPassingAName_shouldReceiveItFormatted() { + var jep412 = new JEP412(); + + var formattedName = jep412.getPrintNameFormat("John"); + + assertEquals("Your name is John", formattedName); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-17/src/test/java/com/baeldung/features/JEP414UnitTest.java b/core-java-modules/core-java-17/src/test/java/com/baeldung/features/JEP414UnitTest.java new file mode 100644 index 0000000000..459344d907 --- /dev/null +++ b/core-java-modules/core-java-17/src/test/java/com/baeldung/features/JEP414UnitTest.java @@ -0,0 +1,38 @@ +package com.baeldung.features; + +import org.junit.jupiter.api.Test; + +import static java.util.stream.Collectors.toList; +import static org.junit.jupiter.api.Assertions.assertEquals; + +class JEP414UnitTest { + + @Test + void vectorComputation_shouldMultiplyVectors() { + var jep414 = new JEP414(); + + float[] a = initArray(100); + float[] b = initArray(100); + float[] c = new float[100]; + float[] d = new float[100]; + + jep414.newVectorComputation(a, b, c); + jep414.commonVectorComputation(a, b, d); + + for (int i = 0; i < a.length; i++) { + assertEquals(c[i], d[i]); + } + } + + private float[] initArray(int size) { + final var jep356 = new JEP356(); + final var values = new float[size]; + final var pseudoInts = jep356.getPseudoInts("Xoshiro256PlusPlus", size).mapToObj(Float::valueOf).collect(toList()); + + for (int i = 0; i < pseudoInts.size(); i++) { + values[i] = pseudoInts.get(i); + } + + return values; + } +} \ No newline at end of file