From ee5fe8faab1b2bdc4c81d840a5545765e2f7260a Mon Sep 17 00:00:00 2001 From: amit2103 Date: Sun, 14 Oct 2018 15:13:39 +0530 Subject: [PATCH 1/5] [BAEL-9461] - Added code examples for difference between thenApply() and thenCompose() --- .../CompletableFutureLongRunningUnitTest.java | 20 +++++++++++++++++++ .../log4j2/${sys:logging.folder.path} | 0 2 files changed, 20 insertions(+) delete mode 100755 logging-modules/log4j2/${sys:logging.folder.path} diff --git a/core-java-concurrency/src/test/java/com/baeldung/completablefuture/CompletableFutureLongRunningUnitTest.java b/core-java-concurrency/src/test/java/com/baeldung/completablefuture/CompletableFutureLongRunningUnitTest.java index 45d2ec68e4..d9cf8ae019 100644 --- a/core-java-concurrency/src/test/java/com/baeldung/completablefuture/CompletableFutureLongRunningUnitTest.java +++ b/core-java-concurrency/src/test/java/com/baeldung/completablefuture/CompletableFutureLongRunningUnitTest.java @@ -184,5 +184,25 @@ public class CompletableFutureLongRunningUnitTest { assertEquals("Hello World", future.get()); } + + @Test + public void whenPassingTransformation_thenFunctionExecutionWithThenApply() throws InterruptedException, ExecutionException { + CompletableFuture finalResult = compute().thenApply(s -> s + 1); + assertTrue(finalResult.get() == 11); + } + + @Test + public void whenPassingPreviousStage_thenFunctionExecutionWithThenCompose() throws InterruptedException, ExecutionException { + CompletableFuture finalResult = compute().thenCompose(this::computeAnother); + assertTrue(finalResult.get() == 20); + } + + public CompletableFuture compute(){ + return CompletableFuture.supplyAsync(() -> 10); + } + + public CompletableFuture computeAnother(Integer i){ + return CompletableFuture.supplyAsync(() -> 10 + i); + } } \ No newline at end of file diff --git a/logging-modules/log4j2/${sys:logging.folder.path} b/logging-modules/log4j2/${sys:logging.folder.path} deleted file mode 100755 index e69de29bb2..0000000000 From 29cee792a889d72c211251ee31a5052bf118c7ac Mon Sep 17 00:00:00 2001 From: amit2103 Date: Sun, 14 Oct 2018 20:02:51 +0530 Subject: [PATCH 2/5] [BAEL-9601] - Upgraded groovy-all version in libraries module --- libraries/pom.xml | 3 ++- logging-modules/log4j2/{${sys:logging.folder.path} => ${sys} | 0 2 files changed, 2 insertions(+), 1 deletion(-) rename logging-modules/log4j2/{${sys:logging.folder.path} => ${sys} (100%) mode change 100755 => 100644 diff --git a/libraries/pom.xml b/libraries/pom.xml index 91c54b6113..6bbe8e6f1c 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -358,6 +358,7 @@ org.codehaus.groovy groovy-all + pom ${groovy.version} @@ -922,7 +923,7 @@ 2.3.0 0.9.12 1.19 - 2.4.10 + 2.5.2 1.1.0 3.9.0 2.0.4 diff --git a/logging-modules/log4j2/${sys:logging.folder.path} b/logging-modules/log4j2/${sys old mode 100755 new mode 100644 similarity index 100% rename from logging-modules/log4j2/${sys:logging.folder.path} rename to logging-modules/log4j2/${sys From 38ddcc6477ca324e248173a102be6fd63276a7d9 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Sun, 14 Oct 2018 19:26:46 +0300 Subject: [PATCH 3/5] rename test to manual --- ...{SystemsUtilsUnitTest.java => SystemsUtilsManualTest.java} | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) rename libraries/src/test/java/com/baeldung/commons/lang3/test/{SystemsUtilsUnitTest.java => SystemsUtilsManualTest.java} (86%) diff --git a/libraries/src/test/java/com/baeldung/commons/lang3/test/SystemsUtilsUnitTest.java b/libraries/src/test/java/com/baeldung/commons/lang3/test/SystemsUtilsManualTest.java similarity index 86% rename from libraries/src/test/java/com/baeldung/commons/lang3/test/SystemsUtilsUnitTest.java rename to libraries/src/test/java/com/baeldung/commons/lang3/test/SystemsUtilsManualTest.java index 0efe97f912..cb45ebc24d 100644 --- a/libraries/src/test/java/com/baeldung/commons/lang3/test/SystemsUtilsUnitTest.java +++ b/libraries/src/test/java/com/baeldung/commons/lang3/test/SystemsUtilsManualTest.java @@ -6,8 +6,10 @@ import org.apache.commons.lang3.SystemUtils; import static org.assertj.core.api.Assertions.assertThat; import org.junit.Test; -public class SystemsUtilsUnitTest { +public class SystemsUtilsManualTest { + // the paths depend on the OS and installed version of Java + @Test public void givenSystemUtilsClass_whenCalledgetJavaHome_thenCorrect() { assertThat(SystemUtils.getJavaHome()).isEqualTo(new File("/usr/lib/jvm/java-8-oracle/jre")); From 23b1323446c2c535ab8fdf3aef19761e0649bc98 Mon Sep 17 00:00:00 2001 From: KevinGilmore Date: Sun, 14 Oct 2018 11:37:16 -0500 Subject: [PATCH 4/5] BAEL-2200: Spring Boot auto-configuration report (#5453) * BAEL-1766: Update README * BAEL-1853: add link to article * BAEL-1801: add link to article * Added links back to articles * Add links back to articles * BAEL-1795: Update README * BAEL-1901 and BAEL-1555 add links back to article * BAEL-2026 add link back to article * BAEL-2029: add link back to article * BAEL-1898: Add link back to article * BAEL-2102 and BAEL-2131 Add links back to articles * BAEL-2132 Add link back to article * BAEL-1980: add link back to article * BAEL-2200: Display auto-configuration report in Spring Boot --- .../baeldung/h2db/auto/configuration/AutoConfigurationDemo.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-boot-h2/spring-boot-h2-database/src/main/java/com/baeldung/h2db/auto/configuration/AutoConfigurationDemo.java b/spring-boot-h2/spring-boot-h2-database/src/main/java/com/baeldung/h2db/auto/configuration/AutoConfigurationDemo.java index 87a191554b..8d92e18754 100644 --- a/spring-boot-h2/spring-boot-h2-database/src/main/java/com/baeldung/h2db/auto/configuration/AutoConfigurationDemo.java +++ b/spring-boot-h2/spring-boot-h2-database/src/main/java/com/baeldung/h2db/auto/configuration/AutoConfigurationDemo.java @@ -4,7 +4,7 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; -@SpringBootApplication(scanBasePackages = "com.baeldung.h2db.auto-configuration") +@SpringBootApplication public class AutoConfigurationDemo { public static void main(String[] args) { From 285219c54c3ffe993e9bb9e7819d5b4cb9802de3 Mon Sep 17 00:00:00 2001 From: fanatixan Date: Sun, 14 Oct 2018 19:08:11 +0200 Subject: [PATCH 5/5] Bael-2210 - Heap Sort (#5446) * implementing heap * Heap sort refactor --- .../main/java/com/baeldung/heapsort/Heap.java | 136 ++++++++++++++++++ .../com/baeldung/heapsort/HeapUnitTest.java | 36 +++++ 2 files changed, 172 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/heapsort/Heap.java create mode 100644 core-java/src/test/java/com/baeldung/heapsort/HeapUnitTest.java diff --git a/core-java/src/main/java/com/baeldung/heapsort/Heap.java b/core-java/src/main/java/com/baeldung/heapsort/Heap.java new file mode 100644 index 0000000000..95e0e1d8cd --- /dev/null +++ b/core-java/src/main/java/com/baeldung/heapsort/Heap.java @@ -0,0 +1,136 @@ +package com.baeldung.heapsort; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class Heap> { + + private List elements = new ArrayList<>(); + + public static > List sort(Iterable elements) { + Heap heap = of(elements); + + List result = new ArrayList<>(); + + while (!heap.isEmpty()) { + result.add(heap.pop()); + } + + return result; + } + + public static > Heap of(E... elements) { + return of(Arrays.asList(elements)); + } + + public static > Heap of(Iterable elements) { + Heap result = new Heap<>(); + for (E element : elements) { + result.add(element); + } + return result; + } + + public void add(E e) { + elements.add(e); + int elementIndex = elements.size() - 1; + while (!isRoot(elementIndex) && !isCorrectChild(elementIndex)) { + int parentIndex = parentIndex(elementIndex); + swap(elementIndex, parentIndex); + elementIndex = parentIndex; + } + } + + public E pop() { + if (isEmpty()) { + throw new IllegalStateException("You cannot pop from an empty heap"); + } + + E result = elementAt(0); + + int lasElementIndex = elements.size() - 1; + swap(0, lasElementIndex); + elements.remove(lasElementIndex); + + int elementIndex = 0; + while (!isLeaf(elementIndex) && !isCorrectParent(elementIndex)) { + int smallerChildIndex = smallerChildIndex(elementIndex); + swap(elementIndex, smallerChildIndex); + elementIndex = smallerChildIndex; + } + + return result; + } + + public boolean isEmpty() { + return elements.isEmpty(); + } + + private boolean isRoot(int index) { + return index == 0; + } + + private int smallerChildIndex(int index) { + int leftChildIndex = leftChildIndex(index); + int rightChildIndex = rightChildIndex(index); + + if (!isValidIndex(rightChildIndex)) { + return leftChildIndex; + } + + if (elementAt(leftChildIndex).compareTo(elementAt(rightChildIndex)) < 0) { + return leftChildIndex; + } + + return rightChildIndex; + } + + private boolean isLeaf(int index) { + return !isValidIndex(leftChildIndex(index)); + } + + private boolean isCorrectParent(int index) { + return isCorrect(index, leftChildIndex(index)) && isCorrect(index, rightChildIndex(index)); + } + + private boolean isCorrectChild(int index) { + return isCorrect(parentIndex(index), index); + } + + private boolean isCorrect(int parentIndex, int childIndex) { + if (!isValidIndex(parentIndex) || !isValidIndex(childIndex)) { + return true; + } + + return elementAt(parentIndex).compareTo(elementAt(childIndex)) < 0; + } + + private boolean isValidIndex(int index) { + return index < elements.size(); + } + + private void swap(int index1, int index2) { + E element1 = elementAt(index1); + E element2 = elementAt(index2); + elements.set(index1, element2); + elements.set(index2, element1); + } + + private E elementAt(int index) { + return elements.get(index); + } + + private int parentIndex(int index) { + return (index - 1) / 2; + } + + private int leftChildIndex(int index) { + return 2 * index + 1; + } + + private int rightChildIndex(int index) { + return 2 * index + 2; + } + +} diff --git a/core-java/src/test/java/com/baeldung/heapsort/HeapUnitTest.java b/core-java/src/test/java/com/baeldung/heapsort/HeapUnitTest.java new file mode 100644 index 0000000000..cc3e49b6c9 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/heapsort/HeapUnitTest.java @@ -0,0 +1,36 @@ +package com.baeldung.heapsort; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Arrays; +import java.util.List; + +import org.junit.Test; + +public class HeapUnitTest { + + @Test + public void givenNotEmptyHeap_whenPopCalled_thenItShouldReturnSmallestElement() { + // given + Heap heap = Heap.of(3, 5, 1, 4, 2); + + // when + int head = heap.pop(); + + // then + assertThat(head).isEqualTo(1); + } + + @Test + public void givenNotEmptyIterable_whenSortCalled_thenItShouldReturnElementsInSortedList() { + // given + List elements = Arrays.asList(3, 5, 1, 4, 2); + + // when + List sortedElements = Heap.sort(elements); + + // then + assertThat(sortedElements).isEqualTo(Arrays.asList(1, 2, 3, 4, 5)); + } + +}