Sort members and clean up comments.
This commit is contained in:
parent
10b13b76ed
commit
1dd511132f
|
@ -16,6 +16,8 @@
|
||||||
*/
|
*/
|
||||||
package org.apache.commons.lang3;
|
package org.apache.commons.lang3;
|
||||||
|
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Operations on {@code CharSet} instances.</p>
|
* <p>Operations on {@code CharSet} instances.</p>
|
||||||
*
|
*
|
||||||
|
@ -30,18 +32,161 @@
|
||||||
public class CharSetUtils {
|
public class CharSetUtils {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>CharSetUtils instances should NOT be constructed in standard programming.
|
* <p>Takes an argument in set-syntax, see evaluateSet,
|
||||||
* Instead, the class should be used as {@code CharSetUtils.evaluateSet(null);}.</p>
|
* and identifies whether any of the characters are present in the specified string.</p>
|
||||||
*
|
*
|
||||||
* <p>This constructor is public to permit tools that require a JavaBean instance
|
* <pre>
|
||||||
* to operate.</p>
|
* CharSetUtils.containsAny(null, *) = false
|
||||||
|
* CharSetUtils.containsAny("", *) = false
|
||||||
|
* CharSetUtils.containsAny(*, null) = false
|
||||||
|
* CharSetUtils.containsAny(*, "") = false
|
||||||
|
* CharSetUtils.containsAny("hello", "k-p") = true
|
||||||
|
* CharSetUtils.containsAny("hello", "a-d") = false
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* @see CharSet#getInstance(java.lang.String...) for set-syntax.
|
||||||
|
* @param str String to look for characters in, may be null
|
||||||
|
* @param set String[] set of characters to identify, may be null
|
||||||
|
* @return whether or not the characters in the set are in the primary string
|
||||||
|
* @since 3.2
|
||||||
*/
|
*/
|
||||||
public CharSetUtils() {
|
public static boolean containsAny(final String str, final String... set) {
|
||||||
super();
|
if (StringUtils.isEmpty(str) || deepEmpty(set)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
final CharSet chars = CharSet.getInstance(set);
|
||||||
|
for (final char c : str.toCharArray()) {
|
||||||
|
if (chars.contains(c)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>Takes an argument in set-syntax, see evaluateSet,
|
||||||
|
* and returns the number of characters present in the specified string.</p>
|
||||||
|
*
|
||||||
|
* <pre>
|
||||||
|
* CharSetUtils.count(null, *) = 0
|
||||||
|
* CharSetUtils.count("", *) = 0
|
||||||
|
* CharSetUtils.count(*, null) = 0
|
||||||
|
* CharSetUtils.count(*, "") = 0
|
||||||
|
* CharSetUtils.count("hello", "k-p") = 3
|
||||||
|
* CharSetUtils.count("hello", "a-e") = 1
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* @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(final String str, final String... set) {
|
||||||
|
if (StringUtils.isEmpty(str) || deepEmpty(set)) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
final CharSet chars = CharSet.getInstance(set);
|
||||||
|
int count = 0;
|
||||||
|
for (final char c : str.toCharArray()) {
|
||||||
|
if (chars.contains(c)) {
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determines whether or not all the Strings in an array are
|
||||||
|
* empty or not.
|
||||||
|
*
|
||||||
|
* @param strings String[] whose elements are being checked for emptiness
|
||||||
|
* @return whether or not the String is empty
|
||||||
|
*/
|
||||||
|
private static boolean deepEmpty(final String[] strings) {
|
||||||
|
if (strings != null) {
|
||||||
|
for (final String s : strings) {
|
||||||
|
if (StringUtils.isNotEmpty(s)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>Takes an argument in set-syntax, see evaluateSet,
|
||||||
|
* and deletes any of characters present in the specified string.</p>
|
||||||
|
*
|
||||||
|
* <pre>
|
||||||
|
* CharSetUtils.delete(null, *) = null
|
||||||
|
* CharSetUtils.delete("", *) = ""
|
||||||
|
* CharSetUtils.delete(*, null) = *
|
||||||
|
* CharSetUtils.delete(*, "") = *
|
||||||
|
* CharSetUtils.delete("hello", "hl") = "eo"
|
||||||
|
* CharSetUtils.delete("hello", "le") = "ho"
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* @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(final String str, final String... set) {
|
||||||
|
if (StringUtils.isEmpty(str) || deepEmpty(set)) {
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
return modify(str, set, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>Takes an argument in set-syntax, see evaluateSet,
|
||||||
|
* and keeps any of characters present in the specified string.</p>
|
||||||
|
*
|
||||||
|
* <pre>
|
||||||
|
* CharSetUtils.keep(null, *) = null
|
||||||
|
* CharSetUtils.keep("", *) = ""
|
||||||
|
* CharSetUtils.keep(*, null) = ""
|
||||||
|
* CharSetUtils.keep(*, "") = ""
|
||||||
|
* CharSetUtils.keep("hello", "hl") = "hll"
|
||||||
|
* CharSetUtils.keep("hello", "le") = "ell"
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* @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(final String str, final String... set) {
|
||||||
|
if (str == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (str.isEmpty() || deepEmpty(set)) {
|
||||||
|
return StringUtils.EMPTY;
|
||||||
|
}
|
||||||
|
return modify(str, set, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implementation of delete and keep
|
||||||
|
*
|
||||||
|
* @param str String to modify characters within
|
||||||
|
* @param set String[] set of characters to modify
|
||||||
|
* @param expect whether to evaluate on match, or non-match
|
||||||
|
* @return the modified String, not null
|
||||||
|
*/
|
||||||
|
private static String modify(final String str, final String[] set, final boolean expect) {
|
||||||
|
final CharSet chars = CharSet.getInstance(set);
|
||||||
|
final StringBuilder buffer = new StringBuilder(str.length());
|
||||||
|
final char[] chrs = str.toCharArray();
|
||||||
|
for (final char chr : chrs) {
|
||||||
|
if (chars.contains(chr) == expect) {
|
||||||
|
buffer.append(chr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return buffer.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Squeeze
|
|
||||||
//-----------------------------------------------------------------------
|
|
||||||
/**
|
/**
|
||||||
* <p>Squeezes any repetitions of a character that is mentioned in the
|
* <p>Squeezes any repetitions of a character that is mentioned in the
|
||||||
* supplied set.</p>
|
* supplied set.</p>
|
||||||
|
@ -93,168 +238,14 @@ public static String squeeze(final String str, final String... set) {
|
||||||
return buffer.toString();
|
return buffer.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
// ContainsAny
|
|
||||||
//-----------------------------------------------------------------------
|
|
||||||
/**
|
/**
|
||||||
* <p>Takes an argument in set-syntax, see evaluateSet,
|
* <p>CharSetUtils instances should NOT be constructed in standard programming.
|
||||||
* and identifies whether any of the characters are present in the specified string.</p>
|
* Instead, the class should be used as {@code CharSetUtils.evaluateSet(null);}.</p>
|
||||||
*
|
*
|
||||||
* <pre>
|
* <p>This constructor is public to permit tools that require a JavaBean instance
|
||||||
* CharSetUtils.containsAny(null, *) = false
|
* to operate.</p>
|
||||||
* CharSetUtils.containsAny("", *) = false
|
|
||||||
* CharSetUtils.containsAny(*, null) = false
|
|
||||||
* CharSetUtils.containsAny(*, "") = false
|
|
||||||
* CharSetUtils.containsAny("hello", "k-p") = true
|
|
||||||
* CharSetUtils.containsAny("hello", "a-d") = false
|
|
||||||
* </pre>
|
|
||||||
*
|
|
||||||
* @see CharSet#getInstance(java.lang.String...) for set-syntax.
|
|
||||||
* @param str String to look for characters in, may be null
|
|
||||||
* @param set String[] set of characters to identify, may be null
|
|
||||||
* @return whether or not the characters in the set are in the primary string
|
|
||||||
* @since 3.2
|
|
||||||
*/
|
*/
|
||||||
public static boolean containsAny(final String str, final String... set) {
|
public CharSetUtils() {
|
||||||
if (StringUtils.isEmpty(str) || deepEmpty(set)) {
|
super();
|
||||||
return false;
|
|
||||||
}
|
|
||||||
final CharSet chars = CharSet.getInstance(set);
|
|
||||||
for (final char c : str.toCharArray()) {
|
|
||||||
if (chars.contains(c)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Count
|
|
||||||
//-----------------------------------------------------------------------
|
|
||||||
/**
|
|
||||||
* <p>Takes an argument in set-syntax, see evaluateSet,
|
|
||||||
* and returns the number of characters present in the specified string.</p>
|
|
||||||
*
|
|
||||||
* <pre>
|
|
||||||
* CharSetUtils.count(null, *) = 0
|
|
||||||
* CharSetUtils.count("", *) = 0
|
|
||||||
* CharSetUtils.count(*, null) = 0
|
|
||||||
* CharSetUtils.count(*, "") = 0
|
|
||||||
* CharSetUtils.count("hello", "k-p") = 3
|
|
||||||
* CharSetUtils.count("hello", "a-e") = 1
|
|
||||||
* </pre>
|
|
||||||
*
|
|
||||||
* @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(final String str, final String... set) {
|
|
||||||
if (StringUtils.isEmpty(str) || deepEmpty(set)) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
final CharSet chars = CharSet.getInstance(set);
|
|
||||||
int count = 0;
|
|
||||||
for (final char c : str.toCharArray()) {
|
|
||||||
if (chars.contains(c)) {
|
|
||||||
count++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return count;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Keep
|
|
||||||
//-----------------------------------------------------------------------
|
|
||||||
/**
|
|
||||||
* <p>Takes an argument in set-syntax, see evaluateSet,
|
|
||||||
* and keeps any of characters present in the specified string.</p>
|
|
||||||
*
|
|
||||||
* <pre>
|
|
||||||
* CharSetUtils.keep(null, *) = null
|
|
||||||
* CharSetUtils.keep("", *) = ""
|
|
||||||
* CharSetUtils.keep(*, null) = ""
|
|
||||||
* CharSetUtils.keep(*, "") = ""
|
|
||||||
* CharSetUtils.keep("hello", "hl") = "hll"
|
|
||||||
* CharSetUtils.keep("hello", "le") = "ell"
|
|
||||||
* </pre>
|
|
||||||
*
|
|
||||||
* @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(final String str, final String... set) {
|
|
||||||
if (str == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
if (str.isEmpty() || deepEmpty(set)) {
|
|
||||||
return StringUtils.EMPTY;
|
|
||||||
}
|
|
||||||
return modify(str, set, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Delete
|
|
||||||
//-----------------------------------------------------------------------
|
|
||||||
/**
|
|
||||||
* <p>Takes an argument in set-syntax, see evaluateSet,
|
|
||||||
* and deletes any of characters present in the specified string.</p>
|
|
||||||
*
|
|
||||||
* <pre>
|
|
||||||
* CharSetUtils.delete(null, *) = null
|
|
||||||
* CharSetUtils.delete("", *) = ""
|
|
||||||
* CharSetUtils.delete(*, null) = *
|
|
||||||
* CharSetUtils.delete(*, "") = *
|
|
||||||
* CharSetUtils.delete("hello", "hl") = "eo"
|
|
||||||
* CharSetUtils.delete("hello", "le") = "ho"
|
|
||||||
* </pre>
|
|
||||||
*
|
|
||||||
* @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(final String str, final String... set) {
|
|
||||||
if (StringUtils.isEmpty(str) || deepEmpty(set)) {
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
return modify(str, set, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------
|
|
||||||
/**
|
|
||||||
* Implementation of delete and keep
|
|
||||||
*
|
|
||||||
* @param str String to modify characters within
|
|
||||||
* @param set String[] set of characters to modify
|
|
||||||
* @param expect whether to evaluate on match, or non-match
|
|
||||||
* @return the modified String, not null
|
|
||||||
*/
|
|
||||||
private static String modify(final String str, final String[] set, final boolean expect) {
|
|
||||||
final CharSet chars = CharSet.getInstance(set);
|
|
||||||
final StringBuilder buffer = new StringBuilder(str.length());
|
|
||||||
final char[] chrs = str.toCharArray();
|
|
||||||
for (final char chr : chrs) {
|
|
||||||
if (chars.contains(chr) == expect) {
|
|
||||||
buffer.append(chr);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return buffer.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Determines whether or not all the Strings in an array are
|
|
||||||
* empty or not.
|
|
||||||
*
|
|
||||||
* @param strings String[] whose elements are being checked for emptiness
|
|
||||||
* @return whether or not the String is empty
|
|
||||||
*/
|
|
||||||
private static boolean deepEmpty(final String[] strings) {
|
|
||||||
if (strings != null) {
|
|
||||||
for (final String s : strings) {
|
|
||||||
if (StringUtils.isNotEmpty(s)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue