Fix defaultIsEmpty typing.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@922904 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
1ced894df7
commit
5b7817cc3f
|
@ -37,26 +37,31 @@ public class CharSequenceUtils {
|
||||||
return cs == null ? 0 : cs.length();
|
return cs == null ? 0 : cs.length();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Reverses a CharSequence as per {@link StringBuilder#reverse()}.</p>
|
* <p>
|
||||||
*
|
* Reverses a CharSequence as per {@link StringBuilder#reverse()}.
|
||||||
* <p>A <code>null</code> CharSequence returns <code>null</code>.</p>
|
* </p>
|
||||||
*
|
*
|
||||||
* <pre>
|
* <p>
|
||||||
* CharSequenceUtils.reverse(null) = null
|
* A <code>null</code> CharSequence returns <code>null</code>.
|
||||||
* CharSequenceUtils.reverse("").toString() = ""
|
* </p>
|
||||||
* CharSequenceUtils.reverse("bat").toString() = "tab"
|
*
|
||||||
* </pre>
|
* <pre>
|
||||||
*
|
* CharSequenceUtils.reverse(null) = null
|
||||||
* @param str the String to reverse, may be null
|
* CharSequenceUtils.reverse("").toString() = ""
|
||||||
* @return the reversed String, <code>null</code> if null String input
|
* CharSequenceUtils.reverse("bat").toString() = "tab"
|
||||||
*/
|
* </pre>
|
||||||
public static CharSequence reverse(CharSequence str) {
|
*
|
||||||
if (str == null) {
|
* @param str
|
||||||
return null;
|
* the String to reverse, may be null
|
||||||
}
|
* @return the reversed String, <code>null</code> if null String input
|
||||||
return new StringBuilder(str).reverse();
|
*/
|
||||||
}
|
public static CharSequence reverse(CharSequence str) {
|
||||||
|
if (str == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return new StringBuilder(str).reverse();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a new <code>CharSequence</code> that is a subsequence of this
|
* Returns a new <code>CharSequence</code> that is a subsequence of this
|
||||||
|
@ -78,4 +83,39 @@ public class CharSequenceUtils {
|
||||||
public static CharSequence subSequence(CharSequence cs, int start) {
|
public static CharSequence subSequence(CharSequence cs, int start) {
|
||||||
return cs == null ? null : cs.subSequence(start, cs.length());
|
return cs == null ? null : cs.subSequence(start, cs.length());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static int indexOf(CharSequence cs, int ch, int startPos) {
|
||||||
|
int max = cs.length();
|
||||||
|
|
||||||
|
if (startPos < 0) {
|
||||||
|
startPos = 0;
|
||||||
|
} else if (startPos >= max) {
|
||||||
|
return StringUtils.INDEX_NOT_FOUND;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
|
||||||
|
for (int i = startPos; i < max; i++) {
|
||||||
|
if (cs.charAt(i) == ch) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return StringUtils.INDEX_NOT_FOUND;
|
||||||
|
}
|
||||||
|
|
||||||
|
// supp chars
|
||||||
|
if (ch <= Character.MAX_CODE_POINT) {
|
||||||
|
char[] surrogates = Character.toChars(ch);
|
||||||
|
for (int i = startPos; i < max; i++) {
|
||||||
|
if (cs.charAt(i) == surrogates[0]) {
|
||||||
|
if (i + 1 == max) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (cs.charAt(i + 1) == surrogates[1]) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return StringUtils.INDEX_NOT_FOUND;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5390,7 +5390,7 @@ public class StringUtils {
|
||||||
* if the input is empty ("") or <code>null</code>, may be null
|
* if the input is empty ("") or <code>null</code>, may be null
|
||||||
* @return the passed in CharSequence, or the default
|
* @return the passed in CharSequence, or the default
|
||||||
*/
|
*/
|
||||||
public static CharSequence defaultIfEmpty(CharSequence str, CharSequence defaultStr) {
|
public static <T extends CharSequence> T defaultIfEmpty(T str, T defaultStr) {
|
||||||
return StringUtils.isEmpty(str) ? defaultStr : str;
|
return StringUtils.isEmpty(str) ? defaultStr : str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1416,24 +1416,36 @@ public class StringUtilsTest extends TestCase {
|
||||||
assertEquals("NULL", StringUtils.defaultIfEmpty("", "NULL"));
|
assertEquals("NULL", StringUtils.defaultIfEmpty("", "NULL"));
|
||||||
assertEquals("abc", StringUtils.defaultIfEmpty("abc", "NULL"));
|
assertEquals("abc", StringUtils.defaultIfEmpty("abc", "NULL"));
|
||||||
assertNull(StringUtils.defaultIfEmpty("", null));
|
assertNull(StringUtils.defaultIfEmpty("", null));
|
||||||
|
// Tests compatibility for the API return type
|
||||||
|
String s = StringUtils.defaultIfEmpty("abc", "NULL");
|
||||||
|
assertEquals("abc", s);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testDefaultIfEmpty_StringBuilders() {
|
public void testDefaultIfEmpty_StringBuilders() {
|
||||||
assertEquals("NULL", StringUtils.defaultIfEmpty(new StringBuilder(""), new StringBuilder("NULL")).toString());
|
assertEquals("NULL", StringUtils.defaultIfEmpty(new StringBuilder(""), new StringBuilder("NULL")).toString());
|
||||||
assertEquals("abc", StringUtils.defaultIfEmpty(new StringBuilder("abc"), new StringBuilder("NULL")).toString());
|
assertEquals("abc", StringUtils.defaultIfEmpty(new StringBuilder("abc"), new StringBuilder("NULL")).toString());
|
||||||
assertNull(StringUtils.defaultIfEmpty(new StringBuilder(""), null));
|
assertNull(StringUtils.defaultIfEmpty(new StringBuilder(""), null));
|
||||||
|
// Tests compatibility for the API return type
|
||||||
|
StringBuilder s = StringUtils.defaultIfEmpty(new StringBuilder("abc"), new StringBuilder("NULL"));
|
||||||
|
assertEquals("abc", s.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testDefaultIfEmpty_StringBuffers() {
|
public void testDefaultIfEmpty_StringBuffers() {
|
||||||
assertEquals("NULL", StringUtils.defaultIfEmpty(new StringBuffer(""), new StringBuffer("NULL")).toString());
|
assertEquals("NULL", StringUtils.defaultIfEmpty(new StringBuffer(""), new StringBuffer("NULL")).toString());
|
||||||
assertEquals("abc", StringUtils.defaultIfEmpty(new StringBuffer("abc"), new StringBuffer("NULL")).toString());
|
assertEquals("abc", StringUtils.defaultIfEmpty(new StringBuffer("abc"), new StringBuffer("NULL")).toString());
|
||||||
assertNull(StringUtils.defaultIfEmpty(new StringBuffer(""), null));
|
assertNull(StringUtils.defaultIfEmpty(new StringBuffer(""), null));
|
||||||
|
// Tests compatibility for the API return type
|
||||||
|
StringBuffer s = StringUtils.defaultIfEmpty(new StringBuffer("abc"), new StringBuffer("NULL"));
|
||||||
|
assertEquals("abc", s.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testDefaultIfEmpty_CharBuffers() {
|
public void testDefaultIfEmpty_CharBuffers() {
|
||||||
assertEquals("NULL", StringUtils.defaultIfEmpty(CharBuffer.wrap(""), CharBuffer.wrap("NULL")).toString());
|
assertEquals("NULL", StringUtils.defaultIfEmpty(CharBuffer.wrap(""), CharBuffer.wrap("NULL")).toString());
|
||||||
assertEquals("abc", StringUtils.defaultIfEmpty(CharBuffer.wrap("abc"), CharBuffer.wrap("NULL")).toString());
|
assertEquals("abc", StringUtils.defaultIfEmpty(CharBuffer.wrap("abc"), CharBuffer.wrap("NULL")).toString());
|
||||||
assertNull(StringUtils.defaultIfEmpty(CharBuffer.wrap(""), null));
|
assertNull(StringUtils.defaultIfEmpty(CharBuffer.wrap(""), null));
|
||||||
|
// Tests compatibility for the API return type
|
||||||
|
CharBuffer s = StringUtils.defaultIfEmpty(CharBuffer.wrap("abc"), CharBuffer.wrap("NULL"));
|
||||||
|
assertEquals("abc", s.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
|
|
Loading…
Reference in New Issue