Skip to content

Commit 6b5a6aa

Browse files
committed
Merge branch 'master' of https://github.com/eugenp/tutorials into BAEL-9635
2 parents edff913 + 285219c commit 6b5a6aa

7 files changed

Lines changed: 198 additions & 3 deletions

File tree

‎core-java-concurrency/src/test/java/com/baeldung/completablefuture/CompletableFutureLongRunningUnitTest.java‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,5 +184,25 @@ public void whenAddingThenApplyAsyncToFuture_thenFunctionExecutesAfterComputatio
184184

185185
assertEquals("Hello World", future.get());
186186
}
187+
188+
@Test
189+
public void whenPassingTransformation_thenFunctionExecutionWithThenApply() throws InterruptedException, ExecutionException {
190+
CompletableFuture<Integer> finalResult = compute().thenApply(s -> s + 1);
191+
assertTrue(finalResult.get() == 11);
192+
}
193+
194+
@Test
195+
public void whenPassingPreviousStage_thenFunctionExecutionWithThenCompose() throws InterruptedException, ExecutionException {
196+
CompletableFuture<Integer> finalResult = compute().thenCompose(this::computeAnother);
197+
assertTrue(finalResult.get() == 20);
198+
}
199+
200+
public CompletableFuture<Integer> compute(){
201+
return CompletableFuture.supplyAsync(() -> 10);
202+
}
203+
204+
public CompletableFuture<Integer> computeAnother(Integer i){
205+
return CompletableFuture.supplyAsync(() -> 10 + i);
206+
}
187207

