maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hbout...@apache.org
Subject svn commit: r1588924 - in /maven/shared/trunk/maven-shared-utils/src: main/java/org/apache/maven/shared/utils/StringUtils.java test/java/org/apache/maven/shared/utils/StringUtilsTest.java
Date Mon, 21 Apr 2014 16:49:36 GMT
Author: hboutemy
Date: Mon Apr 21 16:49:35 2014
New Revision: 1588924

URL: http://svn.apache.org/r1588924
Log:
[MSHARED-330] added StringUtils.endsWithIgnoreCase()

Modified:
    maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/StringUtils.java
    maven/shared/trunk/maven-shared-utils/src/test/java/org/apache/maven/shared/utils/StringUtilsTest.java

Modified: maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/StringUtils.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/StringUtils.java?rev=1588924&r1=1588923&r2=1588924&view=diff
==============================================================================
--- maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/StringUtils.java
(original)
+++ maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/StringUtils.java
Mon Apr 21 16:49:35 2014
@@ -2502,10 +2502,45 @@ public class StringUtils
      * @param searchStr the String to find, may be null
      * @return true if the String contains the search String,
      *         false if not or <code>null</code> string input
-     * 
      */
     public static boolean contains( @Nullable String str, @Nullable String searchStr )
     {
         return !( str == null || searchStr == null ) && str.contains( searchStr );
     }
+
+    /**
+     * <p>Checks if String ends with a search String, handling <code>null</code>.</p>
+     * <p/>
+     * <p>A <code>null</code> String will return <code>false</code>.</p>
+     * <p/>
+     * <pre>
+     * StringUtils.endsWithIgnoreCase(null, *)     = false
+     * StringUtils.endsWithIgnoreCase(*, null)     = false
+     * StringUtils.endsWithIgnoreCase("", "")      = true
+     * StringUtils.endsWithIgnoreCase("abc", "")   = true
+     * StringUtils.endsWithIgnoreCase("abc", "C")  = true
+     * StringUtils.endsWithIgnoreCase("abc", "a")  = false
+     * </pre>
+     *
+     * @param str       the String to check, may be null
+     * @param searchStr the String to find at end, may be null
+     * @return true if the String ends with the search String,
+     *         false if not or <code>null</code> string input
+     * 
+     */
+    public static boolean endsWithIgnoreCase( @Nullable String str, @Nullable String searchStr
)
+    {
+        if ( str == null || searchStr == null )
+        {
+            // for consistency with contains
+            return false;
+        }
+
+        if ( str.length() < searchStr.length() )
+        {
+            return false;
+        }
+
+        return str.regionMatches( true, str.length() - searchStr.length(), searchStr, 0,
searchStr.length() );
+    }
 }

Modified: maven/shared/trunk/maven-shared-utils/src/test/java/org/apache/maven/shared/utils/StringUtilsTest.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-utils/src/test/java/org/apache/maven/shared/utils/StringUtilsTest.java?rev=1588924&r1=1588923&r2=1588924&view=diff
==============================================================================
--- maven/shared/trunk/maven-shared-utils/src/test/java/org/apache/maven/shared/utils/StringUtilsTest.java
(original)
+++ maven/shared/trunk/maven-shared-utils/src/test/java/org/apache/maven/shared/utils/StringUtilsTest.java
Mon Apr 21 16:49:35 2014
@@ -550,6 +550,28 @@ public class StringUtilsTest
     }
 
     @Test
+    public void testEndsWithIgnoreCase()
+    {
+        assertThat( StringUtils.endsWithIgnoreCase( null, null )
+                , is( false ) );
+
+        assertThat( StringUtils.endsWithIgnoreCase( null, "string" )
+                , is( false ) );
+
+        assertThat( StringUtils.endsWithIgnoreCase( "string", null )
+                , is( false ) );
+
+        assertThat( StringUtils.endsWithIgnoreCase( "string", "ing" )
+                , is( true ) );
+
+        assertThat( StringUtils.endsWithIgnoreCase( "string", "a string" )
+                , is( false ) );
+
+        assertThat( StringUtils.endsWithIgnoreCase( "string", "str" )
+                , is( false ) );
+    }
+
+    @Test
     public void testEquals()
     {
         assertThat( StringUtils.equals( null, null )
@@ -2063,5 +2085,4 @@ public class StringUtilsTest
 
     }
 
-
 }



Mime
View raw message