You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Loic Guibert <lf...@yahoo.fr> on 2015/10/05 15:19:48 UTC

[LANG] Adding null safe compare methods in StringUtils

Hello,
I've implemented null safe methods to compare 2 Strings in StringUtils :
  - public static int compare(final String str1, final String str2);
  - public static int compare(final String str1, final String str2,
final boolean nullIsLess);
  - public static int compareIgnoreCase(final String str1, final String
str2);
  - public static int compareIgnoreCase(final String str1, final String
str2, final boolean nullIsLess);


I opened a JIRA ticket and an associated Pull Request to add those
methods in StringUtils :
  - https://issues.apache.org/jira/browse/LANG-1171
  - https://github.com/apache/commons-lang/pull/110


More details :
/**
 * <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}
 */
public static int compare(final String str1, final String str2);

/**
 * <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}
 */
public static int compare(final String str1, final String str2, final
boolean nullIsLess);

/**
 * <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.
 */
public static int compareIgnoreCase(final String str1, final String 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} 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.
 */
public static int compareIgnoreCase(final String str1, final String
str2, final boolean nullIsLess);


-- 
Loic Guibert
PGP : 0x65EB4F33

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: [LANG] Adding null safe compare methods in StringUtils

Posted by Benedikt Ritter <br...@apache.org>.
Hello Loic,

looks good to me, but you should remove the System.out.println calls.

Regards,
Benedikt

2015-10-20 17:28 GMT+02:00 Loic Guibert <lg...@apache.org>:

> I've updated my pull request if you want to look at what I plan to do
> concerning methods exclusion in
> StringUtilsTest.testStringUtilsCharSequenceContract()
>
> https://github.com/apache/commons-lang/pull/110
>
> https://github.com/apache/commons-lang/commit/131917a0d3303ca2c38fd1d6765b9bed2c23ff89
>
>
> Loic Guibert
> PGP : 0x65EB4F33
>
> Le 20/10/2015 17:24, Loic Guibert a écrit :
> > Hi Benedikt,
> > There are exactly the two options I was considering.
> >
> > If nobody objects, I'll go on the second way and exclude those methods in
> > StringUtilsTest.testStringUtilsCharSequenceContract().
> >
> > This is closer to Java conception where CharSequence are not comparable
> and
> > comparing two CharSequence of distinct nature
> > (like StringBuilder & CharBuffer for exemple)
> > may be undefined and can cause some problems
> > (like in CharBuffer where length method return remaining char
> > and not the total CharSequence length
> > or charAt(int index) that return the (current position + index)th char).
> >
> > Loic Guibert
> > PGP : 0x65EB4F33
> >
> >
> > Le 06/10/2015 23:07, Benedikt Ritter a écrit :
> >> Hi Loic,
> >>
> >> first of all, I like this proposal. Thank you for that!
> >>
> >> Regarding your question I see two options:
> >> - implement compare so that is accepts CharSequences as well. That would
> >> mean to implement a compare for CharSequences which compares two
> sequences
> >> character by character
> >> - exclude the compare methods
> >>
> >> Benedikt
> >>
> >> 2015-10-05 15:32 GMT+02:00 Loic Guibert <lf...@yahoo.fr>:
> >>
> >>> But i've a problem in this unit test :
> >>>
> >>>
> org.apache.commons.lang3.StringUtilsTest.testStringUtilsCharSequenceContract()
> >>>
> >>> It said :
> >>>
> >>> The method public static int
> >>> org.apache.commons.lang3.StringUtils.compare(String,String,boolean)
> appears
> >>> to be immutable in spirit and therefore must not accept a String
> >>>
> >>> I use String parameters instead of CharSequence because in the Java
> >>> conception, only String are Comparable and not CharSequence.
> >>> I call String#compareTo(String) in the implementation.
> >>>
> >>> So what should I do ?
> >>>
> >>> Loic Guibert
> >>> PGP : 0x65EB4F33
> >>>
> >>>
> >>>
> >>> Le 05/10/2015 17:19, Loic Guibert a écrit :
> >>>> Hello,
> >>>> I've implemented null safe methods to compare 2 Strings in
> StringUtils :
> >>>>   - public static int compare(final String str1, final String str2);
> >>>>   - public static int compare(final String str1, final String str2,
> >>>> final boolean nullIsLess);
> >>>>   - public static int compareIgnoreCase(final String str1, final
> String
> >>>> str2);
> >>>>   - public static int compareIgnoreCase(final String str1, final
> String
> >>>> str2, final boolean nullIsLess);
> >>>>
> >>>>
> >>>> I opened a JIRA ticket and an associated Pull Request to add those
> >>>> methods in StringUtils :
> >>>>   - https://issues.apache.org/jira/browse/LANG-1171
> >>>>   - https://github.com/apache/commons-lang/pull/110
> >>>>
> >>>>
> >>>> More details :
> >>>> /**
> >>>>  * <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}
> >>>>  */
> >>>> public static int compare(final String str1, final String str2);
> >>>>
> >>>> /**
> >>>>  * <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}
> >>>>  */
> >>>> public static int compare(final String str1, final String str2, final
> >>>> boolean nullIsLess);
> >>>>
> >>>> /**
> >>>>  * <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.
> >>>>  */
> >>>> public static int compareIgnoreCase(final String str1, final String
> >>> 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} 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.
> >>>>  */
> >>>> public static int compareIgnoreCase(final String str1, final String
> >>>> str2, final boolean nullIsLess);
> >>>>
> >>>>
> >>> ---------------------------------------------------------------------
> >>> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> >>> For additional commands, e-mail: dev-help@commons.apache.org
> >>>
> >>>
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> > For additional commands, e-mail: dev-help@commons.apache.org
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>
>


