[BAEL-9555] - Created a core-java-modules folder
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.reflection.voidtype;
|
||||
|
||||
public interface Action {
|
||||
void execute();
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -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()));
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user