Skip to content

Commit

Permalink
[pinpoint-apm#2815] Enhancement StringUtils
Browse files Browse the repository at this point in the history
 - Add isNotEmpty()
 - Move from bootstrap-core module to commons module
   - Deprecate StringUtils of bootstrap-core module
 - Change splitAndTrim() API
 - splitAndTrim() -> tokenizeToStringList()
    - Copy from spring framework
  • Loading branch information
emeroad committed Apr 14, 2017
1 parent 442f9d4 commit 68fccae
Show file tree
Hide file tree
Showing 5 changed files with 381 additions and 252 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,119 +17,45 @@
package com.navercorp.pinpoint.bootstrap.util;


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public final class StringUtils {

private static final int DEFAULT_ABBREVIATE_MAX_WIDTH = 64;

private StringUtils() {
}

public static String defaultString(final String str, final String defaultStr) {
return str == null ? defaultStr : str;
}

public static boolean isEmpty(String string) {
return string == null || string.isEmpty();
}

public static String toString(final Object object) {
if (object == null) {
return "null";
}
return object.toString();
}
/**
* @deprecated Since 1.7.0. Use {@link com.navercorp.pinpoint.common.util.StringUtils}
*/
@Deprecated
public final class StringUtils extends com.navercorp.pinpoint.common.util.StringUtils {

public static List<String> splitAndTrim(String value, String separator) {
if(isEmpty(value)) {
return Collections.emptyList();
}
if (separator == null) {
throw new NullPointerException("separator must not be null");
}
final List<String> result = new ArrayList<String>();
// TODO remove regex 'separator'
final String[] split = value.split(separator);
for (String method : split) {
if (isEmpty(method)) {
continue;
}
method = method.trim();
if (method.isEmpty()) {
continue;
}
result.add(method);
}
return result;
/**
* @deprecated Since 1.7.0. Use {@link com.navercorp.pinpoint.common.util.StringUtils#tokenizeToStringList(String, String)}
*/
@Deprecated
public static List<String> splitAndTrim(final String value, final String separator) {
return tokenizeToStringList(value, separator);
}

/**
* @deprecated Since 1.6.1. Use {@link StringUtils#abbreviate(String)}
* @deprecated Since 1.6.1. Use {@link com.navercorp.pinpoint.common.util.StringUtils#abbreviate(String)}
*/
@Deprecated
public static String drop(final String str) {
return abbreviate(str);
}


public static String abbreviate(final String str) {
return abbreviate(str, DEFAULT_ABBREVIATE_MAX_WIDTH);
}

/**
* @deprecated Since 1.6.1. Use {@link StringUtils#abbreviate(String, int)}
* @deprecated Since 1.6.1. Use {@link com.navercorp.pinpoint.common.util.StringUtils#abbreviate(String, int)}
*/
@Deprecated
public static String drop(final String str, final int maxWidth) {
return abbreviate(str, maxWidth);
}

public static String abbreviate(final String str, final int maxWidth) {
if (str == null) {
return "null";
}
if (maxWidth < 0) {
throw new IllegalArgumentException("negative maxWidth:" + maxWidth);
}
if (str.length() > maxWidth) {
StringBuilder buffer = new StringBuilder(maxWidth + 10);
buffer.append(str, 0, maxWidth);
appendAbbreviateMessage(buffer, str.length());
return buffer.toString();
} else {
return str;
}
}

/**
* @deprecated Since 1.6.1. Use {@link StringUtils#appendAbbreviate(StringBuilder, String, int)}
* @deprecated Since 1.6.1. Use {@link com.navercorp.pinpoint.common.util.StringUtils#appendAbbreviate(StringBuilder, String, int)}
*/
@Deprecated
public static void appendDrop(StringBuilder builder, final String str, final int maxWidth) {
public static void appendDrop(final StringBuilder builder, final String str, final int maxWidth) {
appendAbbreviate(builder, str, maxWidth);
}

public static void appendAbbreviate(StringBuilder builder, final String str, final int maxWidth) {
if (str == null) {
return;
}
if (maxWidth < 0) {
return;
}
if (str.length() > maxWidth) {
builder.append(str, 0, maxWidth);
appendAbbreviateMessage(builder, str.length());
} else {
builder.append(str);
}
}

private static void appendAbbreviateMessage(StringBuilder buffer, int strLength) {
buffer.append("...(");
buffer.append(strLength);
buffer.append(')');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
* @author Sam Brannen
* @since 16 April 2001
*/
public class StringUtils {
class StringUtils {

/**
* Check whether the given {@code CharSequence} contains actual <em>text</em>.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
* Copyright 2017 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.navercorp.pinpoint.common.util;


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;

public class StringUtils {

private static final int DEFAULT_ABBREVIATE_MAX_WIDTH = 64;

private static final String NULL_STRING = "null";

public StringUtils() {
throw new AssertionError();
}

public static String defaultString(final String str, final String defaultStr) {
return str == null ? defaultStr : str;
}

public static boolean isEmpty(final String string) {
return string == null || string.isEmpty();
}

public static boolean isNotEmpty(final String string) {
return string != null && string.length() > 0;
}

public static String toString(final Object object) {
if (object == null) {
return NULL_STRING;
}
return object.toString();
}


public static String abbreviate(final String str) {
return abbreviate(str, DEFAULT_ABBREVIATE_MAX_WIDTH);
}


public static String abbreviate(final String str, final int maxWidth) {
if (str == null) {
return NULL_STRING;
}
if (maxWidth < 0) {
throw new IllegalArgumentException("negative maxWidth:" + maxWidth);
}
if (str.length() > maxWidth) {
StringBuilder buffer = new StringBuilder(maxWidth + 10);
buffer.append(str, 0, maxWidth);
appendAbbreviateMessage(buffer, str.length());
return buffer.toString();
} else {
return str;
}
}

public static void appendAbbreviate(final StringBuilder builder, final String str, final int maxWidth) {
if (str == null) {
return;
}
if (maxWidth < 0) {
return;
}
if (str.length() > maxWidth) {
builder.append(str, 0, maxWidth);
appendAbbreviateMessage(builder, str.length());
} else {
builder.append(str);
}
}

private static void appendAbbreviateMessage(final StringBuilder buffer, final int strLength) {
buffer.append("...(");
buffer.append(strLength);
buffer.append(')');
}


/**
* Copy Spring Framework StringUtils
*
* <p>Tokenize the given String into a String array via a StringTokenizer.
* Trims tokens and omits empty tokens.
* <p>The given delimiters string is supposed to consist of any number of
* delimiter characters. Each of those characters can be used to separate
* tokens. A delimiter is always a single character; for multi-character
* delimiters, consider using {@code delimitedListToStringArray}
* @param str the String to tokenize
* @param delimiters the delimiter characters, assembled as String
* (each of those characters is individually considered as delimiter).
* @return List of the tokens
* @see java.util.StringTokenizer
* @see String#trim()
*/
public static List<String> tokenizeToStringList(final String str, final String delimiters) {
return tokenizeToStringList(str, delimiters, true, true);
}


/**
* Spring Framework StringUtils
*
* <p>Tokenize the given String into a String array via a StringTokenizer.
* Trims tokens and omits empty tokens.
* <p>The given delimiters string is supposed to consist of any number of
* delimiter characters. Each of those characters can be used to separate
* tokens. A delimiter is always a single character; for multi-character
* delimiters, consider using {@code delimitedListToStringArray}
* @param str the String to tokenize
* @param delimiters the delimiter characters, assembled as String
* (each of those characters is individually considered as delimiter).
* @return List of the tokens
* @see java.util.StringTokenizer
* @see String#trim()
*/
public static List<String> tokenizeToStringList(final String str, final String delimiters, final boolean trimTokens, final boolean ignoreEmptyTokens) {

if (isEmpty(str)) {
return Collections.emptyList();
}
StringTokenizer st = new StringTokenizer(str, delimiters);
List<String> tokens = new ArrayList<String>();
while (st.hasMoreTokens()) {
String token = st.nextToken();
if (trimTokens) {
token = token.trim();
}
if (!ignoreEmptyTokens || token.length() > 0) {
tokens.add(token);
}
}
return tokens;
}
}

0 comments on commit 68fccae

Please sign in to comment.