You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by pk...@apache.org on 2024/03/19 13:18:53 UTC
(logging-log4j2) 04/07: Add `Strings#trimToOptional`
This is an automated email from the ASF dual-hosted git repository.
pkarwasz pushed a commit to branch fix/backport-log4j3-api
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
commit 5bd7a760f41c2878f3628fca937187c99a5d18ce
Author: Piotr P. Karwasz <pi...@karwasz.org>
AuthorDate: Fri Mar 15 14:28:49 2024 +0100
Add `Strings#trimToOptional`
---
.../main/java/org/apache/logging/log4j/util/Strings.java | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/util/Strings.java b/log4j-api/src/main/java/org/apache/logging/log4j/util/Strings.java
index bee54324f4..8eea0e782c 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/util/Strings.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/util/Strings.java
@@ -21,6 +21,7 @@ import static org.apache.logging.log4j.util.StringBuilders.trimToMaxSize;
import java.util.Iterator;
import java.util.Locale;
import java.util.Objects;
+import java.util.Optional;
/**
* <em>Consider this class private.</em>
@@ -288,6 +289,19 @@ public final class Strings {
return isEmpty(ts) ? null : ts;
}
+ /**
+ * Removes control characters from both ends of this String returning {@code Optional.empty()} if the String is
+ * empty ("") after the trim or if it is {@code null}.
+ * @param str The String to trim.
+ * @return An Optional containing the String.
+ *
+ * @see #trimToNull(String)
+ * @since 2.24.0
+ */
+ public static Optional<String> trimToOptional(final String str) {
+ return Optional.ofNullable(str).map(String::trim).filter(s -> !s.isEmpty());
+ }
+
private Strings() {
// empty
}