Author: mattsicker
Date: Mon May 26 02:34:51 2014
New Revision: 1597502
URL: http://svn.apache.org/r1597502
Log:
Add trimToNull to Strings utility class.
- Made class final.
- Added private constructor.
- Fixed javadoc to refer to correct class.
- Removed @since tags from javadoc (wrong version references).
Modified:
logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/util/Strings.java
Modified:
logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/util/Strings.java
URL:
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/util/Strings.java?rev=1597502&r1=1597501&r2=1597502&view=diff
==============================================================================
---
logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/util/Strings.java
(original)
+++
logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/util/Strings.java
Mon May 26 02:34:51 2014
@@ -3,13 +3,16 @@ package org.apache.logging.log4j.util;
/**
* <em>Consider this class private.</em>
*/
-public class Strings {
+public final class Strings {
/**
* The empty string.
*/
public static final String EMPTY = "";
+ private Strings() {
+ }
+
/**
* <p>Checks if a CharSequence is empty ("") or null.</p>
*
@@ -38,20 +41,47 @@ public class Strings {
* <p>Checks if a CharSequence is not empty ("") and not null.</p>
*
* <pre>
- * StringUtils.isNotEmpty(null) = false
- * StringUtils.isNotEmpty("") = false
- * StringUtils.isNotEmpty(" ") = true
- * StringUtils.isNotEmpty("bob") = true
- * StringUtils.isNotEmpty(" bob ") = true
+ * Strings.isNotEmpty(null) = false
+ * Strings.isNotEmpty("") = false
+ * Strings.isNotEmpty(" ") = true
+ * Strings.isNotEmpty("bob") = true
+ * Strings.isNotEmpty(" bob ") = true
* </pre>
*
* <p>Copied from Apache Commons Lang
org.apache.commons.lang3.StringUtils.isNotEmpty(CharSequence)</p>
*
* @param cs the CharSequence to check, may be null
* @return {@code true} if the CharSequence is not empty and not null
- * @since 3.0 Changed signature from isNotEmpty(String) to
isNotEmpty(CharSequence)
*/
public static boolean isNotEmpty(final CharSequence cs) {
return !isEmpty(cs);
}
+
+ /**
+ * <p>Removes control characters (char <= 32) from both
+ * ends of this String returning {@code null} if the String is
+ * empty ("") after the trim or if it is {@code null}.
+ *
+ * <p>The String is trimmed using {@link String#trim()}.
+ * Trim removes start and end characters <= 32.</p>
+ *
+ * <pre>
+ * Strings.trimToNull(null) = null
+ * Strings.trimToNull("") = null
+ * Strings.trimToNull(" ") = null
+ * Strings.trimToNull("abc") = "abc"
+ * Strings.trimToNull(" abc ") = "abc"
+ * </pre>
+ *
+ * <p>Copied from Apache Commons Lang
org.apache.commons.lang3.StringUtils.trimToNull(String)</p>
+ *
+ * @param str the String to be trimmed, may be null
+ * @return the trimmed String,
+ * {@code null} if only chars <= 32, empty or null String input
+ */
+ public static String trimToNull(final String str) {
+ final String ts = str == null ? null : str.trim();
+ return isEmpty(ts) ? null : ts;
+ }
+
}