← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 20287: DataElementOperandController, moving code to CollectionUtils

 

------------------------------------------------------------
revno: 20287
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2015-09-22 12:47:41 +0200
message:
  DataElementOperandController, moving code to CollectionUtils
modified:
  dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/collection/CollectionUtils.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/dataelement/DataElementOperandController.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-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/collection/CollectionUtils.java'
--- dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/collection/CollectionUtils.java	2015-07-15 19:25:25 +0000
+++ dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/collection/CollectionUtils.java	2015-09-22 10:47:41 +0000
@@ -238,4 +238,30 @@
 
         return valueList;
     }
+    
+    /**
+     * Searches for and returns the first string which starts with the given
+     * prefix. Removes the match from the collection.
+     * 
+     * @param collection the collection.
+     * @param prefix the string prefix.
+     * @return a string, or null if no matches.
+     */
+    public static String popStartsWith( Collection<String> collection, String prefix )
+    {
+        Iterator<String> iterator = collection.iterator();
+        
+        while ( iterator.hasNext() )
+        {
+            String element = iterator.next();
+            
+            if ( element != null && element.startsWith( prefix ) )
+            {
+                iterator.remove();
+                return element;
+            }
+        }
+        
+        return null;
+    }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/dataelement/DataElementOperandController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/dataelement/DataElementOperandController.java	2015-09-22 10:25:26 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/dataelement/DataElementOperandController.java	2015-09-22 10:47:41 +0000
@@ -29,9 +29,9 @@
  */
 
 import java.util.ArrayList;
-import java.util.Iterator;
 import java.util.List;
 
+import org.hisp.dhis.commons.collection.CollectionUtils;
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.dataelement.DataElementCategoryService;
 import org.hisp.dhis.dataelement.DataElementGroup;
@@ -68,20 +68,8 @@
         }
         else
         {
-            Iterator<String> iterator = filters.iterator();
-            String deGroup = null;
-
-            while ( iterator.hasNext() )
-            {
-                String filter = iterator.next();
-
-                if ( filter.startsWith( "dataElement.dataElementGroups.id:eq:" ) )
-                {
-                    deGroup = filter.substring( "dataElement.dataElementGroups.id:eq:".length() );
-                    iterator.remove();
-                    break;
-                }
-            }
+            String deGroup = CollectionUtils.popStartsWith( filters, "dataElement.dataElementGroups.id:eq:" );
+            deGroup = deGroup.substring( "dataElement.dataElementGroups.id:eq:".length() );
 
             if ( deGroup != null )
             {