Java 11 migration: patterns (remaining b-c) (#1081)

* Moves business-delegate pattern  to java 11

* Moves bytecode pattern  to java 11

* Moves caching pattern  to java 11

* Moves callback pattern  to java 11

* Moves chain pattern  to java 11

* Moves circuit-breaker pattern  to java 11

* Moves collection-pipeline pattern  to java 11

* Moves command pattern  to java 11

* Moves commander pattern  to java 11

* Moves composite pattern  to java 11

* Corrects test cases
This commit is contained in:
Anurag Agarwal
2019-11-13 01:26:46 +05:30
committed by Ilkka Seppälä
parent 6ef840f3cf
commit 33ea7335b1
63 changed files with 798 additions and 979 deletions

View File

@ -43,8 +43,7 @@ public final class App {
* Program entry point.
*/
public static void main(final String[] args) {
Task task = new SimpleTask();
Callback callback = () -> LOGGER.info("I'm done now.");
task.executeWith(callback);
var task = new SimpleTask();
task.executeWith(() -> LOGGER.info("I'm done now."));
}
}

View File

@ -42,8 +42,7 @@ public final class LambdasApp {
* Program entry point.
*/
public static void main(final String[] args) {
Task task = new SimpleTask();
Callback c = () -> LOGGER.info("I'm done now.");
task.executeWith(c);
var task = new SimpleTask();
task.executeWith(() -> LOGGER.info("I'm done now."));
}
}

View File

@ -23,6 +23,8 @@
package com.iluwatar.callback;
import java.util.Optional;
/**
* Template-method class for callback hook execution.
*/
@ -33,9 +35,7 @@ public abstract class Task {
*/
final void executeWith(final Callback callback) {
execute();
if (callback != null) {
callback.call();
}
Optional.ofNullable(callback).ifPresent(Callback::call);
}
public abstract void execute();

View File

@ -25,15 +25,12 @@ package com.iluwatar.callback;
import org.junit.jupiter.api.Test;
import java.io.IOException;
/**
* Tests that Callback example runs without errors.
*/
public class AppTest {
@Test
public void test() throws IOException {
String[] args = {};
App.main(args);
public void test() {
App.main(new String[]{});
}
}

View File

@ -23,14 +23,14 @@
package com.iluwatar.callback;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
/**
* Add a field as a counter. Every time the callback method is called increment this field. Unit
* test checks that the field is being incremented.
*
* <p>
* Could be done with mock objects as well where the call method call is verified.
*/
public class CallbackTest {
@ -41,17 +41,17 @@ public class CallbackTest {
public void test() {
Callback callback = () -> callingCount++;
Task task = new SimpleTask();
var task = new SimpleTask();
assertEquals(new Integer(0), callingCount, "Initial calling count of 0");
assertEquals(Integer.valueOf(0), callingCount, "Initial calling count of 0");
task.executeWith(callback);
assertEquals(new Integer(1), callingCount, "Callback called once");
assertEquals(Integer.valueOf(1), callingCount, "Callback called once");
task.executeWith(callback);
assertEquals(new Integer(2), callingCount, "Callback called twice");
assertEquals(Integer.valueOf(2), callingCount, "Callback called twice");
}
}