-- 
http://people.apache.org/~britter/
http://www.systemoutprintln.de/
http://twitter.com/BenediktRitter
http://github.com/britter

Re: [LANG] Adding null safe compare methods in StringUtils

Posted by Loic Guibert <lg...@apache.org>.
I've updated my pull request if you want to look at what I plan to do
concerning methods exclusion in
StringUtilsTest.testStringUtilsCharSequenceContract()

https://github.com/apache/commons-lang/pull/110
https://github.com/apache/commons-lang/commit/131917a0d3303ca2c38fd1d6765b9bed2c23ff89
 

Loic Guibert
PGP : 0x65EB4F33

Le 20/10/2015 17:24, Loic Guibert a écrit :
> Hi Benedikt,
> There are exactly the two options I was considering.
>
> If nobody objects, I'll go on the second way and exclude those methods in
> StringUtilsTest.testStringUtilsCharSequenceContract().
>
> This is closer to Java conception where CharSequence are not comparable and
> comparing two CharSequence of distinct nature
> (like StringBuilder & CharBuffer for exemple)
> may be undefined and can cause some problems
> (like in CharBuffer where length method return remaining char
> and not the total CharSequence length
> or charAt(int index) that return the (current position + index)th char).
>
> Loic Guibert
> PGP : 0x65EB4F33
>
>
> Le 06/10/2015 23:07, Benedikt Ritter a écrit :
>> Hi Loic,
>>
>> first of all, I like this proposal. Thank you for that!
>>
>> Regarding your question I see two options:
>> - implement compare so that is accepts CharSequences as well. That would
>> mean to implement a compare for CharSequences which compares two sequences
>> character by character
>> - exclude the compare methods
>>
>> Benedikt
>>
>> 2015-10-05 15:32 GMT+02:00 Loic Guibert <lf...@yahoo.fr>:
>>
>>> But i've a problem in this unit test :
>>>
>>> org.apache.commons.lang3.StringUtilsTest.testStringUtilsCharSequenceContract()
>>>
>>> It said :
>>>
>>> The method public static int
>>> org.apache.commons.lang3.StringUtils.compare(String,String,boolean) appears
>>> to be immutable in spirit and therefore must not accept a String
>>>
>>> I use String parameters instead of CharSequence because in the Java
>>> conception, only String are Comparable and not CharSequence.
>>> I call String#compareTo(String) in the implementation.
>>>
>>> So what should I do ?
>>>
>>> Loic Guibert
>>> PGP : 0x65EB4F33
>>>
>>>
>>>
>>> Le 05/10/2015 17:19, Loic Guibert a écrit :
>>>> Hello,
>>>> I've implemented null safe methods to compare 2 Strings in StringUtils :
>>>>   - public static int compare(final String str1, final String str2);
>>>>   - public static int compare(final String str1, final String str2,
>>>> final boolean nullIsLess);
>>>>   - public static int compareIgnoreCase(final String str1, final String
>>>> str2);
>>>>   - public static int compareIgnoreCase(final String str1, final String
>>>> str2, final boolean nullIsLess);
>>>>
>>>>
>>>> I opened a JIRA ticket and an associated Pull Request to add those
>>>> methods in StringUtils :
>>>>   - https://issues.apache.org/jira/browse/LANG-1171
>>>>   - https://github.com/apache/commons-lang/pull/110
>>>>
>>>>
>>>> More details :
>>>> /**
>>>>  * <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}
>>>>  */
>>>> public static int compare(final String str1, final String str2);
>>>>
>>>> /**
>>>>  * <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}
>>>>  */
>>>> public static int compare(final String str1, final String str2, final
>>>> boolean nullIsLess);
>>>>
>>>> /**
>>>>  * <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.
>>>>  */
>>>> public static int compareIgnoreCase(final String str1, final String
>>> 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} 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.
>>>>  */
>>>> public static int compareIgnoreCase(final String str1, final String
>>>> str2, final boolean nullIsLess);
>>>>
>>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
>>> For additional commands, e-mail: dev-help@commons.apache.org
>>>
>>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: [LANG] Adding null safe compare methods in StringUtils

