From 0e38d8a520ea66cd431e80cd0bf13a4c7844583d Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Thu, 15 Feb 2024 08:29:58 +0530 Subject: [PATCH 1/2] Add uses of StringUtil.joining() --- src/main/java/org/jsoup/nodes/Element.java | 30 +++++++++----------- src/main/java/org/jsoup/select/Elements.java | 30 ++++++-------------- 2 files changed, 22 insertions(+), 38 deletions(-) diff --git a/src/main/java/org/jsoup/nodes/Element.java b/src/main/java/org/jsoup/nodes/Element.java index a947b2d261..c70742ba36 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()); } /** 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")); } /** From e6847486c51380f9cd961f205d8b4dbd29a7c786 Mon Sep 17 00:00:00 2001 From: Jonathan Hedley Date: Mon, 29 Jul 2024 14:51:46 +1000 Subject: [PATCH 2/2] Tweaked method name, code format --- src/main/java/org/jsoup/nodes/Element.java | 19 +++++++------------ src/main/java/org/jsoup/select/Elements.java | 12 ++++++------ 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/jsoup/nodes/Element.java b/src/main/java/org/jsoup/nodes/Element.java index c70742ba36..d923bb1c08 100644 --- a/src/main/java/org/jsoup/nodes/Element.java +++ b/src/main/java/org/jsoup/nodes/Element.java @@ -1430,20 +1430,15 @@ public void tail(Node node, int depth) { @see #wholeOwnText() */ public String wholeText() { - return getWholeText(nodeStream()); + return wholeTextOf(nodeStream()); } - private static String getWholeText(Stream stream) { + private static String wholeTextOf(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("")); + if (node instanceof TextNode) return ((TextNode) node).getWholeText(); + if (node.nameIs("br")) return "\n"; + return ""; + }).collect(StringUtil.joining("")); } /** @@ -1456,7 +1451,7 @@ private static String getWholeText(Stream stream) { @since 1.15.1 */ public String wholeOwnText() { - return getWholeText(childNodes.stream()); + return wholeTextOf(childNodes.stream()); } /** diff --git a/src/main/java/org/jsoup/select/Elements.java b/src/main/java/org/jsoup/select/Elements.java index 2d305857b3..1986c6e457 100644 --- a/src/main/java/org/jsoup/select/Elements.java +++ b/src/main/java/org/jsoup/select/Elements.java @@ -215,8 +215,8 @@ public Elements val(String value) { */ public String text() { return stream() - .map(Element::text) - .collect(StringUtil.joining(" ")); + .map(Element::text) + .collect(StringUtil.joining(" ")); } /** @@ -257,8 +257,8 @@ public List eachText() { */ public String html() { return stream() - .map(Element::html) - .collect(StringUtil.joining("\n")); + .map(Element::html) + .collect(StringUtil.joining("\n")); } /** @@ -269,8 +269,8 @@ public String html() { */ public String outerHtml() { return stream() - .map(Element::outerHtml) - .collect(StringUtil.joining("\n")); + .map(Element::outerHtml) + .collect(StringUtil.joining("\n")); } /**