[BAEL-5197] Add code samples and tests

This commit is contained in:
Thiago dos Santos Hora
2021-10-24 18:42:53 +02:00
parent 3320d8413b
commit a059f32171
15 changed files with 421 additions and 0 deletions

View File

@@ -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);
}
}

View File

@@ -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";
};
}
}

View File

@@ -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;
}
}
}

View File

@@ -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.");
}
}

View File

@@ -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];
}
}
}

View File

@@ -0,0 +1,4 @@
module core.java {
requires jdk.incubator.vector;
requires jdk.incubator.foreign;
}