From ba695d89f2164b66cef61bb60a1a8405b02e4f5b Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Thu, 15 Feb 2024 08:29:58 +0530 Subject: [PATCH] Add uses of StringUtil.joining() --- src/main/java/org/jsoup/nodes/Element.java | 36 +++++++++----------- src/main/java/org/jsoup/select/Elements.java | 30 +++++----------- 2 files changed, 25 insertions(+), 41 deletions(-) diff --git a/src/main/java/org/jsoup/nodes/Element.java b/src/main/java/org/jsoup/nodes/Element.java index a947b2d261..eb8e40cecf 100644 --- a/src/main/java/org/jsoup/nodes/Element.java +++ b/src/main/java/org/jsoup/nodes/Element.java @@ -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 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("")); } /** @@ -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()); } /** @@ -1599,9 +1595,9 @@ public String className() { * @return set of classnames, empty if no class attribute */ public Set classNames() { - String[] names = ClassSplit.split(className()); - Set classNames = new LinkedHashSet<>(Arrays.asList(names)); - classNames.remove(""); // if classNames() was empty, would include an empty class + String[] names = ClassSplit.split(className()); + Set classNames = new LinkedHashSet<>(Arrays.asList(names)); + classNames.remove(""); // if classNames() was empty, would include an empty class return classNames; } diff --git a/src/main/java/org/jsoup/select/Elements.java b/src/main/java/org/jsoup/select/Elements.java index c344f2e06d..2d305857b3 100644 --- a/src/main/java/org/jsoup/select/Elements.java +++ b/src/main/java/org/jsoup/select/Elements.java @@ -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(" ")); } /** @@ -260,13 +256,9 @@ public List 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")); } /** @@ -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")); } /**