You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by sl...@apache.org on 2019/04/15 13:48:28 UTC

[maven] branch MNG-6631 created (now e599840)

This is an automated email from the ASF dual-hosted git repository.

slachiewicz pushed a change to branch MNG-6631
in repository https://gitbox.apache.org/repos/asf/maven.git.


      at e599840  [MNG-6631] - Make DefaultArtifactVersion faster

This branch includes the following new commits:

     new e599840  [MNG-6631] - Make DefaultArtifactVersion faster

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[maven] 01/01: [MNG-6631] - Make DefaultArtifactVersion faster

Posted by sl...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

slachiewicz pushed a commit to branch MNG-6631
in repository https://gitbox.apache.org/repos/asf/maven.git

commit e5998400146459de4cd9a43b3e5e8fed27757fdd
Author: Stefan Oehme <st...@gmail.com>
AuthorDate: Tue Apr 9 15:49:56 2019 +0200

    [MNG-6631] - Make DefaultArtifactVersion faster
    
    Use if-statements instead of exception-based control flow.
    Throwing exceptions is very expensive and should not be used
    for normal flow.
---
 .../versioning/DefaultArtifactVersion.java         | 88 +++++++++++++---------
 1 file changed, 54 insertions(+), 34 deletions(-)

diff --git a/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/DefaultArtifactVersion.java b/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/DefaultArtifactVersion.java
index 81a52b8..0739098 100644
--- a/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/DefaultArtifactVersion.java
+++ b/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/DefaultArtifactVersion.java
@@ -20,8 +20,8 @@ package org.apache.maven.artifact.versioning;
  */
 
 import java.util.StringTokenizer;
-import java.util.regex.Pattern;
-import java.util.NoSuchElementException;
+
+import static org.apache.commons.lang3.math.NumberUtils.isDigits;
 
 /**
  * Default implementation of artifact versioning.
@@ -128,18 +128,15 @@ public class DefaultArtifactVersion
 
         if ( part2 != null )
         {
-            try
+            if ( part2.length() == 1  || !part2.startsWith( "0" ) )
             {
-                if ( ( part2.length() == 1 ) || !part2.startsWith( "0" ) )
-                {
-                    buildNumber = Integer.valueOf( part2 );
-                }
-                else
+                buildNumber = tryParseInt( part2 );
+                if ( buildNumber == null )
                 {
                     qualifier = part2;
                 }
             }
-            catch ( NumberFormatException e )
+            else
             {
                 qualifier = part2;
             }
@@ -147,11 +144,8 @@ public class DefaultArtifactVersion
 
         if ( ( !part1.contains( "." ) ) && !part1.startsWith( "0" ) )
         {
-            try
-            {
-                majorVersion = Integer.valueOf( part1 );
-            }
-            catch ( NumberFormatException e )
+            majorVersion = tryParseInt( part1 );
+            if ( majorVersion == null )
             {
                 // qualifier is the whole version, including "-"
                 qualifier = version;
@@ -163,30 +157,42 @@ public class DefaultArtifactVersion
             boolean fallback = false;
 
             StringTokenizer tok = new StringTokenizer( part1, "." );
-            try
+            if ( tok.hasMoreTokens() )
             {
                 majorVersion = getNextIntegerToken( tok );
-                if ( tok.hasMoreTokens() )
-                {
-                    minorVersion = getNextIntegerToken( tok );
-                }
-                if ( tok.hasMoreTokens() )
+                if ( majorVersion == null )
                 {
-                    incrementalVersion = getNextIntegerToken( tok );
+                    fallback = true;
                 }
-                if ( tok.hasMoreTokens() )
+            }
+            else
+            {
+                fallback = true;
+            }
+            if ( tok.hasMoreTokens() )
+            {
+                minorVersion = getNextIntegerToken( tok );
+                if ( minorVersion == null )
                 {
-                    qualifier = tok.nextToken();
-                    fallback = Pattern.compile( "\\d+" ).matcher( qualifier ).matches();
+                    fallback = true;
                 }
-
-                // string tokenizer won't detect these and ignores them
-                if ( part1.contains( ".." ) || part1.startsWith( "." ) || part1.endsWith( "." ) )
+            }
+            if ( tok.hasMoreTokens() )
+            {
+                incrementalVersion = getNextIntegerToken( tok );
+                if ( incrementalVersion == null )
                 {
                     fallback = true;
                 }
             }
-            catch ( NumberFormatException e )
+            if ( tok.hasMoreTokens() )
+            {
+                qualifier = tok.nextToken();
+                fallback = isDigits( qualifier );
+            }
+
+            // string tokenizer won't detect these and ignores them
+            if ( part1.contains( ".." ) || part1.startsWith( "." ) || part1.endsWith( "." ) )
             {
                 fallback = true;
             }
@@ -205,18 +211,32 @@ public class DefaultArtifactVersion
 
     private static Integer getNextIntegerToken( StringTokenizer tok )
     {
+        String s = tok.nextToken();
+        if ( ( s.length() > 1 ) && s.startsWith( "0" ) )
+        {
+            return null;
+        }
+        return tryParseInt( s );
+    }
+
+    private static Integer tryParseInt( String s )
+    {
         try
         {
-            String s = tok.nextToken();
-            if ( ( s.length() > 1 ) && s.startsWith( "0" ) )
+            if ( !isDigits( s ) )
+            {
+                return null;
+            }
+            long longValue = Long.parseLong( s );
+            if ( longValue > Integer.MAX_VALUE )
             {
-                throw new NumberFormatException( "Number part has a leading 0: '" + s + "'" );
+                return null;
             }
-            return Integer.valueOf( s );
+            return (int) longValue;
         }
-        catch ( NoSuchElementException e )
+        catch ( NumberFormatException e )
         {
-            throw new NumberFormatException( "Number is invalid" );
+            return null;
         }
     }