Posted by Loic Guibert <lg...@apache.org>.
Hi Benedikt,
There are exactly the two options I was considering.

If nobody objects, I'll go on the second way and exclude those methods in
StringUtilsTest.testStringUtilsCharSequenceContract().

This is closer to Java conception where CharSequence are not comparable and
comparing two CharSequence of distinct nature
(like StringBuilder & CharBuffer for exemple)
may be undefined and can cause some problems
(like in CharBuffer where length method return remaining char
and not the total CharSequence length
or charAt(int index) that return the (current position + index)th char).

Loic Guibert
PGP : 0x65EB4F33


Le 06/10/2015 23:07, Benedikt Ritter a écrit :
> Hi Loic,
>
> first of all, I like this proposal. Thank you for that!
>
> Regarding your question I see two options:
> - implement compare so that is accepts CharSequences as well. That would
> mean to implement a compare for CharSequences which compares two sequences
> character by character
> - exclude the compare methods
>
> Benedikt
>
> 2015-10-05 15:32 GMT+02:00 Loic Guibert <lf...@yahoo.fr>:
>
>> But i've a problem in this unit test :
>>
>> org.apache.commons.lang3.StringUtilsTest.testStringUtilsCharSequenceContract()
>>
>> It said :
>>
>> The method public static int
>> org.apache.commons.lang3.StringUtils.compare(String,String,boolean) appears
>> to be immutable in spirit and therefore must not accept a String
>>
>> I use String parameters instead of CharSequence because in the Java
>> conception, only String are Comparable and not CharSequence.
>> I call String#compareTo(String) in the implementation.
>>
>> So what should I do ?
>>
>> Loic Guibert
>> PGP : 0x65EB4F33
>>
>>
>>
>> Le 05/10/2015 17:19, Loic Guibert a écrit :
>>> Hello,
>>> I've implemented null safe methods to compare 2 Strings in StringUtils :
>>>   - public static int compare(final String str1, final String str2);
>>>   - public static int compare(final String str1, final String str2,
>>> final boolean nullIsLess);
>>>   - public static int compareIgnoreCase(final String str1, final String
>>> str2);
>>>   - public static int compareIgnoreCase(final String str1, final String
>>> str2, final boolean nullIsLess);
>>>
>>>
>>> I opened a JIRA ticket and an associated Pull Request to add those
>>> methods in StringUtils :
>>>   - https://issues.apache.org/jira/browse/LANG-1171
>>>   - https://github.com/apache/commons-lang/pull/110
>>>
>>>
>>> More details :
>>> /**
>>>  * <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}
>>>  */
>>> public static int compare(final String str1, final String str2);
>>>
>>> /**
>>>  * <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}
>>>  */
>>> public static int compare(final String str1, final String str2, final
>>> boolean nullIsLess);
>>>
>>> /**
>>>  * <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.
>>>  */
>>> public static int compareIgnoreCase(final String str1, final String
>> 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} 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.
>>>  */
>>> public static int compareIgnoreCase(final String str1, final String
>>> str2, final boolean nullIsLess);
>>>
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
>> For additional commands, e-mail: dev-help@commons.apache.org
>>
>>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: [LANG] Adding null safe compare methods in StringUtils

