From f10738fcb52358aac68c6cbd09019d38f5ae322a Mon Sep 17 00:00:00 2001 From: Matthew Jason Benson Date: Sun, 3 Apr 2011 17:19:31 +0000 Subject: [PATCH] merge CharSetUtils String[] and String method forms to String... git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1088340 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/commons/lang3/CharSetUtils.java | 130 +++--------------- 1 file changed, 20 insertions(+), 110 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/CharSetUtils.java b/src/main/java/org/apache/commons/lang3/CharSetUtils.java index f965352d9..7cec3039d 100644 --- a/src/main/java/org/apache/commons/lang3/CharSetUtils.java +++ b/src/main/java/org/apache/commons/lang3/CharSetUtils.java @@ -64,31 +64,8 @@ public class CharSetUtils { * @param set the character set to use for manipulation, may be null * @return the modified String, {@code null} if null string input */ - public static String squeeze(String str, String set) { - if (StringUtils.isEmpty(str) || StringUtils.isEmpty(set)) { - return str; - } - String[] strs = new String[1]; - strs[0] = set; - return squeeze(str, strs); - } - - /** - *

Squeezes any repetitions of a character that is mentioned in the - * supplied set.

- * - *

An example is:

- * - * - * @see CharSet#getInstance(java.lang.String) for set-syntax. - * @param str the string to squeeze, may be null - * @param set the character set to use for manipulation, may be null - * @return the modified String, {@code null} if null string input - */ - public static String squeeze(String str, String[] set) { - if (StringUtils.isEmpty(str) || ArrayUtils.isEmpty(set)) { + public static String squeeze(String str, String... set) { + if (StringUtils.isEmpty(str) || deepEmpty(set)) { return str; } CharSet chars = CharSet.getInstance(set); @@ -127,42 +104,17 @@ public class CharSetUtils { * * @see CharSet#getInstance(java.lang.String) for set-syntax. * @param str String to count characters in, may be null - * @param set String set of characters to count, may be null - * @return the character count, zero if null string input - */ - public static int count(String str, String set) { - if (StringUtils.isEmpty(str) || StringUtils.isEmpty(set)) { - return 0; - } - String[] strs = new String[1]; - strs[0] = set; - return count(str, strs); - } - - /** - *

Takes an argument in set-syntax, see evaluateSet, - * and returns the number of characters present in the specified string.

- * - *

An example would be:

- * - * - * @see CharSet#getInstance(java.lang.String) for set-syntax. - * @param str String to count characters in, may be null * @param set String[] set of characters to count, may be null * @return the character count, zero if null string input */ - public static int count(String str, String[] set) { - if (StringUtils.isEmpty(str) || ArrayUtils.isEmpty(set)) { + public static int count(String str, String... set) { + if (StringUtils.isEmpty(str) || deepEmpty(set)) { return 0; } CharSet chars = CharSet.getInstance(set); int count = 0; - char[] chrs = str.toCharArray(); - int sz = chrs.length; - for(int i=0; iTakes an argument in set-syntax, see evaluateSet, - * and keeps any of characters present in the specified string.

- * - *

An example would be:

- * - * - * @see CharSet#getInstance(java.lang.String) for set-syntax. - * @param str String to keep characters from, may be null * @param set String[] set of characters to keep, may be null * @return the modified String, {@code null} if null string input * @since 2.0 */ - public static String keep(String str, String[] set) { + public static String keep(String str, String... set) { if (str == null) { return null; } - if (str.length() == 0 || ArrayUtils.isEmpty(set)) { + if (str.length() == 0 || deepEmpty(set)) { return ""; } return modify(str, set, true); @@ -245,35 +169,11 @@ public class CharSetUtils { * * @see CharSet#getInstance(java.lang.String) for set-syntax. * @param str String to delete characters from, may be null - * @param set String set of characters to delete, may be null - * @return the modified String, {@code null} if null string input - */ - public static String delete(String str, String set) { - if (StringUtils.isEmpty(str) || StringUtils.isEmpty(set)) { - return str; - } - String[] strs = new String[1]; - strs[0] = set; - return delete(str, strs); - } - - /** - *

Takes an argument in set-syntax, see evaluateSet, - * and deletes any of characters present in the specified string.

- * - *

An example would be:

- * - * - * @see CharSet#getInstance(java.lang.String) for set-syntax. - * @param str String to delete characters from, may be null * @param set String[] set of characters to delete, may be null * @return the modified String, {@code null} if null string input */ - public static String delete(String str, String[] set) { - if (StringUtils.isEmpty(str) || ArrayUtils.isEmpty(set)) { + public static String delete(String str, String... set) { + if (StringUtils.isEmpty(str) || deepEmpty(set)) { return str; } return modify(str, set, false); @@ -301,4 +201,14 @@ public class CharSetUtils { return buffer.toString(); } + private static boolean deepEmpty(String[] strings) { + if (strings != null) { + for (String s : strings) { + if (StringUtils.isNotEmpty(s)) { + return false; + } + } + } + return true; + } }