Skip to content

Commit

Permalink
Uncomment stream block in Element (#1988)
Browse files Browse the repository at this point in the history
* Uncomment stream block in Element

* Add custom StringJoiner collector

---------

Co-authored-by: Jonathan Hedley <jonathan@hedley.net>
  • Loading branch information
Isira-Seneviratne and jhy authored Dec 29, 2023
1 parent fea3034 commit 38615af
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
<ignore>java.util.List</ignore> <!-- List#stream() -->
<ignore>java.util.Objects</ignore>
<ignore>java.util.Optional</ignore>
<ignore>java.util.Set</ignore> <!-- Set#stream() -->
<ignore>java.util.Spliterator</ignore>
<ignore>java.util.Spliterators</ignore>

Expand Down
19 changes: 19 additions & 0 deletions src/main/java/org/jsoup/internal/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import java.util.Iterator;
import java.util.Stack;
import java.util.regex.Pattern;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
A minimal String utility class. Designed for <b>internal</b> jsoup use only - the API and outcome may change without
Expand Down Expand Up @@ -375,6 +377,23 @@ public static String releaseBuilder(StringBuilder sb) {
return string;
}

/**
* Return a {@link Collector} similar to the one returned by {@link Collectors#joining(CharSequence)},
* but backed by jsoup's {@link StringJoiner}, which allows for more efficient garbage collection.
*
* @param delimiter The delimiter for separating the strings.
* @return A {@code Collector} which concatenates CharSequence elements, separated by the specified delimiter
*/
public static Collector<CharSequence, ?, String> joining(String delimiter) {
return Collector.of(() -> new StringJoiner(delimiter),
StringJoiner::add,
(j1, j2) -> {
j1.append(j2.complete());
return j1;
},
StringJoiner::complete);
}

private static final int MaxCachedBuilderSize = 8 * 1024;
private static final int MaxIdleBuilders = 8;
}
10 changes: 4 additions & 6 deletions src/main/java/org/jsoup/nodes/Element.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.jsoup.parser.ParseSettings;
import org.jsoup.parser.Parser;
import org.jsoup.parser.Tag;
import org.jsoup.parser.TokenQueue;
import org.jsoup.select.Collector;
import org.jsoup.select.Elements;
import org.jsoup.select.Evaluator;
Expand Down Expand Up @@ -935,12 +936,9 @@ private String cssSelectorComponent() {
// Escape tagname, and translate HTML namespace ns:tag to CSS namespace syntax ns|tag
String tagName = escapeCssIdentifier(tagName()).replace("\\:", "|");
StringBuilder selector = StringUtil.borrowBuilder().append(tagName);
// String classes = StringUtil.join(classNames().stream().map(TokenQueue::escapeCssIdentifier).iterator(), ".");
// todo - replace with ^^ in 1.16.1 when we enable Android support for stream etc
StringUtil.StringJoiner escapedClasses = new StringUtil.StringJoiner(".");
for (String name : classNames()) escapedClasses.add(escapeCssIdentifier(name));
String classes = escapedClasses.complete();
if (classes.length() > 0)
String classes = classNames().stream().map(TokenQueue::escapeCssIdentifier)
.collect(StringUtil.joining("."));
if (!classes.isEmpty())
selector.append('.').append(classes);

if (parent() == null || parent() instanceof Document) // don't add Document to selector, as will always have a html node
Expand Down

0 comments on commit 38615af

Please sign in to comment.