← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 22062: Analytics. Centralization and testing of method for rounding of values.

 

------------------------------------------------------------
revno: 22062
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Sat 2016-02-27 11:57:15 +0100
message:
  Analytics. Centralization and testing of method for rounding of values.
modified:
  dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/AnalyticsUtils.java
  dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultAnalyticsService.java
  dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/AnalyticsUtilsTest.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/AnalyticsUtils.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/AnalyticsUtils.java	2016-01-04 02:27:49 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/AnalyticsUtils.java	2016-02-27 10:57:15 +0000
@@ -132,7 +132,7 @@
     }
     
     /**
-     * Returns a value. If the given parameters has skip rounding, the value is
+     * Rounds a value. If the given parameters has skip rounding, the value is
      * returned unchanged. If the given number of decimals is specified, the
      * value is rounded to the given decimals. Otherwise, default rounding is
      * used.
@@ -144,7 +144,7 @@
      */
     public static Double getRoundedValue( DataQueryParams params, Integer decimals, Double value )
     {
-        if ( params.isSkipRounding() )
+        if ( value == null || params.isSkipRounding() )
         {
             return value;
         }
@@ -159,6 +159,25 @@
     }
     
     /**
+     * Rounds a value. If the given parameters has skip rounding, the value is
+     * returned unchanged. If the given number is null or not of class Double,
+     * the value is returned unchanged. Otherwise, default rounding is used.
+     *  
+     * @param params the query parameters.
+     * @param value the value.
+     * @return a value.
+     */
+    public static Object getRoundedValueObject( DataQueryParams params, Object value )
+    {
+        if ( value == null || params.isSkipRounding() || !Double.class.equals( value.getClass() ) )
+        {
+            return value;
+        }
+        
+        return MathUtils.getRounded( (Double) value );
+    }
+    
+    /**
      * Converts the data and option combo identifiers to an operand identifier,
      * i.e. "deuid-cocuid" to "deuid.cocuid".
      * 

=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultAnalyticsService.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultAnalyticsService.java	2016-02-27 08:49:37 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultAnalyticsService.java	2016-02-27 10:57:15 +0000
@@ -330,7 +330,7 @@
             {
                 grid.addRow();
                 grid.addValues( entry.getKey().split( DIMENSION_SEP ) );
-                grid.addValue( params.isSkipRounding() ? entry.getValue() : getRounded( entry.getValue() ) );
+                grid.addValue( AnalyticsUtils.getRoundedValueObject( params, entry.getValue() ) );
             }
         }
     }
@@ -371,7 +371,7 @@
             {
                 grid.addRow();
                 grid.addValues( entry.getKey().split( DIMENSION_SEP ) );
-                grid.addValue( dataSourceParams.isSkipRounding() ? entry.getValue() : getRounded( entry.getValue() ) );
+                grid.addValue( AnalyticsUtils.getRoundedValueObject( dataSourceParams, entry.getValue() ) );
             }
         }
     }
@@ -1030,17 +1030,6 @@
     }
 
     /**
-     * Returns the given value. If of class Double the value is rounded.
-     *
-     * @param value the value to return and potentially round.
-     * @return the rounded value.
-     */
-    private Object getRounded( Object value )
-    {
-        return value != null && Double.class.equals( value.getClass() ) ? MathUtils.getRounded( (Double) value ) : value;
-    }
-
-    /**
      * Returns the max records limit. 0 indicates no limit.
      * 
      * @return the max records limit.

=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/AnalyticsUtilsTest.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/AnalyticsUtilsTest.java	2016-01-04 02:27:49 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/AnalyticsUtilsTest.java	2016-02-27 10:57:15 +0000
@@ -85,4 +85,33 @@
         assertTrue( convertedMap.containsKey( "YkRvCLedQa4.h1dJ9W4dWor-Zrd4DAf8M99" ) );
         assertTrue( convertedMap.containsKey( "PcfRp1HETO8.zqXKIEycBck-KBJBZopYMPV" ) );
     }
+    
+    @Test
+    public void testGetRoundedValueObject()
+    {
+        DataQueryParams paramsA = new DataQueryParams();
+        DataQueryParams paramsB = new DataQueryParams();
+        paramsB.setSkipRounding( true );
+        
+        assertEquals( null, AnalyticsUtils.getRoundedValueObject( paramsA, null ) );
+        assertEquals( "Car", AnalyticsUtils.getRoundedValueObject( paramsA, "Car" ) );
+        assertEquals( 3d, AnalyticsUtils.getRoundedValueObject( paramsA, 3d ) );
+        assertEquals( 3.1, (Double) AnalyticsUtils.getRoundedValueObject( paramsA, 3.123 ), 0.01 );
+        assertEquals( 3.123, (Double) AnalyticsUtils.getRoundedValueObject( paramsB, 3.123 ), 0.01 );
+    }
+
+    @Test
+    public void testGetRoundedValueDouble()
+    {
+        DataQueryParams paramsA = new DataQueryParams();
+        DataQueryParams paramsB = new DataQueryParams();
+        paramsB.setSkipRounding( true );
+        
+        assertEquals( null, AnalyticsUtils.getRoundedValue( paramsA, null, null ) );
+        assertEquals( 3d, AnalyticsUtils.getRoundedValue( paramsA, null, 3d ), 0.01 );
+        assertEquals( 3.1, AnalyticsUtils.getRoundedValue( paramsA, null, 3.123 ), 0.01 );
+        assertEquals( 3.1, AnalyticsUtils.getRoundedValue( paramsA, 1, 3.123 ), 0.01 );
+        assertEquals( 3.12, AnalyticsUtils.getRoundedValue( paramsA, 2, 3.123 ), 0.01 );
+        assertEquals( 3.123, AnalyticsUtils.getRoundedValue( paramsB, 2, 3.123 ), 0.01 );
+    }
 }