2003-01-13 17:58:47 +01:00
|
|
|
/**
|
|
|
|
*
|
2018-03-29 12:35:11 +02:00
|
|
|
* Copyright 2003-2007 Jive Software, 2016-2018 Florian Schmaus.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2014-02-17 18:57:38 +01:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
2004-11-03 00:53:30 +01:00
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2004-11-03 00:53:30 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2004-11-03 00:53:30 +01:00
|
|
|
* 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.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
package org.jivesoftware.smack.util;
|
|
|
|
|
2006-07-18 07:14:33 +02:00
|
|
|
import java.io.UnsupportedEncodingException;
|
2016-01-06 14:56:49 +01:00
|
|
|
import java.security.SecureRandom;
|
2014-05-29 09:21:04 +02:00
|
|
|
import java.util.Collection;
|
2015-07-14 13:40:23 +02:00
|
|
|
import java.util.Iterator;
|
2003-01-13 17:58:47 +01:00
|
|
|
import java.util.Random;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A collection of utility methods for String objects.
|
|
|
|
*/
|
|
|
|
public class StringUtils {
|
2014-07-07 00:08:37 +02:00
|
|
|
|
2014-08-01 10:34:47 +02:00
|
|
|
public static final String MD5 = "MD5";
|
2014-07-07 00:08:37 +02:00
|
|
|
public static final String SHA1 = "SHA-1";
|
|
|
|
public static final String UTF8 = "UTF-8";
|
2014-09-04 11:04:51 +02:00
|
|
|
public static final String USASCII = "US-ASCII";
|
2010-08-15 15:20:48 +02:00
|
|
|
|
2014-03-18 18:40:08 +01:00
|
|
|
public static final String QUOTE_ENCODE = """;
|
|
|
|
public static final String APOS_ENCODE = "'";
|
|
|
|
public static final String AMP_ENCODE = "&";
|
|
|
|
public static final String LT_ENCODE = "<";
|
|
|
|
public static final String GT_ENCODE = ">";
|
2003-01-13 17:58:47 +01:00
|
|
|
|
2014-08-01 10:34:47 +02:00
|
|
|
public static final char[] HEX_CHARS = "0123456789abcdef".toCharArray();
|
|
|
|
|
2016-01-13 15:04:01 +01:00
|
|
|
/**
|
|
|
|
* Escape <code>input</code> for XML.
|
|
|
|
*
|
|
|
|
* @param input the input to escape.
|
|
|
|
* @return the XML escaped variant of <code>input</code>.
|
|
|
|
*/
|
|
|
|
public static CharSequence escapeForXml(CharSequence input) {
|
|
|
|
return escapeForXml(input, XmlEscapeMode.safe);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Escape <code>input</code> for XML.
|
|
|
|
*
|
|
|
|
* @param input the input to escape.
|
|
|
|
* @return the XML escaped variant of <code>input</code>.
|
|
|
|
* @since 4.2
|
|
|
|
*/
|
|
|
|
public static CharSequence escapeForXmlAttribute(CharSequence input) {
|
|
|
|
return escapeForXml(input, XmlEscapeMode.forAttribute);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Escape <code>input</code> for XML.
|
|
|
|
* <p>
|
|
|
|
* This is an optimized variant of {@link #escapeForXmlAttribute(CharSequence)} for XML where the
|
|
|
|
* XML attribute is quoted using ''' (Apos).
|
|
|
|
* </p>
|
|
|
|
*
|
|
|
|
* @param input the input to escape.
|
|
|
|
* @return the XML escaped variant of <code>input</code>.
|
|
|
|
* @since 4.2
|
|
|
|
*/
|
|
|
|
public static CharSequence escapeForXmlAttributeApos(CharSequence input) {
|
|
|
|
return escapeForXml(input, XmlEscapeMode.forAttributeApos);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Escape <code>input</code> for XML.
|
|
|
|
*
|
|
|
|
* @param input the input to escape.
|
|
|
|
* @return the XML escaped variant of <code>input</code>.
|
|
|
|
* @since 4.2
|
|
|
|
*/
|
|
|
|
public static CharSequence escapeForXmlText(CharSequence input) {
|
|
|
|
return escapeForXml(input, XmlEscapeMode.forText);
|
|
|
|
}
|
|
|
|
|
|
|
|
private enum XmlEscapeMode {
|
|
|
|
safe,
|
|
|
|
forAttribute,
|
|
|
|
forAttributeApos,
|
|
|
|
forText,
|
|
|
|
;
|
2016-11-01 12:48:59 +01:00
|
|
|
}
|
2016-01-13 15:04:01 +01:00
|
|
|
|
2003-01-13 17:58:47 +01:00
|
|
|
/**
|
2015-07-01 23:21:53 +02:00
|
|
|
* Escapes all necessary characters in the CharSequence so that it can be used
|
2003-01-13 17:58:47 +01:00
|
|
|
* in an XML doc.
|
|
|
|
*
|
2015-07-01 23:21:53 +02:00
|
|
|
* @param input the CharSequence to escape.
|
2003-01-13 17:58:47 +01:00
|
|
|
* @return the string with appropriate characters escaped.
|
|
|
|
*/
|
2016-01-13 15:04:01 +01:00
|
|
|
private static CharSequence escapeForXml(final CharSequence input, final XmlEscapeMode xmlEscapeMode) {
|
2015-07-01 23:21:53 +02:00
|
|
|
if (input == null) {
|
2003-01-13 17:58:47 +01:00
|
|
|
return null;
|
|
|
|
}
|
2015-07-01 23:21:53 +02:00
|
|
|
final int len = input.length();
|
2017-05-23 16:45:04 +02:00
|
|
|
final StringBuilder out = new StringBuilder((int) (len * 1.3));
|
2014-03-18 18:40:08 +01:00
|
|
|
CharSequence toAppend;
|
2003-01-13 17:58:47 +01:00
|
|
|
char ch;
|
2014-03-18 18:40:08 +01:00
|
|
|
int last = 0;
|
|
|
|
int i = 0;
|
|
|
|
while (i < len) {
|
|
|
|
toAppend = null;
|
2015-07-01 23:21:53 +02:00
|
|
|
ch = input.charAt(i);
|
2016-01-13 15:04:01 +01:00
|
|
|
switch (xmlEscapeMode) {
|
|
|
|
case safe:
|
|
|
|
switch (ch) {
|
|
|
|
case '<':
|
|
|
|
toAppend = LT_ENCODE;
|
|
|
|
break;
|
|
|
|
case '>':
|
|
|
|
toAppend = GT_ENCODE;
|
|
|
|
break;
|
|
|
|
case '&':
|
|
|
|
toAppend = AMP_ENCODE;
|
|
|
|
break;
|
|
|
|
case '"':
|
|
|
|
toAppend = QUOTE_ENCODE;
|
|
|
|
break;
|
|
|
|
case '\'':
|
|
|
|
toAppend = APOS_ENCODE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-03-18 18:40:08 +01:00
|
|
|
break;
|
2016-01-13 15:04:01 +01:00
|
|
|
case forAttribute:
|
|
|
|
// No need to escape '>' for attributes.
|
2017-05-23 16:45:04 +02:00
|
|
|
switch (ch) {
|
2016-01-13 15:04:01 +01:00
|
|
|
case '<':
|
|
|
|
toAppend = LT_ENCODE;
|
|
|
|
break;
|
|
|
|
case '&':
|
|
|
|
toAppend = AMP_ENCODE;
|
|
|
|
break;
|
|
|
|
case '"':
|
|
|
|
toAppend = QUOTE_ENCODE;
|
|
|
|
break;
|
|
|
|
case '\'':
|
|
|
|
toAppend = APOS_ENCODE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-03-18 18:40:08 +01:00
|
|
|
break;
|
2016-01-13 15:04:01 +01:00
|
|
|
case forAttributeApos:
|
|
|
|
// No need to escape '>' and '"' for attributes using '\'' as quote.
|
2017-05-23 16:45:04 +02:00
|
|
|
switch (ch) {
|
2016-01-13 15:04:01 +01:00
|
|
|
case '<':
|
|
|
|
toAppend = LT_ENCODE;
|
|
|
|
break;
|
|
|
|
case '&':
|
|
|
|
toAppend = AMP_ENCODE;
|
|
|
|
break;
|
|
|
|
case '\'':
|
|
|
|
toAppend = APOS_ENCODE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-03-18 18:40:08 +01:00
|
|
|
break;
|
2016-01-13 15:04:01 +01:00
|
|
|
case forText:
|
|
|
|
// No need to escape '"', '\'', and '>' for text.
|
2017-05-23 16:45:04 +02:00
|
|
|
switch (ch) {
|
2016-01-13 15:04:01 +01:00
|
|
|
case '<':
|
|
|
|
toAppend = LT_ENCODE;
|
|
|
|
break;
|
|
|
|
case '&':
|
|
|
|
toAppend = AMP_ENCODE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-03-18 18:40:08 +01:00
|
|
|
break;
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
2014-03-18 18:40:08 +01:00
|
|
|
if (toAppend != null) {
|
2009-06-04 13:58:25 +02:00
|
|
|
if (i > last) {
|
2015-07-01 23:21:53 +02:00
|
|
|
out.append(input, last, i);
|
2009-06-04 13:58:25 +02:00
|
|
|
}
|
2014-03-18 18:40:08 +01:00
|
|
|
out.append(toAppend);
|
|
|
|
last = ++i;
|
|
|
|
} else {
|
|
|
|
i++;
|
2009-06-04 13:58:25 +02:00
|
|
|
}
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
if (last == 0) {
|
2015-07-01 23:21:53 +02:00
|
|
|
return input;
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
if (i > last) {
|
2015-07-01 23:21:53 +02:00
|
|
|
out.append(input, last, i);
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
2014-03-21 09:51:52 +01:00
|
|
|
return out;
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hashes a String using the SHA-1 algorithm and returns the result as a
|
|
|
|
* String of hexadecimal numbers. This method is synchronized to avoid
|
|
|
|
* excessive MessageDigest object creation. If calling this method becomes
|
|
|
|
* a bottleneck in your code, you may wish to maintain a pool of
|
|
|
|
* MessageDigest objects instead of using this method.
|
|
|
|
* <p>
|
|
|
|
* A hash is a one-way function -- that is, given an
|
|
|
|
* input, an output is easily computed. However, given the output, the
|
|
|
|
* input is almost impossible to compute. This is useful for passwords
|
|
|
|
* since we can store the hash and a hacker will then have a very hard time
|
|
|
|
* determining the original password.
|
|
|
|
*
|
|
|
|
* @param data the String to compute the hash of.
|
|
|
|
* @return a hashed version of the passed-in String
|
2014-10-21 11:59:11 +02:00
|
|
|
* @deprecated use {@link org.jivesoftware.smack.util.SHA1#hex(String)} instead.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
2014-10-21 11:59:11 +02:00
|
|
|
@Deprecated
|
2018-03-29 12:35:11 +02:00
|
|
|
public static synchronized String hash(String data) {
|
2014-10-21 11:59:11 +02:00
|
|
|
return org.jivesoftware.smack.util.SHA1.hex(data);
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-08-26 18:49:25 +02:00
|
|
|
* Encodes an array of bytes as String representation of hexadecimal.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2005-08-26 18:49:25 +02:00
|
|
|
* @param bytes an array of bytes to convert to a hex string.
|
|
|
|
* @return generated hex string.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
2005-08-26 18:49:25 +02:00
|
|
|
public static String encodeHex(byte[] bytes) {
|
2014-08-01 10:34:47 +02:00
|
|
|
char[] hexChars = new char[bytes.length * 2];
|
2016-11-29 12:01:41 +01:00
|
|
|
for (int j = 0; j < bytes.length; j++) {
|
2014-08-01 10:34:47 +02:00
|
|
|
int v = bytes[j] & 0xFF;
|
|
|
|
hexChars[j * 2] = HEX_CHARS[v >>> 4];
|
|
|
|
hexChars[j * 2 + 1] = HEX_CHARS[v & 0x0F];
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
2014-08-01 10:34:47 +02:00
|
|
|
return new String(hexChars);
|
|
|
|
}
|
2006-05-03 23:13:25 +02:00
|
|
|
|
2017-08-16 14:51:57 +02:00
|
|
|
public static byte[] toUtf8Bytes(String string) {
|
2014-08-01 10:34:47 +02:00
|
|
|
try {
|
|
|
|
return string.getBytes(StringUtils.UTF8);
|
|
|
|
}
|
|
|
|
catch (UnsupportedEncodingException e) {
|
|
|
|
throw new IllegalStateException("UTF-8 encoding not supported by platform", e);
|
|
|
|
}
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2003-01-13 17:58:47 +01:00
|
|
|
/**
|
|
|
|
* Pseudo-random number generator object for use with randomString().
|
|
|
|
* The Random class is not considered to be cryptographically secure, so
|
|
|
|
* only use these random Strings for low to medium security applications.
|
|
|
|
*/
|
2017-03-10 17:16:53 +01:00
|
|
|
private static final ThreadLocal<Random> randGen = new ThreadLocal<Random>() {
|
|
|
|
@Override
|
|
|
|
protected Random initialValue() {
|
|
|
|
return new Random();
|
|
|
|
}
|
|
|
|
};
|
2003-01-13 17:58:47 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Array of numbers and letters of mixed case. Numbers appear in the list
|
|
|
|
* twice so that there is a more equal chance that a number will be picked.
|
|
|
|
* We can use the array to get a random number or letter by picking a random
|
|
|
|
* array index.
|
|
|
|
*/
|
2016-01-10 18:23:06 +01:00
|
|
|
private static final char[] numbersAndLetters = ("0123456789abcdefghijklmnopqrstuvwxyz" +
|
2017-11-16 17:48:29 +01:00
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
|
2003-01-13 17:58:47 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a random String of numbers and letters (lower and upper case)
|
|
|
|
* of the specified length. The method uses the Random class that is
|
|
|
|
* built-in to Java which is suitable for low to medium grade security uses.
|
|
|
|
* This means that the output is only pseudo random, i.e., each number is
|
|
|
|
* mathematically generated so is not truly random.<p>
|
|
|
|
*
|
|
|
|
* The specified length must be at least one. If not, the method will return
|
|
|
|
* null.
|
|
|
|
*
|
|
|
|
* @param length the desired length of the random String to return.
|
|
|
|
* @return a random String of numbers and letters of the specified length.
|
|
|
|
*/
|
2016-01-06 14:56:49 +01:00
|
|
|
public static String insecureRandomString(int length) {
|
2017-11-16 18:02:02 +01:00
|
|
|
return randomString(length, randGen.get());
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
2003-01-16 16:40:55 +01:00
|
|
|
|
2017-03-10 17:16:53 +01:00
|
|
|
private static final ThreadLocal<SecureRandom> SECURE_RANDOM = new ThreadLocal<SecureRandom>() {
|
|
|
|
@Override
|
|
|
|
protected SecureRandom initialValue() {
|
|
|
|
return new SecureRandom();
|
|
|
|
}
|
|
|
|
};
|
2016-01-06 14:56:49 +01:00
|
|
|
|
|
|
|
public static String randomString(final int length) {
|
2017-11-16 18:02:02 +01:00
|
|
|
return randomString(length, SECURE_RANDOM.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
private static String randomString(final int length, Random random) {
|
2016-01-06 14:56:49 +01:00
|
|
|
if (length < 1) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
byte[] randomBytes = new byte[length];
|
2017-11-16 18:02:02 +01:00
|
|
|
random.nextBytes(randomBytes);
|
2016-01-06 14:56:49 +01:00
|
|
|
char[] randomChars = new char[length];
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
randomChars[i] = getPrintableChar(randomBytes[i]);
|
|
|
|
}
|
|
|
|
return new String(randomChars);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static char getPrintableChar(byte indexByte) {
|
2017-05-23 16:45:04 +02:00
|
|
|
assert (numbersAndLetters.length < Byte.MAX_VALUE * 2);
|
2016-01-06 14:56:49 +01:00
|
|
|
|
|
|
|
// Convert indexByte as it where an unsigned byte by promoting it to int
|
|
|
|
// and masking it with 0xff. Yields results from 0 - 254.
|
|
|
|
int index = indexByte & 0xff;
|
|
|
|
return numbersAndLetters[index % numbersAndLetters.length];
|
|
|
|
}
|
|
|
|
|
2014-03-06 09:42:25 +01:00
|
|
|
/**
|
2015-03-29 12:15:32 +02:00
|
|
|
* Returns true if CharSequence is not null and is not empty, false otherwise.
|
2014-03-06 09:42:25 +01:00
|
|
|
* Examples:
|
|
|
|
* isNotEmpty(null) - false
|
|
|
|
* isNotEmpty("") - false
|
|
|
|
* isNotEmpty(" ") - true
|
|
|
|
* isNotEmpty("empty") - true
|
|
|
|
*
|
2014-04-15 23:33:42 +02:00
|
|
|
* @param cs checked CharSequence
|
2014-03-06 09:42:25 +01:00
|
|
|
* @return true if string is not null and is not empty, false otherwise
|
|
|
|
*/
|
2014-04-15 23:33:42 +02:00
|
|
|
public static boolean isNotEmpty(CharSequence cs) {
|
|
|
|
return !isNullOrEmpty(cs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-08-01 23:20:35 +02:00
|
|
|
* Returns true if the given CharSequence is null or empty.
|
2014-04-15 23:33:42 +02:00
|
|
|
*
|
|
|
|
* @param cs
|
2014-08-01 23:20:35 +02:00
|
|
|
* @return true if the given CharSequence is null or empty
|
2014-04-15 23:33:42 +02:00
|
|
|
*/
|
|
|
|
public static boolean isNullOrEmpty(CharSequence cs) {
|
2014-08-01 23:20:35 +02:00
|
|
|
return cs == null || isEmpty(cs);
|
|
|
|
}
|
|
|
|
|
2016-11-27 21:14:44 +01:00
|
|
|
/**
|
|
|
|
* Returns true if all given CharSequences are not empty.
|
|
|
|
*
|
|
|
|
* @param css the CharSequences to test.
|
|
|
|
* @return true if all given CharSequences are not empty.
|
|
|
|
*/
|
|
|
|
public static boolean isNotEmpty(CharSequence... css) {
|
|
|
|
for (CharSequence cs : css) {
|
|
|
|
if (StringUtils.isNullOrEmpty(cs)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if all given CharSequences are either null or empty.
|
|
|
|
*
|
|
|
|
* @param css the CharSequences to test.
|
|
|
|
* @return true if all given CharSequences are null or empty.
|
|
|
|
*/
|
|
|
|
public static boolean isNullOrEmpty(CharSequence... css) {
|
|
|
|
for (CharSequence cs : css) {
|
|
|
|
if (StringUtils.isNotEmpty(cs)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-08-01 23:20:35 +02:00
|
|
|
/**
|
2015-03-29 12:15:32 +02:00
|
|
|
* Returns true if the given CharSequence is empty.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-08-01 23:20:35 +02:00
|
|
|
* @param cs
|
|
|
|
* @return true if the given CharSequence is empty
|
|
|
|
*/
|
|
|
|
public static boolean isEmpty(CharSequence cs) {
|
|
|
|
return cs.length() == 0;
|
2014-03-06 09:42:25 +01:00
|
|
|
}
|
2014-05-29 09:21:04 +02:00
|
|
|
|
2015-07-14 13:40:23 +02:00
|
|
|
/**
|
2015-07-21 08:19:15 +02:00
|
|
|
* Transform a collection of objects to a whitespace delimited String.
|
2015-07-14 13:40:23 +02:00
|
|
|
*
|
|
|
|
* @param collection the collection to transform.
|
|
|
|
* @return a String with all the elements of the collection.
|
|
|
|
*/
|
2015-07-21 08:19:15 +02:00
|
|
|
public static String collectionToString(Collection<? extends Object> collection) {
|
|
|
|
return toStringBuilder(collection, " ").toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transform a collection of objects to a delimited String.
|
|
|
|
*
|
|
|
|
* @param collection the collection to transform.
|
|
|
|
* @param delimiter the delimiter used to delimit the Strings.
|
|
|
|
* @return a StringBuilder with all the elements of the collection.
|
|
|
|
*/
|
|
|
|
public static StringBuilder toStringBuilder(Collection<? extends Object> collection, String delimiter) {
|
2015-07-14 13:40:23 +02:00
|
|
|
StringBuilder sb = new StringBuilder(collection.size() * 20);
|
2015-07-21 08:19:15 +02:00
|
|
|
for (Iterator<? extends Object> it = collection.iterator(); it.hasNext();) {
|
|
|
|
Object cs = it.next();
|
2015-07-14 13:40:23 +02:00
|
|
|
sb.append(cs);
|
|
|
|
if (it.hasNext()) {
|
2015-07-21 08:19:15 +02:00
|
|
|
sb.append(delimiter);
|
2015-07-14 13:40:23 +02:00
|
|
|
}
|
2014-05-29 09:21:04 +02:00
|
|
|
}
|
2015-07-21 08:19:15 +02:00
|
|
|
return sb;
|
2014-05-29 09:21:04 +02:00
|
|
|
}
|
2014-08-01 10:34:47 +02:00
|
|
|
|
|
|
|
public static String returnIfNotEmptyTrimmed(String string) {
|
|
|
|
if (string == null)
|
|
|
|
return null;
|
|
|
|
String trimmedString = string.trim();
|
|
|
|
if (trimmedString.length() > 0) {
|
|
|
|
return trimmedString;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2014-09-05 22:06:58 +02:00
|
|
|
|
2014-09-01 10:13:23 +02:00
|
|
|
public static boolean nullSafeCharSequenceEquals(CharSequence csOne, CharSequence csTwo) {
|
2017-12-13 23:10:11 +01:00
|
|
|
return nullSafeCharSequenceComparator(csOne, csTwo) == 0;
|
2014-09-01 10:13:23 +02:00
|
|
|
}
|
|
|
|
|
2017-12-13 23:10:11 +01:00
|
|
|
public static int nullSafeCharSequenceComparator(CharSequence csOne, CharSequence csTwo) {
|
2014-09-01 10:13:23 +02:00
|
|
|
if (csOne == null ^ csTwo == null) {
|
|
|
|
return (csOne == null) ? -1 : 1;
|
|
|
|
}
|
|
|
|
if (csOne == null && csTwo == null) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return csOne.toString().compareTo(csTwo.toString());
|
|
|
|
}
|
2015-01-26 20:36:55 +01:00
|
|
|
|
|
|
|
public static <CS extends CharSequence> CS requireNotNullOrEmpty(CS cs, String message) {
|
|
|
|
if (isNullOrEmpty(cs)) {
|
|
|
|
throw new IllegalArgumentException(message);
|
|
|
|
}
|
|
|
|
return cs;
|
|
|
|
}
|
2015-01-28 09:31:28 +01:00
|
|
|
|
2018-06-07 17:55:00 +02:00
|
|
|
public static <CS extends CharSequence> CS requireNullOrNotEmpty(CS cs, String message) {
|
|
|
|
if (cs == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (cs.toString().isEmpty()) {
|
|
|
|
throw new IllegalArgumentException(message);
|
|
|
|
}
|
|
|
|
return cs;
|
|
|
|
}
|
|
|
|
|
2015-01-28 09:31:28 +01:00
|
|
|
/**
|
|
|
|
* Return the String representation of the given char sequence if it is not null.
|
|
|
|
*
|
|
|
|
* @param cs the char sequence or null.
|
|
|
|
* @return the String representation of <code>cs</code> or null.
|
|
|
|
*/
|
|
|
|
public static String maybeToString(CharSequence cs) {
|
|
|
|
if (cs == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return cs.toString();
|
|
|
|
}
|
2009-06-04 13:58:25 +02:00
|
|
|
}
|