Skip to content

Commit 3ec6f0c

Browse files
committed
Merge branch 'BAEL-16646' into BAEL-16646-2
# Conflicts: # core-java-modules/pom.xml # java-dates/README.md
2 parents 100d302 + bd33732 commit 3ec6f0c

738 files changed

Lines changed: 4247 additions & 1809 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,5 @@ jta/transaction-logs/
8383
software-security/sql-injection-samples/derby.log
8484
spring-soap/src/main/java/com/baeldung/springsoap/gen/
8585
/report-*.json
86-
transaction.log
86+
transaction.log
87+
*-shell.log
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.baeldung.algorithms.breadthfirstsearch;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
import java.util.*;
7+
8+
public class BreadthFirstSearchAlgorithm {
9+
10+
private static final Logger LOGGER = LoggerFactory.getLogger(BreadthFirstSearchAlgorithm.class);
11+
12+
public static <T> Optional<Tree<T>> search(T value, Tree<T> root) {
13+
Queue<Tree<T>> queue = new ArrayDeque<>();
14+
queue.add(root);
15+
16+
Tree<T> currentNode;
17+
while (!queue.isEmpty()) {
18+
currentNode = queue.remove();
19+
LOGGER.info("Visited node with value: {}", currentNode.getValue());
20+
21+
if (currentNode.getValue().equals(value)) {
22+
return Optional.of(currentNode);
23+
} else {
24+
queue.addAll(currentNode.getChildren());
25+
}
26+
}
27+
28+
return Optional.empty();
29+
}
30+
31+
public static <T> Optional<Node<T>> search(T value, Node<T> start) {
32+
Queue<Node<T>> queue = new ArrayDeque<>();
33+
queue.add(start);
34+
35+
Node<T> currentNode;
36+
Set<Node<T>> alreadyVisited = new HashSet<>();
37+
38+
while (!queue.isEmpty()) {
39+
currentNode = queue.remove();
40+
LOGGER.info("Visited node with value: {}", currentNode.getValue());
41+
42+
if (currentNode.getValue().equals(value)) {
43+
return Optional.of(currentNode);
44+
} else {
45+
alreadyVisited.add(currentNode);
46+
queue.addAll(currentNode.getNeighbors());
47+
queue.removeAll(alreadyVisited);
48+
}
49+
}
50+
51+
return Optional.empty();
52+
}
53+
54+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.algorithms.breadthfirstsearch;
2+
3+
import java.util.Collections;
4+
import java.util.HashSet;
5+
import java.util.Set;
6+
7+
public class Node<T> {
8+
9+
private T value;
10+
private Set<Node<T>> neighbors;
11+
12+
public Node(T value) {
13+
this.value = value;
14+
this.neighbors = new HashSet<>();
15+
}
16+
17+
public T getValue() {
18+
return value;
19+
}
20+
21+
public Set<Node<T>> getNeighbors() {
22+
return Collections.unmodifiableSet(neighbors);
23+
}
24+
25+
public void connect(Node<T> node) {
26+
if (this == node) throw new IllegalArgumentException("Can't connect node to itself");
27+
this.neighbors.add(node);
28+
node.neighbors.add(this);
29+
}
30+
31+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.algorithms.breadthfirstsearch;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
7+
public class Tree<T> {
8+
9+
private T value;
10+
private List<Tree<T>> children;
11+
12+
private Tree(T value) {
13+
this.value = value;
14+
this.children = new ArrayList<>();
15+
}
16+
17+
public static <T> Tree<T> of(T value) {
18+
return new Tree<>(value);
19+
}
20+
21+
public T getValue() {
22+
return value;
23+
}
24+
25+
public List<Tree<T>> getChildren() {
26+
return Collections.unmodifiableList(children);
27+
}
28+
29+
public Tree<T> addChild(T value) {
30+
Tree<T> newChild = new Tree<>(value);
31+
children.add(newChild);
32+
return newChild;
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.baeldung.algorithms.breadthfirstsearch;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
7+
class BreadthFirstSearchAlgorithmUnitTest {
8+
9+
private Tree<Integer> root;
10+
private Tree<Integer> rootFirstChild;
11+
private Tree<Integer> depthMostChild;
12+
private Tree<Integer> rootSecondChild;
13+
14+
private Node<Integer> start;
15+
private Node<Integer> firstNeighbor;
16+
private Node<Integer> firstNeighborNeighbor;
17+
private Node<Integer> secondNeighbor;
18+
19+
@Test
20+
void givenTree_whenSearchTen_thenRoot() {
21+
initTree();
22+
assertThat(BreadthFirstSearchAlgorithm.search(10, root)).isPresent().contains(root);
23+
}
24+
25+
@Test
26+
void givenTree_whenSearchThree_thenDepthMostValue() {
27+
initTree();
28+
assertThat(BreadthFirstSearchAlgorithm.search(3, root)).isPresent().contains(depthMostChild);
29+
}
30+
31+
@Test
32+
void givenTree_whenSearchFour_thenRootSecondChild() {
33+
initTree();
34+
assertThat(BreadthFirstSearchAlgorithm.search(4, root)).isPresent().contains(rootSecondChild);
35+
}
36+
37+
@Test
38+
void givenTree_whenSearchFive_thenNotFound() {
39+
initTree();
40+
assertThat(BreadthFirstSearchAlgorithm.search(5, root)).isEmpty();
41+
}
42+
43+
private void initTree() {
44+
root = Tree.of(10);
45+
rootFirstChild = root.addChild(2);
46+
depthMostChild = rootFirstChild.addChild(3);
47+
rootSecondChild = root.addChild(4);
48+
}
49+
50+
@Test
51+
void givenNode_whenSearchTen_thenStart() {
52+
initNode();
53+
assertThat(BreadthFirstSearchAlgorithm.search(10, firstNeighborNeighbor)).isPresent().contains(start);
54+
}
55+
56+
@Test
57+
void givenNode_whenSearchThree_thenNeighborNeighbor() {
58+
initNode();
59+
assertThat(BreadthFirstSearchAlgorithm.search(3, firstNeighborNeighbor)).isPresent().contains(firstNeighborNeighbor);
60+
}
61+
62+
@Test
63+
void givenNode_whenSearchFour_thenSecondNeighbor() {
64+
initNode();
65+
assertThat(BreadthFirstSearchAlgorithm.search(4, firstNeighborNeighbor)).isPresent().contains(secondNeighbor);
66+
}
67+
68+
@Test
69+
void givenNode_whenSearchFive_thenNotFound() {
70+
initNode();
71+
assertThat(BreadthFirstSearchAlgorithm.search(5, firstNeighborNeighbor)).isEmpty();
72+
}
73+
74+
private void initNode() {
75+
start = new Node<>(10);
76+
firstNeighbor = new Node<>(2);
77+
start.connect(firstNeighbor);
78+
79+
firstNeighborNeighbor = new Node<>(3);
80+
firstNeighbor.connect(firstNeighborNeighbor);
81+
firstNeighborNeighbor.connect(start);
82+
83+
secondNeighbor = new Node<>(4);
84+
start.connect(secondNeighbor);
85+
}
86+
}

animal-sniffer-mvn-plugin/pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
22
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
33
<modelVersion>4.0.0</modelVersion>
4-
<groupId>com.baeldung</groupId>
54
<artifactId>animal-sniffer-mvn-plugin</artifactId>
65
<version>1.0-SNAPSHOT</version>
76
<name>animal-sniffer-mvn-plugin</name>

apache-avro/pom.xml

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@
1414
</parent>
1515

1616
<dependencies>
17-
<dependency>
18-
<groupId>junit</groupId>
19-
<artifactId>junit</artifactId>
20-
<version>${junit.version}</version>
21-
<scope>test</scope>
22-
</dependency>
2317
<dependency>
2418
<groupId>org.slf4j</groupId>
2519
<artifactId>slf4j-simple</artifactId>
@@ -46,15 +40,6 @@
4640

4741
<build>
4842
<plugins>
49-
<plugin>
50-
<groupId>org.apache.maven.plugins</groupId>
51-
<artifactId>maven-compiler-plugin</artifactId>
52-
<version>${compiler-plugin.version}</version>
53-
<configuration>
54-
<source>${java.version}</source>
55-
<target>${java.version}</target>
56-
</configuration>
57-
</plugin>
5843
<plugin>
5944
<groupId>org.apache.avro</groupId>
6045
<artifactId>avro-maven-plugin</artifactId>
@@ -79,8 +64,6 @@
7964
</build>
8065

8166
<properties>
82-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
83-
<compiler-plugin.version>3.5</compiler-plugin.version>
8467
<avro.version>1.8.2</avro.version>
8568
<slf4j.version>1.7.25</slf4j.version>
8669
</properties>

apache-fop/pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
22
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
33
<modelVersion>4.0.0</modelVersion>
4-
<groupId>com.baeldung</groupId>
54
<artifactId>apache-fop</artifactId>
65
<version>0.1-SNAPSHOT</version>
76
<name>apache-fop</name>

apache-geode/pom.xml

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,7 @@
1919
<artifactId>geode-core</artifactId>
2020
<version>${geode.core}</version>
2121
</dependency>
22-
<dependency>
23-
<groupId>junit</groupId>
24-
<artifactId>junit</artifactId>
25-
<version>${junit.version}</version>
26-
</dependency>
2722
</dependencies>
28-
29-
<build>
30-
<plugins>
31-
<plugin>
32-
<groupId>org.apache.maven.plugins</groupId>
33-
<artifactId>maven-compiler-plugin</artifactId>
34-
<configuration>
35-
<source>${java.version}</source>
36-
<target>${java.version}</target>
37-
</configuration>
38-
</plugin>
39-
</plugins>
40-
</build>
4123

4224
<properties>
4325
<geode.core>1.6.0</geode.core>

apache-meecrowave/pom.xml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
22
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
33
<modelVersion>4.0.0</modelVersion>
4-
<groupId>com.baeldung</groupId>
54
<artifactId>apache-meecrowave</artifactId>
65
<version>0.0.1</version>
76
<name>apache-meecrowave</name>
@@ -38,13 +37,6 @@
3837
<version>${meecrowave-junit.version}</version>
3938
<scope>test</scope>
4039
</dependency>
41-
<!-- https://mvnrepository.com/artifact/junit/junit -->
42-
<dependency>
43-
<groupId>junit</groupId>
44-
<artifactId>junit</artifactId>
45-
<version>${junit.version}</version>
46-
<scope>test</scope>
47-
</dependency>
4840
</dependencies>
4941

5042
<build>

0 commit comments

Comments
 (0)