← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21404: ExpressioService. Returning empty collections instead of null. Better practise.

 

------------------------------------------------------------
revno: 21404
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2015-12-10 12:32:45 +0100
message:
  ExpressioService. Returning empty collections instead of null. Better practise.
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/expression/ExpressionService.java
  dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultAnalyticsService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/expression/DefaultExpressionService.java
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/expression/ExpressionServiceTest.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-api/src/main/java/org/hisp/dhis/expression/ExpressionService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/expression/ExpressionService.java	2015-12-10 11:14:51 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/expression/ExpressionService.java	2015-12-10 11:32:45 +0000
@@ -188,7 +188,8 @@
 
     /**
      * Returns all OrganisationUnitGroups in the numerator and denominator
-     * expressions in the given Indicators.
+     * expressions in the given Indicators. Returns an empty list if the given
+     * indicators are null or empty.
      * 
      * @param indicators the set of indicators.
      * @return a Set of OrganisationUnitGroups.
@@ -196,7 +197,8 @@
     Set<OrganisationUnitGroup> getOrganisationUnitGroupsInIndicators( Collection<Indicator> indicators );
     
     /**
-     * Returns all OrganisationUnitGroups in the given expression string.
+     * Returns all OrganisationUnitGroups in the given expression string. Returns 
+     * an empty list if the given indicators are null or empty.
      * 
      * @param expression the expression string.
      * @return a Set of OrganisationUnitGroups included in the expression string.

=== 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	2015-12-10 10:41:17 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultAnalyticsService.java	2015-12-10 11:32:45 +0000
@@ -687,7 +687,7 @@
     {
         Set<OrganisationUnitGroup> orgUnitGroups = expressionService.getOrganisationUnitGroupsInIndicators( indicators );
 
-        if ( orgUnitGroups == null || orgUnitGroups.isEmpty() )
+        if ( orgUnitGroups.isEmpty() )
         {
             return null;
         }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/expression/DefaultExpressionService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/expression/DefaultExpressionService.java	2015-12-10 11:14:51 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/expression/DefaultExpressionService.java	2015-12-10 11:32:45 +0000
@@ -277,12 +277,10 @@
     @Override
     public Set<OrganisationUnitGroup> getOrganisationUnitGroupsInIndicators( Collection<Indicator> indicators )
     {
-        Set<OrganisationUnitGroup> groups = null;
+        Set<OrganisationUnitGroup> groups = new HashSet<>();
         
         if ( indicators != null )
-        {
-            groups = new HashSet<>();
-            
+        {   
             for ( Indicator indicator : indicators )
             {
                 groups.addAll( getOrganisationUnitGroupsInExpression( indicator.getNumerator() ) );
@@ -296,12 +294,10 @@
     @Override
     public Set<OrganisationUnitGroup> getOrganisationUnitGroupsInExpression( String expression )
     {
-        Set<OrganisationUnitGroup> groupsInExpression = null;
+        Set<OrganisationUnitGroup> groupsInExpression = new HashSet<>();
         
         if ( expression != null )
-        {
-            groupsInExpression = new HashSet<>();
-            
+        {            
             final Matcher matcher = OU_GROUP_PATTERN.matcher( expression );
             
             while ( matcher.find() )

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/expression/ExpressionServiceTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/expression/ExpressionServiceTest.java	2015-12-10 11:14:51 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/expression/ExpressionServiceTest.java	2015-12-10 11:32:45 +0000
@@ -567,4 +567,21 @@
         assertTrue( expressions.contains( exprA ) );
         assertTrue( expressions.contains( exprB ) );
     }
+    
+    @Test
+    public void testGetOrganisationUnitGroupsInExpression()
+    {
+        String expression = "OUG{" + groupA.getUid() + "} + 10";
+        
+        Set<OrganisationUnitGroup> groups = expressionService.getOrganisationUnitGroupsInExpression( expression );
+        
+        assertNotNull( groups );
+        assertEquals( 1, groups.size() );
+        assertTrue( groups.contains( groupA ) );
+
+        groups = expressionService.getOrganisationUnitGroupsInExpression( null );
+        
+        assertNotNull( groups );
+        assertEquals( 0, groups.size() );        
+    }
 }