← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 2077: Code optimization in ParserUtil

 

------------------------------------------------------------
revno: 2077
committer: Lars <larshelg@larshelg-laptop>
branch nick: trunk
timestamp: Sun 2010-07-04 13:02:28 +0200
message:
  Code optimization in ParserUtil
modified:
  dhis-2/dhis-services/dhis-service-aggregationengine-default/src/main/java/org/hisp/dhis/aggregation/impl/indicator/IndicatorAggregation.java
  dhis-2/dhis-services/dhis-service-datamart-default/src/main/java/org/hisp/dhis/datamart/util/ParserUtil.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-aggregationengine-default/src/main/java/org/hisp/dhis/aggregation/impl/indicator/IndicatorAggregation.java'
--- dhis-2/dhis-services/dhis-service-aggregationengine-default/src/main/java/org/hisp/dhis/aggregation/impl/indicator/IndicatorAggregation.java	2010-04-12 21:23:33 +0000
+++ dhis-2/dhis-services/dhis-service-aggregationengine-default/src/main/java/org/hisp/dhis/aggregation/impl/indicator/IndicatorAggregation.java	2010-07-04 11:02:28 +0000
@@ -53,6 +53,8 @@
 public class IndicatorAggregation
 {
     private static final String NULL_REPLACEMENT = "0";
+
+    private static final Pattern OPERAND_PATTERN = Pattern.compile( "(\\[\\d+\\" + SEPARATOR + "\\d+\\])" );
     
     // -------------------------------------------------------------------------
     // Dependencies
@@ -123,10 +125,6 @@
     }
 
     // -------------------------------------------------------------------------
-    // Supportive methods for Indicator aggregation
-    // -------------------------------------------------------------------------
-
-    // -------------------------------------------------------------------------
     // Supportive methods
     // -------------------------------------------------------------------------
 
@@ -159,17 +157,14 @@
     {    	
         try
         {        	
-        	Pattern pattern = Pattern.compile( "(\\[\\d+\\.\\d+\\])" );
+            Matcher matcher = OPERAND_PATTERN.matcher( formula );
             
-            Matcher matcher = pattern.matcher( formula );
             StringBuffer buffer = new StringBuffer();            
             
             while ( matcher.find() )
             {
-                String replaceString = matcher.group();
+                String replaceString = matcher.group().replaceAll( "[\\[\\]]", "" );
                 
-                replaceString = replaceString.replaceAll( "[\\[\\]]", "" );
-
                 String dataElementIdString = replaceString.substring( 0, replaceString.indexOf( SEPARATOR ) );                
                 String optionComboIdString = replaceString.substring( replaceString.indexOf( SEPARATOR ) + 1, replaceString.length() );
                 

=== modified file 'dhis-2/dhis-services/dhis-service-datamart-default/src/main/java/org/hisp/dhis/datamart/util/ParserUtil.java'
--- dhis-2/dhis-services/dhis-service-datamart-default/src/main/java/org/hisp/dhis/datamart/util/ParserUtil.java	2010-04-12 21:23:33 +0000
+++ dhis-2/dhis-services/dhis-service-datamart-default/src/main/java/org/hisp/dhis/datamart/util/ParserUtil.java	2010-07-04 11:02:28 +0000
@@ -43,7 +43,9 @@
  */
 public class ParserUtil
 {
-    private static final String NULL_REPLACEMENT = "0";    
+    private static final String NULL_REPLACEMENT = "0";
+    
+    private static final Pattern OPERAND_PATTERN = Pattern.compile( "(\\[\\d+\\" + SEPARATOR + "\\d+\\])" );
     
     /**
      * Returns the data element identifiers in the given expression. Returns null
@@ -60,19 +62,15 @@
         {
             dataElementIdsInExpression = new HashSet<Integer>();
 
-            final Matcher matcher = getMatcher( "(\\[\\d+\\" + SEPARATOR + "\\d+\\])", expression );
+            final Matcher matcher = OPERAND_PATTERN.matcher( expression );
 
             while ( matcher.find() )
             {
-                String replaceString = matcher.group();
-
-                replaceString = replaceString.replaceAll( "[\\[\\]]", "" );
+                String replaceString = matcher.group().replaceAll( "[\\[\\]]", "" );
 
                 replaceString = replaceString.substring( 0, replaceString.indexOf( SEPARATOR ) );
 
-                final int dataElementId = Integer.parseInt( replaceString );
-
-                dataElementIdsInExpression.add( dataElementId );
+                dataElementIdsInExpression.add( Integer.parseInt( replaceString ) );
             }
         }
 
@@ -91,27 +89,21 @@
     public static String generateExpression( final String formula, final Map<DataElementOperand, Double> valueMap )
     {       
         try
-        {           
-            final Pattern pattern = Pattern.compile( "(\\[\\d+\\.\\d+\\])" );
-            
-            final Matcher matcher = pattern.matcher( formula );
+        {
+            final Matcher matcher = OPERAND_PATTERN.matcher( formula );
             
             final StringBuffer buffer = new StringBuffer();            
             
-            Double aggregatedValue = null;
-            
             while ( matcher.find() )
             {
-                String replaceString = matcher.group();
-                
-                replaceString = replaceString.replaceAll( "[\\[\\]]", "" );
+                String replaceString = matcher.group().replaceAll( "[\\[\\]]", "" );
                 
                 int dataElementId = Integer.parseInt( replaceString.substring( 0, replaceString.indexOf( SEPARATOR ) ) );
                 int categoryOptionComboId = Integer.parseInt( replaceString.substring( replaceString.indexOf( SEPARATOR ) + 1 ) );
                 
                 final DataElementOperand operand = new DataElementOperand( dataElementId, categoryOptionComboId );
                 
-                aggregatedValue = valueMap.get( operand );
+                Double aggregatedValue = valueMap.get( operand );
                 
                 replaceString = ( aggregatedValue == null ) ? NULL_REPLACEMENT : String.valueOf( aggregatedValue );
                 
@@ -124,20 +116,7 @@
         }
         catch ( NumberFormatException ex )
         {
-            throw new RuntimeException( "Illegal data element id", ex );
+            throw new RuntimeException( "Illegal data element or category combo id", ex );
         }
     }
-
-    /**
-     * Returns a matcher object compiled with the given regex and matched with the given expression.
-     * 
-     * @param regex The regular expression.
-     * @param formula The formula.
-     */
-    public static Matcher getMatcher( final String regex, final String formula )
-    {
-        final Pattern pattern = Pattern.compile( regex );
-        
-        return pattern.matcher( formula );
-    }    
 }