188208
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package com.baeldung.heapsort;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
public class Heap<E extends Comparable<E>> {
8+
9+
private List<E> elements = new ArrayList<>();
10+
11+
public static <E extends Comparable<E>> List<E> sort(Iterable<E> elements) {
12+
Heap<E> heap = of(elements);
13+
14+
List<E> result = new ArrayList<>();
15+
16+
while (!heap.isEmpty()) {
17+
result.add(heap.pop());
18+
}
19+
20+
return result;
21+
}
22+
23+
public static <E extends Comparable<E>> Heap<E> of(E... elements) {
24+
return of(Arrays.asList(elements));
25+
}
26+
27+
public static <E extends Comparable<E>> Heap<E> of(Iterable<E> elements) {
28+
Heap<E> result = new Heap<>();
29+
for (E element : elements) {
30+
result.add(element);
31+
}
32+
return result;
33+
}
34+
35+
public void add(E e) {
36+
elements.add(e);
37+
int elementIndex = elements.size() - 1;
38+
while (!isRoot(elementIndex) && !isCorrectChild(elementIndex)) {
39+
int parentIndex = parentIndex(elementIndex);
40+
swap(elementIndex, parentIndex);
41+
elementIndex = parentIndex;
42+
}
43+
}
44+
45+
public E pop() {
46+
if (isEmpty()) {
47+
throw new IllegalStateException("You cannot pop from an empty heap");
48+
}
49+
50+
E result = elementAt(0);
51+
52+
int lasElementIndex = elements.size() - 1;
53+
swap(0, lasElementIndex);
54+
elements.remove(lasElementIndex);
55+
56+
int elementIndex = 0;
57+
while (!isLeaf(elementIndex) && !isCorrectParent(elementIndex)) {
58+
int smallerChildIndex = smallerChildIndex(elementIndex);
59+
swap(elementIndex, smallerChildIndex);
60+
elementIndex = smallerChildIndex;
61+
}
62+
63+
return result;
64+
}
65+
66+
public boolean isEmpty() {
67+
return elements.isEmpty();
68+
}
69+
70+
private boolean isRoot(int index) {
71+
return index == 0;
72+
}
73+
74+
private int smallerChildIndex(int index) {
75+
int leftChildIndex = leftChildIndex(index);
76+
int rightChildIndex = rightChildIndex(index);
77+
78+
if (!isValidIndex(rightChildIndex)) {
79+
return leftChildIndex;
80+
}
81+
82+
if (elementAt(leftChildIndex).compareTo(elementAt(rightChildIndex)) < 0) {
83+
return leftChildIndex;
84+
}
85+
86+
return rightChildIndex;
87+
}
88+
89+
private boolean isLeaf(int index) {
90+
return !isValidIndex(leftChildIndex(index));
91+
}
92+
93+
private boolean isCorrectParent(int index) {
94+
return isCorrect(index, leftChildIndex(index)) && isCorrect(index, rightChildIndex(index));
95+
}
96+
97+
private boolean isCorrectChild(int index) {
98+
return isCorrect(parentIndex(index), index);
99+
}
100+
101+
private boolean isCorrect(int parentIndex, int childIndex) {
102+
if (!isValidIndex(parentIndex) || !isValidIndex(childIndex)) {
103+
return true;
104+
}
105+
106+
return elementAt(parentIndex).compareTo(elementAt(childIndex)) < 0;
107+
}
108+
109+
private boolean isValidIndex(int index) {
110+
return index < elements.size();
111+
}
112+
113+
private void swap(int index1, int index2) {
114+
E element1 = elementAt(index1);
115+
E element2 = elementAt(index2);
116+
elements.set(index1, element2);
117+
elements.set(index2, element1);
118+
}
119+
120+
private E elementAt(int index) {
121+
return elements.get(index);
122+
}
123+
124+
private int parentIndex(int index) {
125+
return (index - 1) / 2;
126+
}
127+
128+
private int leftChildIndex(int index) {
129+
return 2 * index + 1;
130+
}
131+
132+
private int rightChildIndex(int index) {
133+
return 2 * index + 2;
134+
}
135+
136+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.heapsort;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
8+
import org.junit.Test;
9+
10+
public class HeapUnitTest {
11+
12+
@Test
13+
public void givenNotEmptyHeap_whenPopCalled_thenItShouldReturnSmallestElement() {
14+
// given
15+
Heap<Integer> heap = Heap.of(3, 5, 1, 4, 2);
16+
17+
// when
18+
int head = heap.pop();
19+
20+
// then
21+
assertThat(head).isEqualTo(1);
22+
}
23+
24+
@Test
25+
public void givenNotEmptyIterable_whenSortCalled_thenItShouldReturnElementsInSortedList() {
26+
// given
27+
List<Integer> elements = Arrays.asList(3, 5, 1, 4, 2);
28+
29+
// when
30+
List<Integer> sortedElements = Heap.sort(elements);
31+
32+
// then
33+
assertThat(sortedElements).isEqualTo(Arrays.asList(1, 2, 3, 4, 5));
34+
}
35+
36+
}

‎libraries/pom.xml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@
358358
<dependency>
359359
<groupId>org.codehaus.groovy</groupId>
360360
<artifactId>groovy-all</artifactId>
361+
<type>pom</type>
361362
<version>${groovy.version}</version>
362363
</dependency>
363364
<dependency>
@@ -922,7 +923,7 @@
922923
<quartz.version>2.3.0</quartz.version>
923924
<jool.version>0.9.12</jool.version>
924925
<jmh.version>1.19</jmh.version>
925-
<groovy.version>2.4.10</groovy.version>
926+
<groovy.version>2.5.2</groovy.version>
926927
<noexception.version>1.1.0</noexception.version>
927928
<logging-interceptor.version>3.9.0</logging-interceptor.version>
928929
<yarg.version>2.0.4</yarg.version>

libraries/src/test/java/com/baeldung/commons/lang3/test/SystemsUtilsUnitTest.java renamed to libraries/src/test/java/com/baeldung/commons/lang3/test/SystemsUtilsManualTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
import static org.assertj.core.api.Assertions.assertThat;
77
import org.junit.Test;
88

9-
public class SystemsUtilsUnitTest {
9+
public class SystemsUtilsManualTest {
1010

11+
// the paths depend on the OS and installed version of Java
12+
1113
@Test
1214
public void givenSystemUtilsClass_whenCalledgetJavaHome_thenCorrect() {
1315
assertThat(SystemUtils.getJavaHome()).isEqualTo(new File("/usr/lib/jvm/java-8-oracle/jre"));

‎logging-modules/log4j2/${sys‎

Whitespace-only changes.

‎spring-boot-h2/spring-boot-h2-database/src/main/java/com/baeldung/h2db/auto/configuration/AutoConfigurationDemo.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
55
import org.springframework.context.annotation.ComponentScan;
66

7-
@SpringBootApplication(scanBasePackages = "com.baeldung.h2db.auto-configuration")
7+
@SpringBootApplication
88
public class AutoConfigurationDemo {
99

1010
public static void main(String[] args) {

0 commit comments

Comments
 (0)