commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lguib...@apache.org
Subject [4/4] [lang] Merge branch 'fix-LANG-1171'
Date Fri, 23 Oct 2015 17:56:17 GMT
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 <lguibert@apache.org>
Authored: Fri Oct 23 21:53:32 2015 +0400
Committer: Loic Guibert <lguibert@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")   &lt; 0
+      * StringUtils.compare("a", null)    &gt; 0
+      * StringUtils.compare("abc", "abc") = 0
+      * StringUtils.compare("a", "b")     &lt; 0
+      * StringUtils.compare("b", "a")     &gt; 0
+      * StringUtils.compare("a", "B")     &gt; 0
+      * StringUtils.compare("ab", "abc")  &lt; 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 &lt; 0, 0, &gt; 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)  &lt; 0
+      * StringUtils.compare(null , "a", false) &gt; 0
+      * StringUtils.compare("a", null, true)   &gt; 0
+      * StringUtils.compare("a", null, false)  &lt; 0
+      * StringUtils.compare("abc", "abc", *)   = 0
+      * StringUtils.compare("a", "b", *)       &lt; 0
+      * StringUtils.compare("b", "a", *)       &gt; 0
+      * StringUtils.compare("a", "B", *)       &gt; 0
+      * StringUtils.compare("ab", "abc", *)    &lt; 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 &lt; 0, 0, &gt; 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")   &lt; 0
+      * StringUtils.compareIgnoreCase("a", null)    &gt; 0
+      * StringUtils.compareIgnoreCase("abc", "abc") = 0
+      * StringUtils.compareIgnoreCase("abc", "ABC") = 0
+      * StringUtils.compareIgnoreCase("a", "b")     &lt; 0
+      * StringUtils.compareIgnoreCase("b", "a")     &gt; 0
+      * StringUtils.compareIgnoreCase("a", "B")     &lt; 0
+      * StringUtils.compareIgnoreCase("A", "b")     &lt; 0
+      * StringUtils.compareIgnoreCase("ab", "ABC")  &lt; 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 &lt; 0, 0, &gt; 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)  &lt; 0
+      * StringUtils.compareIgnoreCase(null , "a", false) &gt; 0
+      * StringUtils.compareIgnoreCase("a", null, true)   &gt; 0
+      * StringUtils.compareIgnoreCase("a", null, false)  &lt; 0
+      * StringUtils.compareIgnoreCase("abc", "abc", *)   = 0
+      * StringUtils.compareIgnoreCase("abc", "ABC", *)   = 0
+      * StringUtils.compareIgnoreCase("a", "b", *)       &lt; 0
+      * StringUtils.compareIgnoreCase("b", "a", *)       &gt; 0
+      * StringUtils.compareIgnoreCase("a", "B", *)       &lt; 0
+      * StringUtils.compareIgnoreCase("A", "b", *)       &lt; 0
+      * StringUtils.compareIgnoreCase("ab", "abc", *)    &lt; 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 &lt; 0, 0, &gt; 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
      //-----------------------------------------------------------------------
      /**


Mime
View raw message