Skip to content

Commit

Permalink
Simplify node filtering operations using Streams API (#2080)
Browse files Browse the repository at this point in the history
  • Loading branch information
Isira-Seneviratne authored Dec 23, 2023
1 parent 78be89a commit d4b2c36
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 21 deletions.
14 changes: 5 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,15 @@
<version>2.3.3_r2</version>
</signature>
<ignores>
<ignore>java.util.function.Consumer</ignore>
<ignore>java.util.function.Supplier</ignore>
<ignore>java.util.function.*</ignore>
<ignore>java.util.stream.*</ignore>
<ignore>java.lang.ThreadLocal</ignore>
<ignore>java.io.UncheckedIOException</ignore>
<ignore>java.util.function.Predicate</ignore>
<ignore>java.util.function.UnaryOperator</ignore>
<ignore>java.util.stream.Stream</ignore>
<ignore>java.util.stream.StreamSupport</ignore>
<ignore>java.util.List</ignore> <!-- List#stream() -->
<ignore>java.util.Objects</ignore>
<ignore>java.util.Optional</ignore>
<ignore>java.util.Spliterator</ignore>
<ignore>java.util.Spliterators</ignore>
<ignore>java.util.Optional</ignore>
<ignore>java.util.stream.Collector</ignore>
<ignore>java.util.stream.Collectors</ignore>

<ignore>java.net.HttpURLConnection</ignore><!-- .setAuthenticator(java.net.Authenticator) in Java 9; only used in multirelease 9+ version -->
</ignores>
Expand Down
22 changes: 10 additions & 12 deletions src/main/java/org/jsoup/nodes/Element.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.function.Consumer;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.jsoup.internal.Normalizer.normalize;
Expand Down Expand Up @@ -411,6 +412,13 @@ public Stream<Element> stream() {
return NodeUtils.stream(this, Element.class);
}

private <T> List<T> filterNodes(Class<T> clazz) {
return childNodes.stream()
.filter(clazz::isInstance)
.map(clazz::cast)
.collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList));
}

/**
* Get this element's child text nodes. The list is unmodifiable but the text nodes may be manipulated.
* <p>
Expand All @@ -428,12 +436,7 @@ public Stream<Element> stream() {
* </ul>
*/
public List<TextNode> textNodes() {
List<TextNode> textNodes = new ArrayList<>();
for (Node node : childNodes) {
if (node instanceof TextNode)
textNodes.add((TextNode) node);
}
return Collections.unmodifiableList(textNodes);
return filterNodes(TextNode.class);
}

/**
Expand All @@ -446,12 +449,7 @@ public List<TextNode> textNodes() {
* @see #data()
*/
public List<DataNode> dataNodes() {
List<DataNode> dataNodes = new ArrayList<>();
for (Node node : childNodes) {
if (node instanceof DataNode)
dataNodes.add((DataNode) node);
}
return Collections.unmodifiableList(dataNodes);
return filterNodes(DataNode.class);
}

/**
Expand Down

0 comments on commit d4b2c36

Please sign in to comment.