Skip to content

Commit

Permalink
Make optional return value instead of nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
kniazkov committed Apr 17, 2024
1 parent c9c9305 commit e048840
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.cqfn.astranaut.core.Node;

/**
Expand Down Expand Up @@ -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<Node> findFirst(final Visitor visitor) {
return Optional.ofNullable(DeepTraversal.findFirst(this.root, visitor));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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> 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> node = traversal.findFirst(node1 -> !node1.getData().isEmpty());
Assertions.assertFalse(node.isPresent());
}

@Test
Expand Down

0 comments on commit e048840

Please sign in to comment.