← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 18863: ListUtils, removeAll, ignoring out of bounds indexes

 

------------------------------------------------------------
revno: 18863
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2015-04-09 21:24:15 +0200
message:
  ListUtils, removeAll, ignoring out of bounds indexes
modified:
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ListUtils.java
  dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/ListUtilsTest.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-system/src/main/java/org/hisp/dhis/system/util/ListUtils.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ListUtils.java	2015-02-19 09:18:17 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ListUtils.java	2015-04-09 19:24:15 +0000
@@ -46,7 +46,8 @@
 public class ListUtils
 {
     /**
-     * Removes from the given list the elements at the given indexes.
+     * Removes from the given list the elements at the given indexes. Ignores
+     * indexes which are out of bounds of list.
      * 
      * @param list the list to remove elements from.
      * @param indexes the indexes for the elements to remove.
@@ -60,14 +61,20 @@
         
         Collections.sort( indexes, Collections.reverseOrder() );
         
+        final int size = list.size();
+        
         for ( Integer index : indexes )
-        {            
-            list.remove( (int) index );
+        {
+            if ( index >= 0 && index < size )
+            {
+                list.remove( (int) index );
+            }
         }
     }
     
     /**
-     * Removes from the given list the elements at the given indexes.
+     * Removes from the given list the elements at the given indexes. Ignores
+     * indexes which are out of bounds of list.
      * 
      * @param indexes the list to remove elements from.
      * @param indexes the indexes for the elements to remove.

=== modified file 'dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/ListUtilsTest.java'
--- dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/ListUtilsTest.java	2015-01-17 07:41:26 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/ListUtilsTest.java	2015-04-09 19:24:15 +0000
@@ -48,7 +48,7 @@
     {
         List<String> list = new ArrayList<>( Arrays.asList( "a", "b", "c", "d", "e", "f", "g", "h" ) );
         
-        Integer[] indexes = { 0, 2, 5, 7 };
+        Integer[] indexes = { 0, 2, 5, 7, -1, 78 };
 
         assertEquals( 8, list.size() );