← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 10645: Impl method for testing whether string is numeric in a lenient way. Using lenient pattern when po...

 

------------------------------------------------------------
revno: 10645
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Sun 2013-04-21 20:28:51 +0200
message:
  Impl method for testing whether string is numeric in a lenient way. Using lenient pattern when populating analytics tables.
modified:
  dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcAnalyticsTableManager.java
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/MathUtils.java
  dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/MathUtilsTest.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcAnalyticsTableManager.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcAnalyticsTableManager.java	2013-04-19 16:40:53 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcAnalyticsTableManager.java	2013-04-21 18:28:51 +0000
@@ -121,7 +121,7 @@
             Date startDate = period.getStartDate();
             Date endDate = period.getEndDate();
             
-            String intClause = "dv.value " + statementBuilder.getRegexpMatch() + " '" + MathUtils.NUMERIC_REGEXP + "'";
+            String intClause = "dv.value " + statementBuilder.getRegexpMatch() + " '" + MathUtils.NUMERIC_LENIENT_REGEXP + "'";
             
             populateTable( table, startDate, endDate, "cast(dv.value as " + dbl + ")", "int", intClause );
             
@@ -166,7 +166,7 @@
             "left join period pe on dv.periodid=pe.periodid " +
             "where de.valuetype='" + valueType + "' " +
             "and pe.startdate >= '" + start + "' " +
-            "and pe.startdate <= '" + end + "'" +
+            "and pe.startdate <= '" + end + "' " +
             "and dv.value is not null " + 
             "and " + clause;
 

=== modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/MathUtils.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/MathUtils.java	2013-04-19 16:40:53 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/MathUtils.java	2013-04-21 18:28:51 +0000
@@ -46,9 +46,11 @@
     
     private static final double TOLERANCE = 0.01; 
     
-    public static final String NUMERIC_REGEXP = "^(0|-?[1-9]\\d*)(\\.\\d+)?$";
+    public static final String NUMERIC_REGEXP = "^(0|-?[1-9]\\d*)(\\.\\d+)?$";    
+    public static final String NUMERIC_LENIENT_REGEXP = "^(-?\\d+)(\\.\\d+)?$";
     
     private static final Pattern NUMERIC_PATTERN = Pattern.compile( NUMERIC_REGEXP );
+    private static final Pattern NUMERIC_LENIENT_PATTERN = Pattern.compile( NUMERIC_LENIENT_REGEXP );
     private static final Pattern INT_PATTERN = Pattern.compile( "^(0|-?[1-9]\\d*)$" );
     private static final Pattern POSITIVE_INT_PATTERN = Pattern.compile( "^[1-9]\\d*$" );
     private static final Pattern NEGATIVE_INT_PATTERN = Pattern.compile( "^-[1-9]\\d*$" );
@@ -213,6 +215,18 @@
     }
 
     /**
+     * Returns true if the provided string argument is to be considered numeric.
+     * Matches using a lenient pattern where leading zeros are allowed.
+     * 
+     * @param value the value.
+     * @return true if the provided string argument is to be considered numeric. 
+     */
+    public static boolean isNumericLenient( String value )
+    {
+        return value != null && NUMERIC_LENIENT_PATTERN.matcher( value ).matches();
+    }
+
+    /**
      * Returns true if the provided string argument is to be considered an integer. 
      * 
      * @param value the value.

=== modified file 'dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/MathUtilsTest.java'
--- dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/MathUtilsTest.java	2013-04-19 15:02:38 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/MathUtilsTest.java	2013-04-21 18:28:51 +0000
@@ -119,6 +119,41 @@
         assertFalse( MathUtils.isNumeric( "0." ) );
         assertFalse( MathUtils.isNumeric( null ) );
     }
+
+    @Test
+    public void testIsNumericLenient()
+    {
+        assertTrue( MathUtils.isNumericLenient( "0123" ) );
+        assertTrue( MathUtils.isNumericLenient( "123" ) );
+        assertTrue( MathUtils.isNumericLenient( "0" ) );
+        assertTrue( MathUtils.isNumericLenient( "1.2" ) );
+        assertTrue( MathUtils.isNumericLenient( "012.34" ) );
+        assertTrue( MathUtils.isNumericLenient( "12.34" ) );
+        assertTrue( MathUtils.isNumericLenient( "0.0" ) );
+        assertTrue( MathUtils.isNumericLenient( "1.234" ) );
+        assertTrue( MathUtils.isNumericLenient( "-1234" ) );
+        assertTrue( MathUtils.isNumericLenient( "-12.34" ) );
+
+        assertFalse( MathUtils.isNumericLenient( "Hey" ) );
+        assertFalse( MathUtils.isNumericLenient( "45 Perinatal Condition" ) );
+        assertFalse( MathUtils.isNumericLenient( "Long street 2" ) );
+        assertFalse( MathUtils.isNumericLenient( "1.2f" ) );
+        assertFalse( MathUtils.isNumericLenient( "1 234" ) );
+        assertFalse( MathUtils.isNumericLenient( ".1" ) );
+        assertFalse( MathUtils.isNumericLenient( ".4543" ) );
+        assertFalse( MathUtils.isNumericLenient( "." ) );
+        assertFalse( MathUtils.isNumericLenient( "1." ) );
+        assertFalse( MathUtils.isNumericLenient( "" ) );
+        assertFalse( MathUtils.isNumericLenient( " " ) );
+        assertFalse( MathUtils.isNumericLenient( "+6575  " ) );
+        assertFalse( MathUtils.isNumericLenient( "5643  " ) );
+        assertFalse( MathUtils.isNumericLenient( "  3243" ) );
+        assertFalse( MathUtils.isNumericLenient( "1,877" ) );
+        assertFalse( MathUtils.isNumericLenient( "0,1" ) );
+        assertFalse( MathUtils.isNumericLenient( "0," ) );
+        assertFalse( MathUtils.isNumericLenient( "0." ) );
+        assertFalse( MathUtils.isNumericLenient( null ) );
+    }
     
     @Test
     public void testIsInteger()