From 8950304f038a0113d1b4a2f5cd9d617ff354c54f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E7=BF=8A=20SionYang?= <263976490@qq.com> Date: Tue, 10 Nov 2020 18:21:59 +0800 Subject: [PATCH] Revert "[ISSUE #3102] Create utils class to nacos-common (#4165)" (#4191) This reverts commit cf92d19d0350a61d969a1cfa5dbf45333e00d452. --- .../alibaba/nacos/common/utils/ArrayUtil.java | 175 ------ .../nacos/common/utils/BooleanUtil.java | 90 --- .../nacos/common/utils/ConvertUtils.java | 75 +-- .../nacos/common/utils/NumberUtil.java | 177 ------ .../nacos/common/utils/RandomUtil.java | 104 ---- .../nacos/common/utils/StringUtils.java | 562 +----------------- 6 files changed, 6 insertions(+), 1177 deletions(-) delete mode 100644 common/src/main/java/com/alibaba/nacos/common/utils/ArrayUtil.java delete mode 100644 common/src/main/java/com/alibaba/nacos/common/utils/BooleanUtil.java delete mode 100644 common/src/main/java/com/alibaba/nacos/common/utils/NumberUtil.java delete mode 100644 common/src/main/java/com/alibaba/nacos/common/utils/RandomUtil.java diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/ArrayUtil.java b/common/src/main/java/com/alibaba/nacos/common/utils/ArrayUtil.java deleted file mode 100644 index 9ee8ad42b..000000000 --- a/common/src/main/java/com/alibaba/nacos/common/utils/ArrayUtil.java +++ /dev/null @@ -1,175 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * 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.alibaba.nacos.common.utils; - -import java.lang.reflect.Array; - -/** - * nacos array util. - * - * @author wujian - */ -public class ArrayUtil { - - /** - * The index value when an element is not found in a list or array: {@code -1}. This value is returned by methods in - * this class and can also be used in comparisons with values returned by various method from {@link - * java.util.List}. - */ - public static final int INDEX_NOT_FOUND = -1; - - /** - * An empty immutable {@code boolean} array. - */ - public static final boolean[] EMPTY_BOOLEAN_ARRAY = new boolean[0]; - - /** - * An empty immutable {@code String} array. - */ - public static final String[] EMPTY_STRING_ARRAY = new String[0]; - - /** - * Checks if an array of Objects is empty or {@code null}. - * - * @param array the array to test - * @return {@code true} if the array is empty or {@code null} - */ - public static boolean isEmpty(final Object[] array) { - return getLength(array) == 0; - } - - /** - * Returns the length of the specified array. This method can deal with {@code Object} arrays and with primitive - * arrays. - * - *
If the input array is {@code null}, {@code 0} is returned. - * - *
- * ArrayUtils.getLength(null) = 0 - * ArrayUtils.getLength([]) = 0 - * ArrayUtils.getLength([null]) = 1 - * ArrayUtils.getLength([true, false]) = 2 - * ArrayUtils.getLength([1, 2, 3]) = 3 - * ArrayUtils.getLength(["a", "b", "c"]) = 3 - *- * - * @param array the array to retrieve the length from, may be null - * @return The length of the array, or {@code 0} if the array is {@code null} - * @throws IllegalArgumentException if the object argument is not an array. - */ - public static int getLength(final Object array) { - if (array == null) { - return 0; - } - return Array.getLength(array); - } - - /** - * Checks if the object is in the given array. - * - *
The method returns {@code false} if a {@code null} array is passed in. - * - * @param array the array to search through - * @param objectToFind the object to find - * @return {@code true} if the array contains the object - */ - public static boolean contains(final Object[] array, final Object objectToFind) { - return indexOf(array, objectToFind) != INDEX_NOT_FOUND; - } - - // Boolean array converters - // ---------------------------------------------------------------------- - - /** - *
Converts an array of object Booleans to primitives.
- * - *This method returns {@code null} for a {@code null} input array.
- * - * @param array a {@code Boolean} array, may be {@code null} - * @return a {@code boolean} array, {@code null} if null array input - * @throws NullPointerException if array content is {@code null} - */ - public static boolean[] toPrimitive(final Boolean[] array) { - if (array == null) { - return null; - } else if (array.length == 0) { - return EMPTY_BOOLEAN_ARRAY; - } - final boolean[] result = new boolean[array.length]; - for (int i = 0; i < array.length; i++) { - result[i] = array[i].booleanValue(); - } - return result; - } - - // IndexOf search - // ---------------------------------------------------------------------- - - // Object IndexOf - //----------------------------------------------------------------------- - - /** - * Finds the index of the given object in the array. - * - *This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. - * - * @param array the array to search through for the object, may be {@code null} - * @param objectToFind the object to find, may be {@code null} - * @return the index of the object within the array, {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code - * null} array input - */ - public static int indexOf(final Object[] array, final Object objectToFind) { - return indexOf(array, objectToFind, 0); - } - - /** - * Finds the index of the given object in the array starting at the given index. - * - *
This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. - * - *
A negative startIndex is treated as zero. A startIndex larger than the array - * length will return {@link #INDEX_NOT_FOUND} ({@code -1}). - * - * @param array the array to search through for the object, may be {@code null} - * @param objectToFind the object to find, may be {@code null} - * @param startIndex the index to start searching at - * @return the index of the object within the array starting at the index, {@link #INDEX_NOT_FOUND} ({@code -1}) if - * not found or {@code null} array input - */ - public static int indexOf(final Object[] array, final Object objectToFind, int startIndex) { - if (array == null) { - return INDEX_NOT_FOUND; - } - if (startIndex < 0) { - startIndex = 0; - } - if (objectToFind == null) { - for (int i = startIndex; i < array.length; i++) { - if (array[i] == null) { - return i; - } - } - } else { - for (int i = startIndex; i < array.length; i++) { - if (objectToFind.equals(array[i])) { - return i; - } - } - } - return INDEX_NOT_FOUND; - } -} diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/BooleanUtil.java b/common/src/main/java/com/alibaba/nacos/common/utils/BooleanUtil.java deleted file mode 100644 index 6b06148a0..000000000 --- a/common/src/main/java/com/alibaba/nacos/common/utils/BooleanUtil.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * 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.alibaba.nacos.common.utils; - -/** - * nacos boolean util. - * - * @author wujian - */ -public class BooleanUtil { - - /** - *
Performs an and on a set of booleans.
- * - *- * BooleanUtils.and(true, true) = true - * BooleanUtils.and(false, false) = false - * BooleanUtils.and(true, false) = false - * BooleanUtils.and(true, true, false) = false - * BooleanUtils.and(true, true, true) = true - *- * - * @param array an array of {@code boolean}s - * @return {@code true} if the and is successful. - * @throws IllegalArgumentException if {@code array} is {@code null} - * @throws IllegalArgumentException if {@code array} is empty. - */ - public static boolean and(final boolean... array) { - // Validates input - if (array == null) { - throw new IllegalArgumentException("The Array must not be null"); - } - if (array.length == 0) { - throw new IllegalArgumentException("Array is empty"); - } - for (final boolean element : array) { - if (!element) { - return false; - } - } - return true; - } - - /** - *
Performs an and on an array of Booleans.
- * - *- * BooleanUtils.and(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE - * BooleanUtils.and(Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE - * BooleanUtils.and(Boolean.TRUE, Boolean.FALSE) = Boolean.FALSE - * BooleanUtils.and(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE - * BooleanUtils.and(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE - * BooleanUtils.and(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE - *- * - * @param array an array of {@code Boolean}s - * @return {@code true} if the and is successful. - * @throws IllegalArgumentException if {@code array} is {@code null} - * @throws IllegalArgumentException if {@code array} is empty. - * @throws IllegalArgumentException if {@code array} contains a {@code null} - */ - public static Boolean and(final Boolean... array) { - if (array == null) { - throw new IllegalArgumentException("The Array must not be null"); - } - if (array.length == 0) { - throw new IllegalArgumentException("Array is empty"); - } - try { - final boolean[] primitive = ArrayUtil.toPrimitive(array); - return and(primitive) ? Boolean.TRUE : Boolean.FALSE; - } catch (final NullPointerException ex) { - throw new IllegalArgumentException("The array must not contain any null elements"); - } - } -} diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/ConvertUtils.java b/common/src/main/java/com/alibaba/nacos/common/utils/ConvertUtils.java index a3a547e08..50670e411 100644 --- a/common/src/main/java/com/alibaba/nacos/common/utils/ConvertUtils.java +++ b/common/src/main/java/com/alibaba/nacos/common/utils/ConvertUtils.java @@ -245,79 +245,6 @@ public final class ConvertUtils { } } - /** - *
Convert a String
to a double
, returning a
- * default value if the conversion fails.
If the string str
is null
, the default
- * value is returned.
- * NumberUtils.toDouble(null, 1.1d) = 1.1d - * NumberUtils.toDouble("", 1.1d) = 1.1d - * NumberUtils.toDouble("1.5", 0.0d) = 1.5d - *- * - * @param str the string to convert, may be
null
- * @param defaultValue the default value
- * @return the double represented by the string, or defaultValue if conversion fails
- */
- public static double toDouble(final String str, final double defaultValue) {
- if (str == null) {
- return defaultValue;
- }
- try {
- return Double.parseDouble(str);
- } catch (final NumberFormatException nfe) {
- return defaultValue;
- }
- }
+ // end
- /**
- * Convert a String
to a float
, returning
- * 0.0f
if the conversion fails.
If the string str
is null
,
- * 0.0f
is returned.
- * NumberUtils.toFloat(null) = 0.0f - * NumberUtils.toFloat("") = 0.0f - * NumberUtils.toFloat("1.5") = 1.5f - *- * - * @param str the string to convert, may be
null
- * @return the float represented by the string, or 0.0f
if conversion fails
- */
- public static float toFloat(final String str) {
- return toFloat(str, 0.0f);
- }
-
- /**
- * Convert a String
to a float
, returning a
- * default value if the conversion fails.
If the string str
is null
, the default
- * value is returned.
- * NumberUtils.toFloat(null, 1.1f) = 1.0f - * NumberUtils.toFloat("", 1.1f) = 1.1f - * NumberUtils.toFloat("1.5", 0.0f) = 1.5f - *- * - * @param str the string to convert, may be
null
- * @param defaultValue the default value
- * @return the float represented by the string, or defaultValue if conversion fails
- */
- public static float toFloat(final String str, final float defaultValue) {
- if (str == null) {
- return defaultValue;
- }
- try {
- return Float.parseFloat(str);
- } catch (final NumberFormatException nfe) {
- return defaultValue;
- }
- }
}
diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/NumberUtil.java b/common/src/main/java/com/alibaba/nacos/common/utils/NumberUtil.java
deleted file mode 100644
index 172549cee..000000000
--- a/common/src/main/java/com/alibaba/nacos/common/utils/NumberUtil.java
+++ /dev/null
@@ -1,177 +0,0 @@
-/*
- * Copyright 1999-2018 Alibaba Group Holding Ltd.
- *
- * 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.alibaba.nacos.common.utils;
-
-/**
- * nacos Number util.
- *
- * @author wujian
- */
-public class NumberUtil {
-
- /**
- * Checks whether the String a valid Java number.
- * - *Valid numbers include hexadecimal marked with the 0x
or
- * 0X
qualifier, octal numbers, scientific notation and
- * numbers marked with a type qualifier (e.g. 123L).
Non-hexadecimal strings beginning with a leading zero are
- * treated as octal values. Thus the string 09
will return
- * false
, since 9
is not a valid octal value.
- * However, numbers beginning with {@code 0.} are treated as decimal.
null
and empty/blank {@code String} will return
- * false
.
String
to check
- * @return true
if the string is a correctly formatted number
- */
- public static boolean isNumber(final String str) {
- return isCreatable(str);
- }
-
- /**
- * Checks whether the String a valid Java number.
- * - *Valid numbers include hexadecimal marked with the 0x
or
- * 0X
qualifier, octal numbers, scientific notation and
- * numbers marked with a type qualifier (e.g. 123L).
Non-hexadecimal strings beginning with a leading zero are
- * treated as octal values. Thus the string 09
will return
- * false
, since 9
is not a valid octal value.
- * However, numbers beginning with {@code 0.} are treated as decimal.
null
and empty/blank {@code String} will return
- * false
.
String
to check
- * @return true
if the string is a correctly formatted number
- */
- @SuppressWarnings({"PMD.UndefineMagicConstantRule", "PMD.AvoidComplexConditionRule"})
- public static boolean isCreatable(final String str) {
- if (StringUtils.isEmpty(str)) {
- return false;
- }
- final char[] chars = str.toCharArray();
- int sz = chars.length;
- boolean hasExp = false;
- boolean hasDecPoint = false;
- boolean allowSigns = false;
- boolean foundDigit = false;
- // deal with any possible sign up front
- final int start = chars[0] == '-' || chars[0] == '+' ? 1 : 0;
- // leading 0, skip if is a decimal number
- if (sz > start + 1 && chars[start] == '0' && !StringUtils.contains(str, '.')) {
- // leading 0x/0X
- if (chars[start + 1] == 'x' || chars[start + 1] == 'X') {
- int i = start + 2;
- if (i == sz) {
- // str == "0x"
- return false;
- }
- // checking hex (it can't be anything else)
- for (; i < chars.length; i++) {
- if ((chars[i] < '0' || chars[i] > '9') && (chars[i] < 'a' || chars[i] > 'f') && (chars[i] < 'A'
- || chars[i] > 'F')) {
- return false;
- }
- }
- return true;
- } else if (Character.isDigit(chars[start + 1])) {
- // leading 0, but not hex, must be octal
- int i = start + 1;
- for (; i < chars.length; i++) {
- if (chars[i] < '0' || chars[i] > '7') {
- return false;
- }
- }
- return true;
- }
- }
- sz--; // don't want to loop to the last char, check it afterwords
- // for type qualifiers
- int i = start;
- // loop to the next to last char or to the last char if we need another digit to
- // make a valid number (e.g. chars[0..5] = "1234E")
- while (i < sz || i < sz + 1 && allowSigns && !foundDigit) {
- if (chars[i] >= '0' && chars[i] <= '9') {
- foundDigit = true;
- allowSigns = false;
-
- } else if (chars[i] == '.') {
- if (hasDecPoint || hasExp) {
- // two decimal points or dec in exponent
- return false;
- }
- hasDecPoint = true;
- } else if (chars[i] == 'e' || chars[i] == 'E') {
- // we've already taken care of hex.
- if (hasExp) {
- // two E's
- return false;
- }
- if (!foundDigit) {
- return false;
- }
- hasExp = true;
- allowSigns = true;
- } else if (chars[i] == '+' || chars[i] == '-') {
- if (!allowSigns) {
- return false;
- }
- allowSigns = false;
- // we need a digit after the E
- foundDigit = false;
- } else {
- return false;
- }
- i++;
- }
- if (i < chars.length) {
- if (chars[i] >= '0' && chars[i] <= '9') {
- // no type qualifier, OK
- return true;
- }
- if (chars[i] == 'e' || chars[i] == 'E') {
- // can't have an E at the last byte
- return false;
- }
- if (chars[i] == '.') {
- if (hasDecPoint || hasExp) {
- // two decimal points or dec in exponent
- return false;
- }
- // single trailing decimal point after non-exponent is ok
- return foundDigit;
- }
- if (!allowSigns && (chars[i] == 'd' || chars[i] == 'D' || chars[i] == 'f' || chars[i] == 'F')) {
- return foundDigit;
- }
- if (chars[i] == 'l' || chars[i] == 'L') {
- // not allowing L with an exponent or decimal point
- return foundDigit && !hasExp && !hasDecPoint;
- }
- // last character is illegal
- return false;
- }
- // allowSigns is true iff the val ends in 'E'
- // found digit it to make sure weird stuff like '.' and '1E-' doesn't pass
- return !allowSigns && foundDigit;
- }
-}
diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/RandomUtil.java b/common/src/main/java/com/alibaba/nacos/common/utils/RandomUtil.java
deleted file mode 100644
index 486a8e89d..000000000
--- a/common/src/main/java/com/alibaba/nacos/common/utils/RandomUtil.java
+++ /dev/null
@@ -1,104 +0,0 @@
-/*
- * Copyright 1999-2018 Alibaba Group Holding Ltd.
- *
- * 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.alibaba.nacos.common.utils;
-
-import com.google.common.base.Preconditions;
-
-import java.util.Random;
-
-/**
- * nacos random util.
- *
- * @author wujian
- */
-public class RandomUtil {
-
- /**
- * Random object used by random method. This has to be not local to the random method so as to not return the same
- * value in the same millisecond.
- */
- private static final Random RANDOM = new Random();
-
- /**
- * - * Returns a random long within the specified range. - *
- * - * @param startInclusive the smallest value that can be returned, must be non-negative - * @param endExclusive the upper bound (not included) - * @return the random long - * @throws IllegalArgumentException if {@code startInclusive > endExclusive} or if {@code startInclusive} is - * negative - */ - public static long nextLong(final long startInclusive, final long endExclusive) { - Preconditions - .checkArgument(endExclusive >= startInclusive, "Start value must be smaller or equal to end value."); - Preconditions.checkArgument(startInclusive >= 0, "Both range values must be non-negative."); - - if (startInclusive == endExclusive) { - return startInclusive; - } - - return (long) nextDouble(startInclusive, endExclusive); - } - - /** - *- * Returns a random double within the specified range. - *
- * - * @param startInclusive the smallest value that can be returned, must be non-negative - * @param endInclusive the upper bound (included) - * @return the random double - * @throws IllegalArgumentException if {@code startInclusive > endInclusive} or if {@code startInclusive} is - * negative - */ - public static double nextDouble(final double startInclusive, final double endInclusive) { - Preconditions - .checkArgument(endInclusive >= startInclusive, "Start value must be smaller or equal to end value."); - Preconditions.checkArgument(startInclusive >= 0, "Both range values must be non-negative."); - - if (startInclusive == endInclusive) { - return startInclusive; - } - - return startInclusive + ((endInclusive - startInclusive) * RANDOM.nextDouble()); - } - - /** - *- * Returns a random integer within the specified range. - *
- * - * @param startInclusive the smallest value that can be returned, must be non-negative - * @param endExclusive the upper bound (not included) - * @return the random integer - * @throws IllegalArgumentException if {@code startInclusive > endExclusive} or if {@code startInclusive} is - * negative - */ - public static int nextInt(final int startInclusive, final int endExclusive) { - Preconditions - .checkArgument(endExclusive >= startInclusive, "Start value must be smaller or equal to end value."); - Preconditions.checkArgument(startInclusive >= 0, "Both range values must be non-negative."); - - if (startInclusive == endExclusive) { - return startInclusive; - } - - return startInclusive + RANDOM.nextInt(endExclusive - startInclusive); - } -} diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/StringUtils.java b/common/src/main/java/com/alibaba/nacos/common/utils/StringUtils.java index 2b8df6548..de498ad6c 100644 --- a/common/src/main/java/com/alibaba/nacos/common/utils/StringUtils.java +++ b/common/src/main/java/com/alibaba/nacos/common/utils/StringUtils.java @@ -22,9 +22,7 @@ import java.io.IOException; import java.io.StringWriter; import java.io.Writer; import java.nio.charset.Charset; -import java.util.ArrayList; import java.util.Collection; -import java.util.List; import java.util.Locale; /** @@ -42,8 +40,6 @@ public class StringUtils { public static final String EMPTY = ""; - public static final String LF = "\n"; - public static String newStringForUtf8(byte[] bytes) { return new String(bytes, Charset.forName(Constants.ENCODE)); } @@ -67,35 +63,6 @@ public class StringUtils { return true; } - /** - *Checks if a CharSequence is empty (""), null or whitespace only.
- * - *Whitespace is defined by {@link Character#isWhitespace(char)}.
- * - *- * StringUtils.isBlank(null) = true - * StringUtils.isBlank("") = true - * StringUtils.isBlank(" ") = true - * StringUtils.isBlank("bob") = false - * StringUtils.isBlank(" bob ") = false - *- * - * @param cs the CharSequence to check, may be null - * @return {@code true} if the CharSequence is null, empty or whitespace only - */ - public static boolean isBlank(final CharSequence cs) { - int strLen; - if (cs == null || (strLen = cs.length()) == 0) { - return true; - } - for (int i = 0; i < strLen; i++) { - if (!Character.isWhitespace(cs.charAt(i))) { - return false; - } - } - return true; - } - /** * Judge whether all strings are blank. * @@ -123,10 +90,6 @@ public class StringUtils { return str == null || str.length() == 0; } - public static boolean isEmpty(CharSequence cs) { - return cs == null || cs.length() == 0; - } - public static String defaultIfEmpty(String str, String defaultStr) { return StringUtils.isEmpty(str) ? defaultStr : str; } @@ -313,6 +276,8 @@ public class StringUtils { * @param searchStr the CharSequence to find, may be null * @return true if the CharSequence contains the search CharSequence irrespective of case or false if not or {@code * null} string input + * @since 3.0 Changed signature from containsIgnoreCase(String, String) to containsIgnoreCase(CharSequence, + * CharSequence) */ public static boolean containsIgnoreCase(final CharSequence str, final CharSequence searchStr) { if (str == null || searchStr == null) { @@ -388,6 +353,8 @@ public class StringUtils { * @param str1 the first CharSequence, may be null * @param str2 the second CharSequence, may be null * @return {@code true} if the CharSequence are equal, case insensitive, or both {@code null} + * @since 3.0 Changed signature from equalsIgnoreCase(String, String) to equalsIgnoreCase(CharSequence, + * CharSequence) */ public static boolean equalsIgnoreCase(final CharSequence str1, final CharSequence str2) { if (str1 == null || str2 == null) { @@ -445,526 +412,7 @@ public class StringUtils { return true; } - @SuppressWarnings("PMD.UndefineMagicConstantRule") - static int indexOf(CharSequence cs, int searchChar, int start) { - if (cs instanceof String) { - return ((String) cs).indexOf(searchChar, start); - } else { - int sz = cs.length(); - if (start < 0) { - start = 0; - } - - if (searchChar < 65536) { - for (int i = start; i < sz; ++i) { - if (cs.charAt(i) == searchChar) { - return i; - } - } - } - - if (searchChar <= 1114111) { - char[] chars = Character.toChars(searchChar); - - for (int i = start; i < sz - 1; ++i) { - char high = cs.charAt(i); - char low = cs.charAt(i + 1); - if (high == chars[0] && low == chars[1]) { - return i; - } - } - } - - return -1; - } - } - - static int indexOf(CharSequence cs, CharSequence searchChar, int start) { - return cs.toString().indexOf(searchChar.toString(), start); - } } - /** - *
Checks if CharSequence contains a search character, handling {@code null}. - * This method uses {@link String#indexOf(int)} if possible.
- * - *A {@code null} or empty ("") CharSequence will return {@code false}.
- * - *- * StringUtils.contains(null, *) = false - * StringUtils.contains("", *) = false - * StringUtils.contains("abc", 'a') = true - * StringUtils.contains("abc", 'z') = false - *- * - * @param seq the CharSequence to check, may be null - * @param searchChar the character to find - * @return true if the CharSequence contains the search character, false if not or {@code null} string input - */ - public static boolean contains(final CharSequence seq, final int searchChar) { - if (isEmpty(seq)) { - return false; - } - return CharSequenceUtils.indexOf(seq, searchChar, 0) >= 0; - } - - /** - *
Checks if CharSequence contains a search CharSequence, handling {@code null}. - * This method uses {@link String#indexOf(String)} if possible.
- * - *A {@code null} CharSequence will return {@code false}.
- * - *- * StringUtils.contains(null, *) = false - * StringUtils.contains(*, null) = false - * StringUtils.contains("", "") = true - * StringUtils.contains("abc", "") = true - * StringUtils.contains("abc", "a") = true - * StringUtils.contains("abc", "z") = false - *- * - * @param seq the CharSequence to check, may be null - * @param searchSeq the CharSequence to find, may be null - * @return true if the CharSequence contains the search CharSequence, false if not or {@code null} string input - */ - public static boolean contains(final CharSequence seq, final CharSequence searchSeq) { - if (seq == null || searchSeq == null) { - return false; - } - return CharSequenceUtils.indexOf(seq, searchSeq, 0) >= 0; - } - - /** - *
Checks if none of the CharSequences are empty (""), null or whitespace only.
- * - *Whitespace is defined by {@link Character#isWhitespace(char)}.
- * - *- * StringUtils.isNoneBlank((String) null) = false - * StringUtils.isNoneBlank((String[]) null) = true - * StringUtils.isNoneBlank(null, "foo") = false - * StringUtils.isNoneBlank(null, null) = false - * StringUtils.isNoneBlank("", "bar") = false - * StringUtils.isNoneBlank("bob", "") = false - * StringUtils.isNoneBlank(" bob ", null) = false - * StringUtils.isNoneBlank(" ", "bar") = false - * StringUtils.isNoneBlank(new String[] {}) = true - * StringUtils.isNoneBlank(new String[]{""}) = false - * StringUtils.isNoneBlank("foo", "bar") = true - *- * - * @param css the CharSequences to check, may be null or empty - * @return {@code true} if none of the CharSequences are empty or null or whitespace only - */ - public static boolean isNoneBlank(final CharSequence... css) { - return !isAnyBlank(css); - } - - /** - *
Checks if any of the CharSequences are empty ("") or null or whitespace only.
- * - *Whitespace is defined by {@link Character#isWhitespace(char)}.
- * - *- * StringUtils.isAnyBlank((String) null) = true - * StringUtils.isAnyBlank((String[]) null) = false - * StringUtils.isAnyBlank(null, "foo") = true - * StringUtils.isAnyBlank(null, null) = true - * StringUtils.isAnyBlank("", "bar") = true - * StringUtils.isAnyBlank("bob", "") = true - * StringUtils.isAnyBlank(" bob ", null) = true - * StringUtils.isAnyBlank(" ", "bar") = true - * StringUtils.isAnyBlank(new String[] {}) = false - * StringUtils.isAnyBlank(new String[]{""}) = true - * StringUtils.isAnyBlank("foo", "bar") = false - *- * - * @param css the CharSequences to check, may be null or empty - * @return {@code true} if any of the CharSequences are empty or null or whitespace only - */ - public static boolean isAnyBlank(final CharSequence... css) { - if (ArrayUtil.isEmpty(css)) { - return false; - } - for (final CharSequence cs : css) { - if (isBlank(cs)) { - return true; - } - } - return false; - } - - /** - *
Check if a CharSequence starts with a specified prefix.
- * - *{@code null}s are handled without exceptions. Two {@code null} - * references are considered to be equal. The comparison is case sensitive.
- * - *- * StringUtils.startsWith(null, null) = true - * StringUtils.startsWith(null, "abc") = false - * StringUtils.startsWith("abcdef", null) = false - * StringUtils.startsWith("abcdef", "abc") = true - * StringUtils.startsWith("ABCDEF", "abc") = false - *- * - * @param str the CharSequence to check, may be null - * @param prefix the prefix to find, may be null - * @return {@code true} if the CharSequence starts with the prefix, case sensitive, or both {@code null} - */ - public static boolean startsWith(final CharSequence str, final CharSequence prefix) { - return startsWith(str, prefix, false); - } - - /** - *
Check if a CharSequence starts with a specified prefix (optionally case insensitive).
- * - * @param str the CharSequence to check, may be null - * @param prefix the prefix to find, may be null - * @param ignoreCase indicates whether the compare should ignore case (case insensitive) or not. - * @return {@code true} if the CharSequence starts with the prefix or both {@code null} - * @see String#startsWith(String) - */ - private static boolean startsWith(final CharSequence str, final CharSequence prefix, final boolean ignoreCase) { - if (str == null || prefix == null) { - return str == prefix; - } - if (prefix.length() > str.length()) { - return false; - } - return CharSequenceUtils.regionMatches(str, ignoreCase, 0, prefix, 0, prefix.length()); - } - - /** - *Deletes all whitespaces from a String as defined by - * {@link Character#isWhitespace(char)}.
- * - *- * StringUtils.deleteWhitespace(null) = null - * StringUtils.deleteWhitespace("") = "" - * StringUtils.deleteWhitespace("abc") = "abc" - * StringUtils.deleteWhitespace(" ab c ") = "abc" - *- * - * @param str the String to delete whitespace from, may be null - * @return the String without whitespaces, {@code null} if null String input - */ - public static String deleteWhitespace(final String str) { - if (isEmpty(str)) { - return str; - } - final int sz = str.length(); - final char[] chs = new char[sz]; - int count = 0; - for (int i = 0; i < sz; i++) { - if (!Character.isWhitespace(str.charAt(i))) { - chs[count++] = str.charAt(i); - } - } - if (count == sz) { - return str; - } - return new String(chs, 0, count); - } - - /** - *
Case insensitive check if a CharSequence starts with a specified prefix.
- * - *{@code null}s are handled without exceptions. Two {@code null} - * references are considered to be equal. The comparison is case insensitive.
- * - *- * StringUtils.startsWithIgnoreCase(null, null) = true - * StringUtils.startsWithIgnoreCase(null, "abc") = false - * StringUtils.startsWithIgnoreCase("abcdef", null) = false - * StringUtils.startsWithIgnoreCase("abcdef", "abc") = true - * StringUtils.startsWithIgnoreCase("ABCDEF", "abc") = true - *- * - * @param str the CharSequence to check, may be null - * @param prefix the prefix to find, may be null - * @return {@code true} if the CharSequence starts with the prefix, case insensitive, or both {@code null} - * @see java.lang.String#startsWith(String) - */ - public static boolean startsWithIgnoreCase(final CharSequence str, final CharSequence prefix) { - return startsWith(str, prefix, true); - } - - // Stripping - //----------------------------------------------------------------------- - - /** - *
Strips whitespace from the start and end of a String.
- * - *This is similar to {@link #trim(String)} but removes whitespace. - * Whitespace is defined by {@link Character#isWhitespace(char)}.
- * - *A {@code null} input String returns {@code null}.
- * - *- * StringUtils.strip(null) = null - * StringUtils.strip("") = "" - * StringUtils.strip(" ") = "" - * StringUtils.strip("abc") = "abc" - * StringUtils.strip(" abc") = "abc" - * StringUtils.strip("abc ") = "abc" - * StringUtils.strip(" abc ") = "abc" - * StringUtils.strip(" ab c ") = "ab c" - *- * - * @param str the String to remove whitespace from, may be null - * @return the stripped String, {@code null} if null String input - */ - public static String strip(final String str) { - return strip(str, null); - } - - /** - *
Strips any of a set of characters from the start and end of a String. - * This is similar to {@link String#trim()} but allows the characters to be stripped to be controlled.
- * - *A {@code null} input String returns {@code null}. - * An empty string ("") input returns the empty string.
- * - *If the stripChars String is {@code null}, whitespace is - * stripped as defined by {@link Character#isWhitespace(char)}. Alternatively use {@link #strip(String)}.
- * - *- * StringUtils.strip(null, *) = null - * StringUtils.strip("", *) = "" - * StringUtils.strip("abc", null) = "abc" - * StringUtils.strip(" abc", null) = "abc" - * StringUtils.strip("abc ", null) = "abc" - * StringUtils.strip(" abc ", null) = "abc" - * StringUtils.strip(" abcyx", "xyz") = " abc" - *- * - * @param str the String to remove characters from, may be null - * @param stripChars the characters to remove, null treated as whitespace - * @return the stripped String, {@code null} if null String input - */ - public static String strip(String str, final String stripChars) { - if (isEmpty(str)) { - return str; - } - str = stripStart(str, stripChars); - return stripEnd(str, stripChars); - } - - /** - *
Strips any of a set of characters from the start of a String.
- * - *A {@code null} input String returns {@code null}. - * An empty string ("") input returns the empty string.
- * - *If the stripChars String is {@code null}, whitespace is - * stripped as defined by {@link Character#isWhitespace(char)}.
- * - *- * StringUtils.stripStart(null, *) = null - * StringUtils.stripStart("", *) = "" - * StringUtils.stripStart("abc", "") = "abc" - * StringUtils.stripStart("abc", null) = "abc" - * StringUtils.stripStart(" abc", null) = "abc" - * StringUtils.stripStart("abc ", null) = "abc " - * StringUtils.stripStart(" abc ", null) = "abc " - * StringUtils.stripStart("yxabc ", "xyz") = "abc " - *- * - * @param str the String to remove characters from, may be null - * @param stripChars the characters to remove, null treated as whitespace - * @return the stripped String, {@code null} if null String input - */ - public static String stripStart(final String str, final String stripChars) { - int strLen; - if (str == null || (strLen = str.length()) == 0) { - return str; - } - int start = 0; - if (stripChars == null) { - while (start != strLen && Character.isWhitespace(str.charAt(start))) { - start++; - } - } else if (stripChars.isEmpty()) { - return str; - } else { - while (start != strLen && stripChars.indexOf(str.charAt(start)) != INDEX_NOT_FOUND) { - start++; - } - } - return str.substring(start); - } - - /** - *
Strips any of a set of characters from the end of a String.
- * - *A {@code null} input String returns {@code null}. - * An empty string ("") input returns the empty string.
- * - *If the stripChars String is {@code null}, whitespace is - * stripped as defined by {@link Character#isWhitespace(char)}.
- * - *- * StringUtils.stripEnd(null, *) = null - * StringUtils.stripEnd("", *) = "" - * StringUtils.stripEnd("abc", "") = "abc" - * StringUtils.stripEnd("abc", null) = "abc" - * StringUtils.stripEnd(" abc", null) = " abc" - * StringUtils.stripEnd("abc ", null) = "abc" - * StringUtils.stripEnd(" abc ", null) = " abc" - * StringUtils.stripEnd(" abcyx", "xyz") = " abc" - * StringUtils.stripEnd("120.00", ".0") = "12" - *- * - * @param str the String to remove characters from, may be null - * @param stripChars the set of characters to remove, null treated as whitespace - * @return the stripped String, {@code null} if null String input - */ - public static String stripEnd(final String str, final String stripChars) { - int end; - if (str == null || (end = str.length()) == 0) { - return str; - } - - if (stripChars == null) { - while (end != 0 && Character.isWhitespace(str.charAt(end - 1))) { - end--; - } - } else if (stripChars.isEmpty()) { - return str; - } else { - while (end != 0 && stripChars.indexOf(str.charAt(end - 1)) != INDEX_NOT_FOUND) { - end--; - } - } - return str.substring(0, end); - } - - /** - *
Splits the provided text into an array, separators specified. - * This is an alternative to using StringTokenizer.
- * - *The separator is not included in the returned String array. - * Adjacent separators are treated as one separator. For more control over the split use the StrTokenizer - * class.
- * - *A {@code null} input String returns {@code null}. - * A {@code null} separatorChars splits on whitespace.
- * - *- * StringUtils.split(null, *) = null - * StringUtils.split("", *) = [] - * StringUtils.split("abc def", null) = ["abc", "def"] - * StringUtils.split("abc def", " ") = ["abc", "def"] - * StringUtils.split("abc def", " ") = ["abc", "def"] - * StringUtils.split("ab:cd:ef", ":") = ["ab", "cd", "ef"] - *- * - * @param str the String to parse, may be null - * @param separatorChars the characters used as the delimiters, {@code null} splits on whitespace - * @return an array of parsed Strings, {@code null} if null String input - */ - public static String[] split(final String str, final String separatorChars) { - return splitWorker(str, separatorChars, -1, false); - } - - /** - * Performs the logic for the {@code split} and {@code splitPreserveAllTokens} methods that return a maximum array - * length. - * - * @param str the String to parse, may be {@code null} - * @param separatorChars the separate character - * @param max the maximum number of elements to include in the array. A zero or negative value implies - * no limit. - * @param preserveAllTokens if {@code true}, adjacent separators are treated as empty token separators; if {@code - * false}, adjacent separators are treated as one separator. - * @return an array of parsed Strings, {@code null} if null String input - */ - @SuppressWarnings("PMD.AvoidComplexConditionRule") - private static String[] splitWorker(final String str, final String separatorChars, final int max, - final boolean preserveAllTokens) { - // Performance tuned for 2.0 (JDK1.4) - // Direct code is quicker than StringTokenizer. - // Also, StringTokenizer uses isSpace() not isWhitespace() - - if (str == null) { - return null; - } - final int len = str.length(); - if (len == 0) { - return ArrayUtil.EMPTY_STRING_ARRAY; - } - final List