You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by lg...@apache.org on 2015/10/23 19:56:14 UTC
[1/4] [lang] LANG-1171 Add null safe compare methods in StringUtils :
- StringUtils.compare(String str1, String str2);
- StringUtils.compare(String str1, String str2, boolean nullIsLess);
- StringUtils.compareIgnoreCase(String str1, String str2);
Repository: commons-lang
Updated Branches:
refs/heads/master ae865193e -> 849578d3a
LANG-1171 Add null safe compare methods in StringUtils :
- StringUtils.compare(String str1, String str2);
- StringUtils.compare(String str1, String str2, boolean nullIsLess);
- StringUtils.compareIgnoreCase(String str1, String str2);
- StringUtils.compareIgnoreCase(String str1, String str2, boolean nullIsLess);
Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/94ec5a11
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/94ec5a11
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/94ec5a11
Branch: refs/heads/master
Commit: 94ec5a11122b2e60fbffcc35373c978c839bf8ae
Parents: 00fafe7
Author: Loic Guibert <lf...@yahoo.fr>
Authored: Mon Oct 5 17:00:50 2015 +0400
Committer: Loic Guibert <lf...@yahoo.fr>
Committed: Mon Oct 5 17:00:50 2015 +0400
----------------------------------------------------------------------
.../org/apache/commons/lang3/StringUtils.java | 176 ++++++++++++++++++-
.../lang3/StringUtilsEqualsIndexOfTest.java | 69 ++++++++
2 files changed, 244 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/94ec5a11/src/main/java/org/apache/commons/lang3/StringUtils.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java
index 3f0314f..cb39120 100644
--- a/src/main/java/org/apache/commons/lang3/StringUtils.java
+++ b/src/main/java/org/apache/commons/lang3/StringUtils.java
@@ -35,7 +35,7 @@ import java.util.regex.Pattern;
* - checks if a String contains text</li>
* <li><b>Trim/Strip</b>
* - removes leading and trailing whitespace</li>
- * <li><b>Equals</b>
+ * <li><b>Equals/Compare</b>
* - compares two strings null-safe</li>
* <li><b>startsWith</b>
* - check if a String starts with a prefix null-safe</li>
@@ -830,6 +830,180 @@ public class StringUtils {
}
}
+ // Compare
+ //-----------------------------------------------------------------------
+ /**
+ * <p>Compare two Strings lexicographically, as per {@link String#compareTo(String)}, returning :</p>
+ * <ul>
+ * <li>{@code int = 0}, if {@code str1} is equal to {@code str2} (or both {@code null})</li>
+ * <li>{@code int < 0}, if {@code str1} is less than {@code str2}</li>
+ * <li>{@code int > 0}, if {@code str1} is greater than {@code str2}</li>
+ * </ul>
+ *
+ * <p>This is a {@code null} safe version of :</p>
+ * <blockquote><pre>str1.compareTo(str2)</pre></blockquote>
+ *
+ * <p>{@code null} value is considered less than non-{@code null} value.
+ * Two {@code null} references are considered equal.</p>
+ *
+ * <pre>
+ * StringUtils.compare(null, null) = 0
+ * StringUtils.compare(null , "a") < 0
+ * StringUtils.compare("a", null) > 0
+ * StringUtils.compare("abc", "abc") = 0
+ * StringUtils.compare("a", "b") < 0
+ * StringUtils.compare("b", "a") > 0
+ * StringUtils.compare("a", "B") > 0
+ * StringUtils.compare("ab", "abc") < 0
+ * </pre>
+ *
+ * @see #compare(String, String, boolean)
+ * @see String#compareTo(String)
+ * @param str1 the String to compare from
+ * @param str2 the String to compare to
+ * @return < 0, 0, > 0, if {@code str1} is respectively less, equal ou greater than {@code str2}
+ */
+ public static int compare(final String str1, final String str2) {
+ return compare(str1, str2, true);
+ }
+
+ /**
+ * <p>Compare two Strings lexicographically, as per {@link String#compareTo(String)}, returning :</p>
+ * <ul>
+ * <li>{@code int = 0}, if {@code str1} is equal to {@code str2} (or both {@code null})</li>
+ * <li>{@code int < 0}, if {@code str1} is less than {@code str2}</li>
+ * <li>{@code int > 0}, if {@code str1} is greater than {@code str2}</li>
+ * </ul>
+ *
+ * <p>This is a {@code null} safe version of :</p>
+ * <blockquote><pre>str1.compareTo(str2)</pre></blockquote>
+ *
+ * <p>{@code null} inputs are handled according to the {@code nullIsLess} parameter.
+ * Two {@code null} references are considered equal.</p>
+ *
+ * <pre>
+ * StringUtils.compare(null, null, *) = 0
+ * StringUtils.compare(null , "a", true) < 0
+ * StringUtils.compare(null , "a", false) > 0
+ * StringUtils.compare("a", null, true) > 0
+ * StringUtils.compare("a", null, false) < 0
+ * StringUtils.compare("abc", "abc", *) = 0
+ * StringUtils.compare("a", "b", *) < 0
+ * StringUtils.compare("b", "a", *) > 0
+ * StringUtils.compare("a", "B", *) > 0
+ * StringUtils.compare("ab", "abc", *) < 0
+ * </pre>
+ *
+ * @see String#compareTo(String)
+ * @param str1 the String to compare from
+ * @param str2 the String to compare to
+ * @param nullIsLess whether consider {@code null} value less than non-{@code null} value
+ * @return < 0, 0, > 0, if {@code str1} is respectively less, equal ou greater than {@code str2}
+ */
+ public static int compare(final String str1, final String str2, final boolean nullIsLess) {
+ if (str1 == str2) {
+ return 0;
+ }
+ if (str1 == null) {
+ return nullIsLess ? -1 : 1;
+ }
+ if (str2 == null) {
+ return nullIsLess ? 1 : - 1;
+ }
+ return str1.compareTo(str2);
+ }
+
+ /**
+ * <p>Compare two Strings lexicographically, ignoring case differences,
+ * as per {@link String#compareToIgnoreCase(String)}, returning :</p>
+ * <ul>
+ * <li>{@code int = 0}, if {@code str1} is equal to {@code str2} (or both {@code null})</li>
+ * <li>{@code int < 0}, if {@code str1} is less than {@code str2}</li>
+ * <li>{@code int > 0}, if {@code str1} is greater than {@code str2}</li>
+ * </ul>
+ *
+ * <p>This is a {@code null} safe version of :</p>
+ * <blockquote><pre>str1.compareToIgnoreCase(str2)</pre></blockquote>
+ *
+ * <p>{@code null} value is considered less than non-{@code null} value.
+ * Two {@code null} references are considered equal.
+ * Comparison is case insensitive.</p>
+ *
+ * <pre>
+ * StringUtils.compareIgnoreCase(null, null) = 0
+ * StringUtils.compareIgnoreCase(null , "a") < 0
+ * StringUtils.compareIgnoreCase("a", null) > 0
+ * StringUtils.compareIgnoreCase("abc", "abc") = 0
+ * StringUtils.compareIgnoreCase("abc", "ABC") = 0
+ * StringUtils.compareIgnoreCase("a", "b") < 0
+ * StringUtils.compareIgnoreCase("b", "a") > 0
+ * StringUtils.compareIgnoreCase("a", "B") < 0
+ * StringUtils.compareIgnoreCase("A", "b") < 0
+ * StringUtils.compareIgnoreCase("ab", "ABC") < 0
+ * </pre>
+ *
+ * @see #compareIgnoreCase(String, String, boolean)
+ * @see String#compareToIgnoreCase(String)
+ * @param str1 the String to compare from
+ * @param str2 the String to compare to
+ * @return < 0, 0, > 0, if {@code str1} is respectively less, equal ou greater than {@code str2},
+ * ignoring case differences.
+ */
+ public static int compareIgnoreCase(final String str1, final String str2) {
+ return compareIgnoreCase(str1, str2, true);
+ }
+
+ /**
+ * <p>Compare two Strings lexicographically, ignoring case differences,
+ * as per {@link String#compareToIgnoreCase(String)}, returning :</p>
+ * <ul>
+ * <li>{@code int = 0}, if {@code str1} is equal to {@code str2} (or both {@code null})</li>
+ * <li>{@code int < 0}, if {@code str1} is less than {@code str2}</li>
+ * <li>{@code int > 0}, if {@code str1} is greater than {@code str2}</li>
+ * </ul>
+ *
+ * <p>This is a {@code null} safe version of :</p>
+ * <blockquote><pre>str1.compareToIgnoreCase(str2)</pre></blockquote>
+ *
+ * <p>{@code null} inputs are handled according to the {@code nullIsLess} parameter.
+ * Two {@code null} references are considered equal.
+ * Comparison is case insensitive.</p>
+ *
+ * <pre>
+ * StringUtils.compareIgnoreCase(null, null, *) = 0
+ * StringUtils.compareIgnoreCase(null , "a", true) < 0
+ * StringUtils.compareIgnoreCase(null , "a", false) > 0
+ * StringUtils.compareIgnoreCase("a", null, true) > 0
+ * StringUtils.compareIgnoreCase("a", null, false) < 0
+ * StringUtils.compareIgnoreCase("abc", "abc", *) = 0
+ * StringUtils.compareIgnoreCase("abc", "ABC", *) = 0
+ * StringUtils.compareIgnoreCase("a", "b", *) < 0
+ * StringUtils.compareIgnoreCase("b", "a", *) > 0
+ * StringUtils.compareIgnoreCase("a", "B", *) < 0
+ * StringUtils.compareIgnoreCase("A", "b", *) < 0
+ * StringUtils.compareIgnoreCase("ab", "abc", *) < 0
+ * </pre>
+ *
+ * @see String#compareToIgnoreCase(String)
+ * @param str1 the String to compare from
+ * @param str2 the String to compare to
+ * @param nullIsLess whether consider {@code null} value less than non-{@code null} value
+ * @return < 0, 0, > 0, if {@code str1} is respectively less, equal ou greater than {@code str2},
+ * ignoring case differences.
+ */
+ public static int compareIgnoreCase(final String str1, final String str2, final boolean nullIsLess) {
+ if (str1 == str2) {
+ return 0;
+ }
+ if (str1 == null) {
+ return nullIsLess ? -1 : 1;
+ }
+ if (str2 == null) {
+ return nullIsLess ? 1 : - 1;
+ }
+ return str1.compareToIgnoreCase(str2);
+ }
+
// IndexOf
//-----------------------------------------------------------------------
/**
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/94ec5a11/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java
index d2b6418..b45a5f6 100644
--- a/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java
+++ b/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java
@@ -578,6 +578,75 @@ public class StringUtilsEqualsIndexOfTest {
//-----------------------------------------------------------------------
@Test
+ public void testCompare_StringString() {
+ assertTrue(StringUtils.compare(null, null) == 0);
+ assertTrue(StringUtils.compare(null, "a") < 0);
+ assertTrue(StringUtils.compare("a", null) > 0);
+ assertTrue(StringUtils.compare("abc", "abc") == 0);
+ assertTrue(StringUtils.compare("a", "b") < 0);
+ assertTrue(StringUtils.compare("b", "a") > 0);
+ assertTrue(StringUtils.compare("a", "B") > 0);
+ assertTrue(StringUtils.compare("abc", "abd") < 0);
+ assertTrue(StringUtils.compare("ab", "abc") < 0);
+ assertTrue(StringUtils.compare("ab", "ab ") < 0);
+ assertTrue(StringUtils.compare("abc", "ab ") > 0);
+ }
+
+ @Test
+ public void testCompare_StringStringBoolean() {
+ assertTrue(StringUtils.compare(null, null, false) == 0);
+ assertTrue(StringUtils.compare(null, "a", true) < 0);
+ assertTrue(StringUtils.compare(null, "a", false) > 0);
+ assertTrue(StringUtils.compare("a", null, true) > 0);
+ assertTrue(StringUtils.compare("a", null, false) < 0);
+ assertTrue(StringUtils.compare("abc", "abc", false) == 0);
+ assertTrue(StringUtils.compare("a", "b", false) < 0);
+ assertTrue(StringUtils.compare("b", "a", false) > 0);
+ assertTrue(StringUtils.compare("a", "B", false) > 0);
+ assertTrue(StringUtils.compare("abc", "abd", false) < 0);
+ assertTrue(StringUtils.compare("ab", "abc", false) < 0);
+ assertTrue(StringUtils.compare("ab", "ab ", false) < 0);
+ assertTrue(StringUtils.compare("abc", "ab ", false) > 0);
+ }
+
+ @Test
+ public void testCompareIgnoreCase_StringString() {
+ assertTrue(StringUtils.compareIgnoreCase(null, null) == 0);
+ assertTrue(StringUtils.compareIgnoreCase(null, "a") < 0);
+ assertTrue(StringUtils.compareIgnoreCase("a", null) > 0);
+ assertTrue(StringUtils.compareIgnoreCase("abc", "abc") == 0);
+ assertTrue(StringUtils.compareIgnoreCase("abc", "ABC") == 0);
+ assertTrue(StringUtils.compareIgnoreCase("a", "b") < 0);
+ assertTrue(StringUtils.compareIgnoreCase("b", "a") > 0);
+ assertTrue(StringUtils.compareIgnoreCase("a", "B") < 0);
+ assertTrue(StringUtils.compareIgnoreCase("A", "b") < 0);
+ assertTrue(StringUtils.compareIgnoreCase("abc", "ABD") < 0);
+ assertTrue(StringUtils.compareIgnoreCase("ab", "ABC") < 0);
+ assertTrue(StringUtils.compareIgnoreCase("ab", "AB ") < 0);
+ assertTrue(StringUtils.compareIgnoreCase("abc", "AB ") > 0);
+ }
+
+ @Test
+ public void testCompareIgnoreCase_StringStringBoolean() {
+ assertTrue(StringUtils.compareIgnoreCase(null, null, false) == 0);
+ assertTrue(StringUtils.compareIgnoreCase(null, "a", true) < 0);
+ assertTrue(StringUtils.compareIgnoreCase(null, "a", false) > 0);
+ assertTrue(StringUtils.compareIgnoreCase("a", null, true) > 0);
+ assertTrue(StringUtils.compareIgnoreCase("a", null, false) < 0);
+ assertTrue(StringUtils.compareIgnoreCase("abc", "abc", false) == 0);
+ assertTrue(StringUtils.compareIgnoreCase("abc", "ABC", false) == 0);
+ assertTrue(StringUtils.compareIgnoreCase("a", "b", false) < 0);
+ assertTrue(StringUtils.compareIgnoreCase("b", "a", false) > 0);
+ assertTrue(StringUtils.compareIgnoreCase("a", "B", false) < 0);
+ assertTrue(StringUtils.compareIgnoreCase("A", "b", false) < 0);
+ assertTrue(StringUtils.compareIgnoreCase("abc", "ABD", false) < 0);
+ assertTrue(StringUtils.compareIgnoreCase("ab", "ABC", false) < 0);
+ assertTrue(StringUtils.compareIgnoreCase("ab", "AB ", false) < 0);
+ assertTrue(StringUtils.compareIgnoreCase("abc", "AB ", false) > 0);
+ }
+
+ //-----------------------------------------------------------------------
+ @Test
public void testIndexOf_char() {
assertEquals(-1, StringUtils.indexOf(null, ' '));
assertEquals(-1, StringUtils.indexOf("", ' '));
[3/4] [lang] LANG-1171 Remove log of excluded methods in
StringUtilsTest.testStringUtilsCharSequenceContract()
Posted by lg...@apache.org.
LANG-1171 Remove log of excluded methods in StringUtilsTest.testStringUtilsCharSequenceContract()
Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/51512905
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/51512905
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/51512905
Branch: refs/heads/master
Commit: 51512905c31ff19b91fc67b2cb3ddb5b1fa0fd51
Parents: 131917a
Author: Loic Guibert <lg...@apache.org>
Authored: Wed Oct 21 15:15:06 2015 +0400
Committer: Loic Guibert <lg...@apache.org>
Committed: Wed Oct 21 15:15:06 2015 +0400
----------------------------------------------------------------------
src/test/java/org/apache/commons/lang3/StringUtilsTest.java | 8 ++------
1 file changed, 2 insertions(+), 6 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/51512905/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
index a57a7f5..c49ee6a 100644
--- a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
@@ -2390,9 +2390,7 @@ public class StringUtilsTest {
// don't actively test for that.
final Class<?>[] params = m.getParameterTypes();
if (params.length > 0 && (params[0] == CharSequence.class || params[0] == CharSequence[].class)) {
- if (ArrayUtils.contains(excludeMethods, methodStr)) {
- System.out.println("The mutable method \"" + methodStr + "\" is expressly excluded from testStringUtilsCharSequenceContract()");
- } else {
+ if (!ArrayUtils.contains(excludeMethods, methodStr)) {
fail("The method \"" + methodStr + "\" appears to be mutable in spirit and therefore must not accept a CharSequence");
}
}
@@ -2401,9 +2399,7 @@ public class StringUtilsTest {
// As above, it may be something other than CharSequence.
final Class<?>[] params = m.getParameterTypes();
if (params.length > 0 && (params[0] == String.class || params[0] == String[].class)) {
- if (ArrayUtils.contains(excludeMethods, methodStr)) {
- System.out.println("The immutable method \"" + methodStr + "\" is expressly excluded from testStringUtilsCharSequenceContract()");
- } else {
+ if (!ArrayUtils.contains(excludeMethods, methodStr)) {
fail("The method \"" + methodStr + "\" appears to be immutable in spirit and therefore must not accept a String");
}
}
[4/4] [lang] Merge branch 'fix-LANG-1171'
Posted by lg...@apache.org.
Merge branch 'fix-LANG-1171'
LANG-1171: Add compare methods in StringUtils
This closes #110 from github.
Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/849578d3
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/849578d3
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/849578d3
Branch: refs/heads/master
Commit: 849578d3a8ef50eea1c5bfcd63f78779d30c1550
Parents: ae86519 5151290
Author: Loic Guibert <lg...@apache.org>
Authored: Fri Oct 23 21:53:32 2015 +0400
Committer: Loic Guibert <lg...@apache.org>
Committed: Fri Oct 23 21:53:32 2015 +0400
----------------------------------------------------------------------
src/changes/changes.xml | 1 +
.../org/apache/commons/lang3/StringUtils.java | 180 ++++++++++++++++++-
.../lang3/StringUtilsEqualsIndexOfTest.java | 69 +++++++
.../apache/commons/lang3/StringUtilsTest.java | 17 +-
4 files changed, 264 insertions(+), 3 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/849578d3/src/changes/changes.xml
----------------------------------------------------------------------
diff --cc src/changes/changes.xml
index a2a4c26,ae6ed76..165f70d
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@@ -22,8 -22,7 +22,9 @@@
<body>
<release version="3.5" date="tba" description="tba">
- <action issue="LANG-1057" type="update" dev="chas" dute-to="Otávio Santana">Replace StringBuilder with String concatenation for better optimization</action>
++ <action issue="LANG-1171" type="add" dev="lguibert">Add compare methods in StringUtils</action>
+ <action issue="LANG-1174" type="add" dev="britter" due-to="Punkratz312">Add sugar to RandomUtils</action>
+ <action issue="LANG-1057" type="update" dev="chas" due-to="Otávio Santana">Replace StringBuilder with String concatenation for better optimization</action>
<action issue="LANG-1075" type="update" dev="chas">Deprecate SystemUtils.FILE_SEPARATOR and SystemUtils.PATH_SEPARATOR</action>
<action issue="LANG-1154" type="add" dev="chas" due-to="Gary Gregory">FastDateFormat APIs that use a StringBuilder</action>
<action issue="LANG-1149" type="add" dev="chas" due-to="Gregory Zak">Ability to throw checked exceptions without declaring them</action>
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/849578d3/src/main/java/org/apache/commons/lang3/StringUtils.java
----------------------------------------------------------------------
diff --cc src/main/java/org/apache/commons/lang3/StringUtils.java
index 3f0314f,cb39120..e109714
--- a/src/main/java/org/apache/commons/lang3/StringUtils.java
+++ b/src/main/java/org/apache/commons/lang3/StringUtils.java
@@@ -830,6 -830,180 +830,184 @@@ public class StringUtils
}
}
+ // Compare
+ //-----------------------------------------------------------------------
+ /**
+ * <p>Compare two Strings lexicographically, as per {@link String#compareTo(String)}, returning :</p>
+ * <ul>
+ * <li>{@code int = 0}, if {@code str1} is equal to {@code str2} (or both {@code null})</li>
+ * <li>{@code int < 0}, if {@code str1} is less than {@code str2}</li>
+ * <li>{@code int > 0}, if {@code str1} is greater than {@code str2}</li>
+ * </ul>
+ *
+ * <p>This is a {@code null} safe version of :</p>
+ * <blockquote><pre>str1.compareTo(str2)</pre></blockquote>
+ *
+ * <p>{@code null} value is considered less than non-{@code null} value.
+ * Two {@code null} references are considered equal.</p>
+ *
+ * <pre>
+ * StringUtils.compare(null, null) = 0
+ * StringUtils.compare(null , "a") < 0
+ * StringUtils.compare("a", null) > 0
+ * StringUtils.compare("abc", "abc") = 0
+ * StringUtils.compare("a", "b") < 0
+ * StringUtils.compare("b", "a") > 0
+ * StringUtils.compare("a", "B") > 0
+ * StringUtils.compare("ab", "abc") < 0
+ * </pre>
+ *
+ * @see #compare(String, String, boolean)
+ * @see String#compareTo(String)
+ * @param str1 the String to compare from
+ * @param str2 the String to compare to
+ * @return < 0, 0, > 0, if {@code str1} is respectively less, equal ou greater than {@code str2}
++ * @since 3.5
+ */
+ public static int compare(final String str1, final String str2) {
+ return compare(str1, str2, true);
+ }
+
+ /**
+ * <p>Compare two Strings lexicographically, as per {@link String#compareTo(String)}, returning :</p>
+ * <ul>
+ * <li>{@code int = 0}, if {@code str1} is equal to {@code str2} (or both {@code null})</li>
+ * <li>{@code int < 0}, if {@code str1} is less than {@code str2}</li>
+ * <li>{@code int > 0}, if {@code str1} is greater than {@code str2}</li>
+ * </ul>
+ *
+ * <p>This is a {@code null} safe version of :</p>
+ * <blockquote><pre>str1.compareTo(str2)</pre></blockquote>
+ *
+ * <p>{@code null} inputs are handled according to the {@code nullIsLess} parameter.
+ * Two {@code null} references are considered equal.</p>
+ *
+ * <pre>
+ * StringUtils.compare(null, null, *) = 0
+ * StringUtils.compare(null , "a", true) < 0
+ * StringUtils.compare(null , "a", false) > 0
+ * StringUtils.compare("a", null, true) > 0
+ * StringUtils.compare("a", null, false) < 0
+ * StringUtils.compare("abc", "abc", *) = 0
+ * StringUtils.compare("a", "b", *) < 0
+ * StringUtils.compare("b", "a", *) > 0
+ * StringUtils.compare("a", "B", *) > 0
+ * StringUtils.compare("ab", "abc", *) < 0
+ * </pre>
+ *
+ * @see String#compareTo(String)
+ * @param str1 the String to compare from
+ * @param str2 the String to compare to
+ * @param nullIsLess whether consider {@code null} value less than non-{@code null} value
+ * @return < 0, 0, > 0, if {@code str1} is respectively less, equal ou greater than {@code str2}
++ * @since 3.5
+ */
+ public static int compare(final String str1, final String str2, final boolean nullIsLess) {
+ if (str1 == str2) {
+ return 0;
+ }
+ if (str1 == null) {
+ return nullIsLess ? -1 : 1;
+ }
+ if (str2 == null) {
+ return nullIsLess ? 1 : - 1;
+ }
+ return str1.compareTo(str2);
+ }
+
+ /**
+ * <p>Compare two Strings lexicographically, ignoring case differences,
+ * as per {@link String#compareToIgnoreCase(String)}, returning :</p>
+ * <ul>
+ * <li>{@code int = 0}, if {@code str1} is equal to {@code str2} (or both {@code null})</li>
+ * <li>{@code int < 0}, if {@code str1} is less than {@code str2}</li>
+ * <li>{@code int > 0}, if {@code str1} is greater than {@code str2}</li>
+ * </ul>
+ *
+ * <p>This is a {@code null} safe version of :</p>
+ * <blockquote><pre>str1.compareToIgnoreCase(str2)</pre></blockquote>
+ *
+ * <p>{@code null} value is considered less than non-{@code null} value.
+ * Two {@code null} references are considered equal.
+ * Comparison is case insensitive.</p>
+ *
+ * <pre>
+ * StringUtils.compareIgnoreCase(null, null) = 0
+ * StringUtils.compareIgnoreCase(null , "a") < 0
+ * StringUtils.compareIgnoreCase("a", null) > 0
+ * StringUtils.compareIgnoreCase("abc", "abc") = 0
+ * StringUtils.compareIgnoreCase("abc", "ABC") = 0
+ * StringUtils.compareIgnoreCase("a", "b") < 0
+ * StringUtils.compareIgnoreCase("b", "a") > 0
+ * StringUtils.compareIgnoreCase("a", "B") < 0
+ * StringUtils.compareIgnoreCase("A", "b") < 0
+ * StringUtils.compareIgnoreCase("ab", "ABC") < 0
+ * </pre>
+ *
+ * @see #compareIgnoreCase(String, String, boolean)
+ * @see String#compareToIgnoreCase(String)
+ * @param str1 the String to compare from
+ * @param str2 the String to compare to
+ * @return < 0, 0, > 0, if {@code str1} is respectively less, equal ou greater than {@code str2},
+ * ignoring case differences.
++ * @since 3.5
+ */
+ public static int compareIgnoreCase(final String str1, final String str2) {
+ return compareIgnoreCase(str1, str2, true);
+ }
+
+ /**
+ * <p>Compare two Strings lexicographically, ignoring case differences,
+ * as per {@link String#compareToIgnoreCase(String)}, returning :</p>
+ * <ul>
+ * <li>{@code int = 0}, if {@code str1} is equal to {@code str2} (or both {@code null})</li>
+ * <li>{@code int < 0}, if {@code str1} is less than {@code str2}</li>
+ * <li>{@code int > 0}, if {@code str1} is greater than {@code str2}</li>
+ * </ul>
+ *
+ * <p>This is a {@code null} safe version of :</p>
+ * <blockquote><pre>str1.compareToIgnoreCase(str2)</pre></blockquote>
+ *
+ * <p>{@code null} inputs are handled according to the {@code nullIsLess} parameter.
+ * Two {@code null} references are considered equal.
+ * Comparison is case insensitive.</p>
+ *
+ * <pre>
+ * StringUtils.compareIgnoreCase(null, null, *) = 0
+ * StringUtils.compareIgnoreCase(null , "a", true) < 0
+ * StringUtils.compareIgnoreCase(null , "a", false) > 0
+ * StringUtils.compareIgnoreCase("a", null, true) > 0
+ * StringUtils.compareIgnoreCase("a", null, false) < 0
+ * StringUtils.compareIgnoreCase("abc", "abc", *) = 0
+ * StringUtils.compareIgnoreCase("abc", "ABC", *) = 0
+ * StringUtils.compareIgnoreCase("a", "b", *) < 0
+ * StringUtils.compareIgnoreCase("b", "a", *) > 0
+ * StringUtils.compareIgnoreCase("a", "B", *) < 0
+ * StringUtils.compareIgnoreCase("A", "b", *) < 0
+ * StringUtils.compareIgnoreCase("ab", "abc", *) < 0
+ * </pre>
+ *
+ * @see String#compareToIgnoreCase(String)
+ * @param str1 the String to compare from
+ * @param str2 the String to compare to
+ * @param nullIsLess whether consider {@code null} value less than non-{@code null} value
+ * @return < 0, 0, > 0, if {@code str1} is respectively less, equal ou greater than {@code str2},
+ * ignoring case differences.
++ * @since 3.5
+ */
+ public static int compareIgnoreCase(final String str1, final String str2, final boolean nullIsLess) {
+ if (str1 == str2) {
+ return 0;
+ }
+ if (str1 == null) {
+ return nullIsLess ? -1 : 1;
+ }
+ if (str2 == null) {
+ return nullIsLess ? 1 : - 1;
+ }
+ return str1.compareToIgnoreCase(str2);
+ }
+
// IndexOf
//-----------------------------------------------------------------------
/**
[2/4] [lang] LANG-1171 Exclude methods from
StringUtilsTest.testStringUtilsCharSequenceContract() : -
StringUtils.compare(String str1, String str2);
- StringUtils.compare(String str1, String str2, boolean nullIsLess);
- StringUtils.compareIgnoreCas
Posted by lg...@apache.org.
LANG-1171 Exclude methods from StringUtilsTest.testStringUtilsCharSequenceContract() :
- StringUtils.compare(String str1, String str2);
- StringUtils.compare(String str1, String str2, boolean nullIsLess);
- StringUtils.compareIgnoreCase(String str1, String str2);
- StringUtils.compareIgnoreCase(String str1, String str2, boolean nullIsLess);
Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/131917a0
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/131917a0
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/131917a0
Branch: refs/heads/master
Commit: 131917a0d3303ca2c38fd1d6765b9bed2c23ff89
Parents: 94ec5a1
Author: Loic Guibert <lg...@apache.org>
Authored: Tue Oct 20 18:50:13 2015 +0400
Committer: Loic Guibert <lg...@apache.org>
Committed: Tue Oct 20 18:50:13 2015 +0400
----------------------------------------------------------------------
.../apache/commons/lang3/StringUtilsTest.java | 21 ++++++++++++++++++--
1 file changed, 19 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/131917a0/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
index 5cc665a..a57a7f5 100644
--- a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
@@ -2373,22 +2373,39 @@ public class StringUtilsTest {
@Test
public void testStringUtilsCharSequenceContract() {
final Class<StringUtils> c = StringUtils.class;
+ // Methods that are expressly excluded from testStringUtilsCharSequenceContract()
+ final String[] excludeMethods = {
+ "public static int org.apache.commons.lang3.StringUtils.compare(java.lang.String,java.lang.String)",
+ "public static int org.apache.commons.lang3.StringUtils.compare(java.lang.String,java.lang.String,boolean)",
+ "public static int org.apache.commons.lang3.StringUtils.compareIgnoreCase(java.lang.String,java.lang.String)",
+ "public static int org.apache.commons.lang3.StringUtils.compareIgnoreCase(java.lang.String,java.lang.String,boolean)"
+ };
final Method[] methods = c.getMethods();
+
for (final Method m : methods) {
+ String methodStr = m.toString();
if (m.getReturnType() == String.class || m.getReturnType() == String[].class) {
// Assume this is mutable and ensure the first parameter is not CharSequence.
// It may be String or it may be something else (String[], Object, Object[]) so
// don't actively test for that.
final Class<?>[] params = m.getParameterTypes();
if (params.length > 0 && (params[0] == CharSequence.class || params[0] == CharSequence[].class)) {
- fail("The method " + m + " appears to be mutable in spirit and therefore must not accept a CharSequence");
+ if (ArrayUtils.contains(excludeMethods, methodStr)) {
+ System.out.println("The mutable method \"" + methodStr + "\" is expressly excluded from testStringUtilsCharSequenceContract()");
+ } else {
+ fail("The method \"" + methodStr + "\" appears to be mutable in spirit and therefore must not accept a CharSequence");
+ }
}
} else {
// Assume this is immutable in spirit and ensure the first parameter is not String.
// As above, it may be something other than CharSequence.
final Class<?>[] params = m.getParameterTypes();
if (params.length > 0 && (params[0] == String.class || params[0] == String[].class)) {
- fail("The method " + m + " appears to be immutable in spirit and therefore must not accept a String");
+ if (ArrayUtils.contains(excludeMethods, methodStr)) {
+ System.out.println("The immutable method \"" + methodStr + "\" is expressly excluded from testStringUtilsCharSequenceContract()");
+ } else {
+ fail("The method \"" + methodStr + "\" appears to be immutable in spirit and therefore must not accept a String");
+ }
}
}
}