[BAEL-2506] Moved Java Void Type examples into a new core-java-reflection module

This commit is contained in:
dupirefr 2019-05-02 08:55:50 +02:00
parent 84a9cad62d
commit 48f4709d61
9 changed files with 224 additions and 0 deletions

View File

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-reflection</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-reflection</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-java</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj-core.version}</version>
</dependency>
</dependencies>
<properties>
<assertj-core.version>3.10.0</assertj-core.version>
</properties>
</project>

View File

@ -0,0 +1,5 @@
package com.baeldung.reflection.voidtype;
public interface Action {
void execute();
}

View File

@ -0,0 +1,21 @@
package com.baeldung.reflection.voidtype;
public class Calculator {
private int result = 0;
public int add(int number) {
return result += number;
}
public int sub(int number) {
return result -= number;
}
public void clear() {
result = 0;
}
public void print() {
System.out.println(result);
}
}

View File

@ -0,0 +1,27 @@
package com.baeldung.reflection.voidtype;
import java.util.concurrent.Callable;
import java.util.function.Consumer;
import java.util.function.Function;
public class Defer {
public static <V> V defer(Callable<V> callable) throws Exception {
return callable.call();
}
public static void defer(Runnable runnable) {
runnable.run();
}
public static <T, R> R defer(Function<T, R> function, T arg) {
return function.apply(arg);
}
public static <T> void defer(Consumer<T> consumer, T arg) {
consumer.accept(arg);
}
public static void defer(Action action) {
action.execute();
}
}

View File

@ -0,0 +1,15 @@
package com.baeldung.reflection.voidtype;
import java.util.concurrent.Callable;
public class MyOwnDefer {
public static void defer(Runnable runnable) throws Exception {
Defer.defer(new Callable<Void>() {
@Override
public Void call() {
runnable.run();
return null;
}
});
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.reflection.voidtype;
import org.junit.jupiter.api.Test;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
class CalculatorUnitTest {
@Test
void givenCalculator_whenGettingVoidMethodsByReflection_thenOnlyClearAndPrint() {
Method[] calculatorMethods = Calculator.class.getDeclaredMethods();
List<Method> calculatorVoidMethods = Arrays.stream(calculatorMethods)
.filter(method -> method.getReturnType().equals(Void.TYPE))
.collect(Collectors.toList());
assertThat(calculatorVoidMethods)
.allMatch(method -> Arrays.asList("clear", "print").contains(method.getName()));
}
}

View File

@ -0,0 +1,81 @@
package com.baeldung.reflection.voidtype;
import org.junit.jupiter.api.Test;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import java.util.function.Function;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
class DeferUnitTest {
@Test
void givenVoidCallable_whenDiffer_thenReturnNull() throws Exception {
Callable<Void> callable = new Callable<Void>() {
@Override
public Void call() {
System.out.println("Hello!");
return null;
}
};
assertThat(Defer.defer(callable)).isNull();
}
@Test
void givenVoidRunnable_whenDiffer_thenNoReturn() {
AtomicBoolean run = new AtomicBoolean(false);
Runnable runnable = new Runnable() {
@Override
public void run() {
System.out.println("Hello!");
run.set(true);
}
};
Defer.defer(runnable);
assertTrue(run.get());
}
@Test
void givenVoidFunction_whenDiffer_thenReturnNull() {
Function<String, Void> function = s -> {
System.out.println("Hello " + s + "!");
return null;
};
assertThat(Defer.defer(function, "World")).isNull();
}
@Test
void givenVoidConsumer_whenDiffer_thenReturnNull() {
AtomicBoolean run = new AtomicBoolean(false);
Consumer<String> function = s -> {
System.out.println("Hello " + s + "!");
run.set(true);
};
Defer.defer(function, "World");
assertTrue(run.get());
}
@Test
void givenAction_whenDiffer_thenNoReturn() {
AtomicBoolean run = new AtomicBoolean(false);
Action action = () -> {
System.out.println("Hello!");
run.set(true);
};
Defer.defer(action);
assertTrue(run.get());
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.reflection.voidtype;
import org.junit.jupiter.api.Test;
import java.util.concurrent.atomic.AtomicBoolean;
import static org.junit.jupiter.api.Assertions.*;
class MyOwnDeferUnitTest {
@Test
void defer() throws Exception {
AtomicBoolean run = new AtomicBoolean(false);
Runnable runnable = () -> {
System.out.println("Hello!");
run.set(true);
};
MyOwnDefer.defer(runnable);
assertTrue(run.get());
}
}

View File

@ -13,6 +13,7 @@
<modules>
<module>lombok-custom</module>
<module>picocli</module>
<module>core-java-reflection</module>
</modules>
<dependencies>