Return-Path: X-Original-To: apmail-commons-issues-archive@minotaur.apache.org Delivered-To: apmail-commons-issues-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 366F3189FB for ; Mon, 5 Oct 2015 12:53:36 +0000 (UTC) Received: (qmail 44360 invoked by uid 500); 5 Oct 2015 12:53:26 -0000 Delivered-To: apmail-commons-issues-archive@commons.apache.org Received: (qmail 44278 invoked by uid 500); 5 Oct 2015 12:53:26 -0000 Mailing-List: contact issues-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: issues@commons.apache.org Delivered-To: mailing list issues@commons.apache.org Received: (qmail 44259 invoked by uid 99); 5 Oct 2015 12:53:26 -0000 Received: from arcas.apache.org (HELO arcas.apache.org) (140.211.11.28) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 05 Oct 2015 12:53:26 +0000 Date: Mon, 5 Oct 2015 12:53:26 +0000 (UTC) From: "Loic Guibert (JIRA)" To: issues@commons.apache.org Message-ID: In-Reply-To: References: Subject: [jira] [Created] (LANG-1171) Add compare methods in StringUtils MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 Loic Guibert created LANG-1171: ---------------------------------- Summary: Add compare methods in StringUtils Key: LANG-1171 URL: https://issues.apache.org/jira/browse/LANG-1171 Project: Commons Lang Issue Type: New Feature Components: lang.* Reporter: Loic Guibert Add null safe methods in {{StringUtils}} to compare 2 Strings : {code:java} 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); {code} Those methods are null safe equivalent to : {code:java} String.compareTo(String); String.compareToIgnoreCase(String); {code} More details : {code:java} /** *

Compare two Strings lexicographically, as per {@link String#compareTo(String)}, returning :

*
    *
  • {@code int = 0}, if {@code str1} is equal to {@code str2} (or both {@code null})
  • *
  • {@code int < 0}, if {@code str1} is less than {@code str2}
  • *
  • {@code int > 0}, if {@code str1} is greater than {@code str2}
  • *
* *

This is a {@code null} safe version of :

*
str1.compareTo(str2)
* *

{@code null} value is considered less than non-{@code null} value. * Two {@code null} references are considered equal.

* *
 * 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
 * 
* * @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); /** *

Compare two Strings lexicographically, as per {@link String#compareTo(String)}, returning :

*
    *
  • {@code int = 0}, if {@code str1} is equal to {@code str2} (or both {@code null})
  • *
  • {@code int < 0}, if {@code str1} is less than {@code str2}
  • *
  • {@code int > 0}, if {@code str1} is greater than {@code str2}
  • *
* *

This is a {@code null} safe version of :

*
str1.compareTo(str2)
* *

{@code null} inputs are handled according to the {@code nullIsLess} parameter. * Two {@code null} references are considered equal.

* *
 * 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
 * 
* * @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); /** *

Compare two Strings lexicographically, ignoring case differences, * as per {@link String#compareToIgnoreCase(String)}, returning :

*
    *
  • {@code int = 0}, if {@code str1} is equal to {@code str2} (or both {@code null})
  • *
  • {@code int < 0}, if {@code str1} is less than {@code str2}
  • *
  • {@code int > 0}, if {@code str1} is greater than {@code str2}
  • *
* *

This is a {@code null} safe version of :

*
str1.compareToIgnoreCase(str2)
* *

{@code null} value is considered less than non-{@code null} value. * Two {@code null} references are considered equal. * Comparison is case insensitive.

* *
 * 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
 * 
* * @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); /** *

Compare two Strings lexicographically, ignoring case differences, * as per {@link String#compareToIgnoreCase(String)}, returning :

*
    *
  • {@code int = 0}, if {@code str1} is equal to {@code str2} (or both {@code null})
  • *
  • {@code int < 0}, if {@code str1} is less than {@code str2}
  • *
  • {@code int > 0}, if {@code str1} is greater than {@code str2}
  • *
* *

This is a {@code null} safe version of :

*
str1.compareToIgnoreCase(str2)
* *

{@code null} inputs are handled according to the {@code nullIsLess} parameter. * Two {@code null} references are considered equal. * Comparison is case insensitive.

* *
 * 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
 * 
* * @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); {code} I'm working on it and will send a pull request soon. -- This message was sent by Atlassian JIRA (v6.3.4#6332)