Merge pull request #11363 from thiagohora/BAEL-5197/new_features_in_javav17
[BAEL-5197] New Features in Java 17
This commit is contained in:
commit
015cdea7c8
|
@ -23,6 +23,18 @@
|
|||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</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>
|
||||
|
@ -38,32 +50,35 @@
|
|||
<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>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${surefire.plugin.version}</version>
|
||||
|
||||
<configuration>
|
||||
<argLine>--enable-preview</argLine>
|
||||
<argLine>--enable-native-access=core.java</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>17</maven.compiler.source.version>
|
||||
<maven.compiler.target.version>17</maven.compiler.target.version>
|
||||
<maven.compiler.release>17</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>
|
||||
<maven.compiler.release>17</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,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<String> 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);
|
||||
}
|
||||
}
|
|
@ -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";
|
||||
};
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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.");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -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<Float> 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];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
module core.java {
|
||||
requires jdk.incubator.vector;
|
||||
requires jdk.incubator.foreign;
|
||||
}
|
|
@ -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/
|
|
@ -0,0 +1,8 @@
|
|||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
|
||||
char* printName(char *name) {
|
||||
char* newString = (char*)malloc((15 + sizeof(name))*sizeof(char));
|
||||
sprintf(newString, "Your name is %s", name);
|
||||
return newString;
|
||||
}
|
|
@ -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));
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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();
|
||||
};
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue