diff --git a/src/main/java/org/cqfn/astranaut/core/algorithms/DeepTraversal.java b/src/main/java/org/cqfn/astranaut/core/algorithms/DeepTraversal.java index ae79d69..f24d99f 100644 --- a/src/main/java/org/cqfn/astranaut/core/algorithms/DeepTraversal.java +++ b/src/main/java/org/cqfn/astranaut/core/algorithms/DeepTraversal.java @@ -25,6 +25,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.Optional; import org.cqfn.astranaut.core.Node; /** @@ -54,10 +55,10 @@ public DeepTraversal(final Node root) { * And yes, you can use this algorithm not only to find nodes, but also just to traverse * the tree in the specific order. * @param visitor Visitor that processes nodes - * @return Found node or {@code null} if no node is found + * @return Found node (optional) */ - public Node findFirst(final Visitor visitor) { - return DeepTraversal.findFirst(this.root, visitor); + public Optional findFirst(final Visitor visitor) { + return Optional.ofNullable(DeepTraversal.findFirst(this.root, visitor)); } /** diff --git a/src/test/java/org/cqfn/astranaut/core/algorithms/DeepTraversalTest.java b/src/test/java/org/cqfn/astranaut/core/algorithms/DeepTraversalTest.java index a2a6a19..3b76c48 100644 --- a/src/test/java/org/cqfn/astranaut/core/algorithms/DeepTraversalTest.java +++ b/src/test/java/org/cqfn/astranaut/core/algorithms/DeepTraversalTest.java @@ -24,6 +24,7 @@ package org.cqfn.astranaut.core.algorithms; import java.util.List; +import java.util.Optional; import org.cqfn.astranaut.core.DraftNode; import org.cqfn.astranaut.core.Node; import org.junit.jupiter.api.Assertions; @@ -39,17 +40,17 @@ class DeepTraversalTest { void testFindFirst() { final Node root = DraftNode.createByDescription("A(B,C,D(E<\"eee\">,F<\"fff\">)))"); final DeepTraversal traversal = new DeepTraversal(root); - final Node node = traversal.findFirst(node1 -> !node1.getData().isEmpty()); - Assertions.assertNotNull(node); - Assertions.assertEquals("E", node.getTypeName()); + final Optional node = traversal.findFirst(node1 -> !node1.getData().isEmpty()); + Assertions.assertTrue(node.isPresent()); + Assertions.assertEquals("E", node.get().getTypeName()); } @Test void testNotFoundFirst() { final Node root = DraftNode.createByDescription("A(B,C,D)"); final DeepTraversal traversal = new DeepTraversal(root); - final Node node = traversal.findFirst(node1 -> !node1.getData().isEmpty()); - Assertions.assertNull(node); + final Optional node = traversal.findFirst(node1 -> !node1.getData().isEmpty()); + Assertions.assertFalse(node.isPresent()); } @Test