Skip to content

Commit

Permalink
Add uses of StringUtil.joining()
Browse files Browse the repository at this point in the history
  • Loading branch information
Isira-Seneviratne committed Feb 15, 2024
1 parent 1f1f72d commit ba695d8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 41 deletions.
36 changes: 16 additions & 20 deletions src/main/java/org/jsoup/nodes/Element.java
Original file line number Diff line number Diff line change
Expand Up @@ -1430,17 +1430,20 @@ 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 getWholeText(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 getWholeText(Stream<Node> stream) {
return stream.map(node -> {
if (node instanceof TextNode) {
return ((TextNode) node).getWholeText();
} else if (node.nameIs("br")) {
return "\n";
} else {
return "";
}
})
.collect(StringUtil.joining(""));
}

/**
Expand All @@ -1453,14 +1456,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 getWholeText(childNodes.stream());
}

/**
Expand Down Expand Up @@ -1599,9 +1595,9 @@ public String className() {
* @return set of classnames, empty if no class attribute
*/
public Set<String> classNames() {
String[] names = ClassSplit.split(className());
Set<String> classNames = new LinkedHashSet<>(Arrays.asList(names));
classNames.remove(""); // if classNames() was empty, would include an empty class
String[] names = ClassSplit.split(className());
Set<String> classNames = new LinkedHashSet<>(Arrays.asList(names));
classNames.remove(""); // if classNames() was empty, would include an empty class

return classNames;
}
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

0 comments on commit ba695d8

Please sign in to comment.