Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add uses of StringUtil.joining() #2119

Merged
merged 3 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 8 additions & 17 deletions src/main/java/org/jsoup/nodes/Element.java
Original file line number Diff line number Diff line change
Expand Up @@ -1430,17 +1430,15 @@ public void tail(Node node, int depth) {
@see #wholeOwnText()
*/
public String wholeText() {
final StringBuilder accum = StringUtil.borrowBuilder();
nodeStream().forEach(node -> appendWholeText(node, accum));
return StringUtil.releaseBuilder(accum);
return wholeTextOf(nodeStream());
}

private static void appendWholeText(Node node, StringBuilder accum) {
if (node instanceof TextNode) {
accum.append(((TextNode) node).getWholeText());
} else if (node.nameIs("br")) {
accum.append("\n");
}
private static String wholeTextOf(Stream<Node> stream) {
return stream.map(node -> {
if (node instanceof TextNode) return ((TextNode) node).getWholeText();
if (node.nameIs("br")) return "\n";
return "";
}).collect(StringUtil.joining(""));
}

/**
Expand All @@ -1453,14 +1451,7 @@ private static void appendWholeText(Node node, StringBuilder accum) {
@since 1.15.1
*/
public String wholeOwnText() {
final StringBuilder accum = StringUtil.borrowBuilder();
final int size = childNodeSize();
for (int i = 0; i < size; i++) {
Node node = childNodes.get(i);
appendWholeText(node, accum);
}

return StringUtil.releaseBuilder(accum);
return wholeTextOf(childNodes.stream());
}

/**
Expand Down
30 changes: 9 additions & 21 deletions src/main/java/org/jsoup/select/Elements.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,9 @@ public Elements val(String value) {
* @see #eachText()
*/
public String text() {
StringBuilder sb = StringUtil.borrowBuilder();
for (Element element : this) {
if (sb.length() != 0)
sb.append(" ");
sb.append(element.text());
}
return StringUtil.releaseBuilder(sb);
return stream()
.map(Element::text)
.collect(StringUtil.joining(" "));
}

/**
Expand Down Expand Up @@ -260,13 +256,9 @@ public List<String> eachText() {
* @see #outerHtml()
*/
public String html() {
StringBuilder sb = StringUtil.borrowBuilder();
for (Element element : this) {
if (sb.length() != 0)
sb.append("\n");
sb.append(element.html());
}
return StringUtil.releaseBuilder(sb);
return stream()
.map(Element::html)
.collect(StringUtil.joining("\n"));
}

/**
Expand All @@ -276,13 +268,9 @@ public String html() {
* @see #html()
*/
public String outerHtml() {
StringBuilder sb = StringUtil.borrowBuilder();
for (Element element : this) {
if (sb.length() != 0)
sb.append("\n");
sb.append(element.outerHtml());
}
return StringUtil.releaseBuilder(sb);
return stream()
.map(Element::outerHtml)
.collect(StringUtil.joining("\n"));
}

/**
Expand Down
Loading