Posted by Benedikt Ritter <br...@apache.org>.
Hi Loic,

first of all, I like this proposal. Thank you for that!

Regarding your question I see two options:
- implement compare so that is accepts CharSequences as well. That would
mean to implement a compare for CharSequences which compares two sequences
character by character
- exclude the compare methods

Benedikt

2015-10-05 15:32 GMT+02:00 Loic Guibert <lf...@yahoo.fr>:

> But i've a problem in this unit test :
>
> org.apache.commons.lang3.StringUtilsTest.testStringUtilsCharSequenceContract()
>
> It said :
>
> The method public static int
> org.apache.commons.lang3.StringUtils.compare(String,String,boolean) appears
> to be immutable in spirit and therefore must not accept a String
>
> I use String parameters instead of CharSequence because in the Java
> conception, only String are Comparable and not CharSequence.
> I call String#compareTo(String) in the implementation.
>
> So what should I do ?
>
> Loic Guibert
> PGP : 0x65EB4F33
>
>
>
> Le 05/10/2015 17:19, Loic Guibert a écrit :
> > Hello,
> > I've implemented null safe methods to compare 2 Strings in StringUtils :
> >   - public static int compare(final String str1, final String str2);
> >   - public static int compare(final String str1, final String str2,
> > final boolean nullIsLess);
> >   - public static int compareIgnoreCase(final String str1, final String
> > str2);
> >   - public static int compareIgnoreCase(final String str1, final String
> > str2, final boolean nullIsLess);
> >
> >
> > I opened a JIRA ticket and an associated Pull Request to add those
> > methods in StringUtils :
> >   - https://issues.apache.org/jira/browse/LANG-1171
> >   - https://github.com/apache/commons-lang/pull/110
> >
> >
> > More details :
> > /**
> >  * <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}
> >  */
> > public static int compare(final String str1, final String str2);
> >
> > /**
> >  * <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}
> >  */
> > public static int compare(final String str1, final String str2, final
> > boolean nullIsLess);
> >
> > /**
> >  * <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.
> >  */
> > public static int compareIgnoreCase(final String str1, final String
> 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} 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.
> >  */
> > public static int compareIgnoreCase(final String str1, final String
> > str2, final boolean nullIsLess);
> >
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>
>


-- 
http://people.apache.org/~britter/
http://www.systemoutprintln.de/
http://twitter.com/BenediktRitter
http://github.com/britter

Re: [LANG] Adding null safe compare methods in StringUtils

Posted by Loic Guibert <lf...@yahoo.fr>.
But i've a problem in this unit test :
org.apache.commons.lang3.StringUtilsTest.testStringUtilsCharSequenceContract()

It said :

The method public static int org.apache.commons.lang3.StringUtils.compare(String,String,boolean) appears to be immutable in spirit and therefore must not accept a String

I use String parameters instead of CharSequence because in the Java
conception, only String are Comparable and not CharSequence.
I call String#compareTo(String) in the implementation.

So what should I do ?

Loic Guibert
PGP : 0x65EB4F33



Le 05/10/2015 17:19, Loic Guibert a écrit :
> Hello,
> I've implemented null safe methods to compare 2 Strings in StringUtils :
>   - public static int compare(final String str1, final String str2);
>   - public static int compare(final String str1, final String str2,
> final boolean nullIsLess);
>   - public static int compareIgnoreCase(final String str1, final String
> str2);
>   - public static int compareIgnoreCase(final String str1, final String
> str2, final boolean nullIsLess);
>
>
> I opened a JIRA ticket and an associated Pull Request to add those
> methods in StringUtils :
>   - https://issues.apache.org/jira/browse/LANG-1171
>   - https://github.com/apache/commons-lang/pull/110
>
>
> More details :
> /**
>  * <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}
>  */
> public static int compare(final String str1, final String str2);
>
> /**
>  * <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}
>  */
> public static int compare(final String str1, final String str2, final
> boolean nullIsLess);
>
> /**
>  * <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.
>  */
> public static int compareIgnoreCase(final String str1, final String 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} 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.
>  */
> public static int compareIgnoreCase(final String str1, final String
> str2, final boolean nullIsLess);
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org