directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r1621895 - in /directory/shared/trunk/util/src/main/java/org/apache/directory/api/util: GeneralizedTime.java Strings.java
Date Mon, 01 Sep 2014 23:42:18 GMT
Author: elecharny
Date: Mon Sep  1 23:42:18 2014
New Revision: 1621895

URL: http://svn.apache.org/r1621895
Log:
o Added a Strings.parseInt() method which is faster than Integer.parseInt()

Modified:
    directory/shared/trunk/util/src/main/java/org/apache/directory/api/util/GeneralizedTime.java
    directory/shared/trunk/util/src/main/java/org/apache/directory/api/util/Strings.java

Modified: directory/shared/trunk/util/src/main/java/org/apache/directory/api/util/GeneralizedTime.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/util/src/main/java/org/apache/directory/api/util/GeneralizedTime.java?rev=1621895&r1=1621894&r2=1621895&view=diff
==============================================================================
--- directory/shared/trunk/util/src/main/java/org/apache/directory/api/util/GeneralizedTime.java
(original)
+++ directory/shared/trunk/util/src/main/java/org/apache/directory/api/util/GeneralizedTime.java
Mon Sep  1 23:42:18 2014
@@ -512,7 +512,7 @@ public class GeneralizedTime implements 
         }
         try
         {
-            int second = Integer.parseInt( upGeneralizedTime.substring( 12, 14 ) );
+            int second = Strings.parseInt( upGeneralizedTime.substring( 12, 14 ) );
             calendar.set( Calendar.SECOND, second );
         }
         catch ( NumberFormatException e )
@@ -531,7 +531,7 @@ public class GeneralizedTime implements 
         }
         try
         {
-            int minute = Integer.parseInt( upGeneralizedTime.substring( 10, 12 ) );
+            int minute = Strings.parseInt( upGeneralizedTime.substring( 10, 12 ) );
             calendar.set( Calendar.MINUTE, minute );
         }
         catch ( NumberFormatException e )
@@ -549,7 +549,7 @@ public class GeneralizedTime implements 
         }
         try
         {
-            int hour = Integer.parseInt( upGeneralizedTime.substring( 8, 10 ) );
+            int hour = Strings.parseInt( upGeneralizedTime.substring( 8, 10 ) );
             calendar.set( Calendar.HOUR_OF_DAY, hour );
         }
         catch ( NumberFormatException e )
@@ -567,7 +567,7 @@ public class GeneralizedTime implements 
         }
         try
         {
-            int day = Integer.parseInt( upGeneralizedTime.substring( 6, 8 ) );
+            int day = Strings.parseInt( upGeneralizedTime.substring( 6, 8 ) );
             calendar.set( Calendar.DAY_OF_MONTH, day );
         }
         catch ( NumberFormatException e )
@@ -585,7 +585,7 @@ public class GeneralizedTime implements 
         }
         try
         {
-            int month = Integer.parseInt( upGeneralizedTime.substring( 4, 6 ) );
+            int month = Strings.parseInt( upGeneralizedTime.substring( 4, 6 ) );
             calendar.set( Calendar.MONTH, month - 1 );
         }
         catch ( NumberFormatException e )
@@ -603,7 +603,7 @@ public class GeneralizedTime implements 
         }
         try
         {
-            int year = Integer.parseInt( upGeneralizedTime.substring( 0, 4 ) );
+            int year = Strings.parseInt( upGeneralizedTime.substring( 0, 4 ) );
             calendar.set( Calendar.YEAR, year );
         }
         catch ( NumberFormatException e )

Modified: directory/shared/trunk/util/src/main/java/org/apache/directory/api/util/Strings.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/util/src/main/java/org/apache/directory/api/util/Strings.java?rev=1621895&r1=1621894&r2=1621895&view=diff
==============================================================================
--- directory/shared/trunk/util/src/main/java/org/apache/directory/api/util/Strings.java (original)
+++ directory/shared/trunk/util/src/main/java/org/apache/directory/api/util/Strings.java Mon
Sep  1 23:42:18 2014
@@ -2302,4 +2302,36 @@ public final class Strings
     {
         return new UUID( 0, value ).toString();
     }
+    
+    
+    /**
+     * Past an ASCII String to a number
+     *
+     * @param value The string to parse
+     * @return the parsed value.
+     * @throws NumberFormatException If we don't have a number
+     */
+    public static int parseInt( String value ) throws NumberFormatException
+    {
+        long res = 0;
+        
+        for ( char c : value.toCharArray() )
+        {
+            if ( ( c >= '0' ) && ( c <= '9' ) )
+            {
+                res = res * 10 + ( c - '0' );
+                
+                if ( res > Integer.MAX_VALUE )
+                {
+                    throw new NumberFormatException( "Integer " + value + " is too big" );
+                }
+            }
+            else
+            {
+                throw new NumberFormatException( "Integer " + value + " is not valid" );
+            }
+        }
+        
+        return (int)res;
+    }
 }



Mime
View raw message