You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by GitBox <gi...@apache.org> on 2019/01/26 19:50:56 UTC

[GitHub] belingueres commented on a change in pull request #234: [MNG-6572] - use int or long instead of BigIntegers for little numbers in ComparableVersion

belingueres commented on a change in pull request #234: [MNG-6572] - use int or long instead of BigIntegers for little numbers in ComparableVersion
URL: https://github.com/apache/maven/pull/234#discussion_r251210661
 
 

 ##########
 File path: maven-artifact/src/main/java/org/apache/maven/artifact/versioning/ComparableVersion.java
 ##########
 @@ -82,48 +84,181 @@
     }
 
     /**
-     * Represents a numeric item in the version item list.
+     * Represents a numeric item in the version item list that can be represented with an int.
      */
-    private static class IntegerItem
+    private static class IntItem
         implements Item
     {
-        private static final BigInteger BIG_INTEGER_ZERO = new BigInteger( "0" );
+        private final int value;
 
-        private final BigInteger value;
+        public static final IntItem ZERO = new IntItem();
+
+        private IntItem()
+        {
+            this.value = 0;
+        }
+
+        IntItem( String str )
+        {
+            this.value = Integer.parseInt( str );
+        }
+
+        public int getType()
+        {
+            return INT_ITEM;
+        }
+
+        public boolean isNull()
+        {
+            return value == 0;
+        }
+
+        public int compareTo( Item item )
+        {
+            if ( item == null )
+            {
+                return ( value == 0 ) ? 0 : 1; // 1.0 == 1, 1.1 > 1
+            }
+
+            switch ( item.getType() )
+            {
+                case INT_ITEM:
+                {
+                    int itemValue = ( (IntItem) item ).value;
+                    return ( value < itemValue ) ? -1 : ( ( value == itemValue ) ? 0 : 1 );
+                }
+                case LONG_ITEM:
+                {
+                    long itemValue = ( (LongItem) item ).value;
+                    return ( value < itemValue ) ? -1 : ( ( value == itemValue ) ? 0 : 1 );
+                }
+                case BIGINTEGER_ITEM:
+                    return BigInteger.valueOf( value ).compareTo( ( (BigIntegerItem) item ).value );
+
+                case STRING_ITEM:
+                    return 1; // 1.1 > 1-sp
+
+                case LIST_ITEM:
+                    return 1; // 1.1 > 1-1
+
+                default:
+                    throw new RuntimeException( "invalid item: " + item.getClass() );
+            }
+        }
+
+        public String toString()
+        {
+            return Integer.toString( value );
+        }
+    }
+
+    /**
+     * Represents a numeric item in the version item list that can be represented with a long.
+     */
+    private static class LongItem
+        implements Item
+    {
+        private final long value;
+
+        LongItem( String str )
+        {
+            this.value = Long.parseLong( str );
+        }
+
+        public int getType()
+        {
+            return LONG_ITEM;
+        }
+
+        public boolean isNull()
+        {
+            return value == 0;
+        }
+
+        public int compareTo( Item item )
+        {
+            if ( item == null )
+            {
+                return ( value == 0 ) ? 0 : 1; // 1.0 == 1, 1.1 > 1
+            }
+
+            switch ( item.getType() )
+            {
+                case INT_ITEM:
+                {
+                    int itemValue = ( (IntItem) item ).value;
+                    return ( value < itemValue ) ? -1 : ( ( value == itemValue ) ? 0 : 1 );
+                }
+                case LONG_ITEM:
+                {
+                    long itemValue = ( (LongItem) item ).value;
+                    return ( value < itemValue ) ? -1 : ( ( value == itemValue ) ? 0 : 1 );
+                }
+                case BIGINTEGER_ITEM:
+                    return BigInteger.valueOf( value ).compareTo( ( (BigIntegerItem) item ).value );
+
+                case STRING_ITEM:
+                    return 1; // 1.1 > 1-sp
+
+                case LIST_ITEM:
+                    return 1; // 1.1 > 1-1
 
-        public static final IntegerItem ZERO = new IntegerItem();
+                default:
+                    throw new RuntimeException( "invalid item: " + item.getClass() );
 
 Review comment:
   should not reach there...IllegalStateException fits better?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services