=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectUtils.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectUtils.java 2013-11-25 14:54:19 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectUtils.java 2014-01-07 19:16:54 +0000 @@ -31,6 +31,7 @@ import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.Iterator; @@ -140,10 +141,7 @@ if ( objects != null ) { - for ( IdentifiableObject object : objects ) - { - list.add( object ); - } + Collections.addAll( list, objects ); } return list; === modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/NameableObjectUtils.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/NameableObjectUtils.java 2013-10-04 10:59:00 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/NameableObjectUtils.java 2014-01-07 19:16:54 +0000 @@ -30,6 +30,7 @@ import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.List; /** @@ -39,28 +40,25 @@ { /** * Returns a list of NameableObjects. - * + * * @param objects the NameableObjects to include in the list. * @return a list of NameableObjects. */ public static List getList( NameableObject... objects ) { List list = new ArrayList(); - + if ( objects != null ) { - for ( NameableObject object : objects ) - { - list.add( object ); - } + Collections.addAll( list, objects ); } - + return list; } - + /** * Returns a list with erasure NameableObject based on the given collection. - * + * * @param collection the collection. * @return a list of NameableObjects. */ @@ -75,7 +73,7 @@ * Returns a list typed with the desired erasure based on the given collection. * This operation implies an unchecked cast and it is the responsibility of * the caller to make sure the cast is valid. - * + * * @param collection the collection. * @return a list. */ @@ -83,7 +81,7 @@ public static List asTypedList( Collection collection ) { List list = new ArrayList(); - + if ( collection != null ) { for ( NameableObject object : collection ) @@ -91,7 +89,7 @@ list.add( (T) object ); } } - + return list; } @@ -99,9 +97,9 @@ * Returns a list typed with the desired erasure based on the given collection. * This operation implies an unchecked cast and it is the responsibility of * the caller to make sure the cast is valid. - * + * * @param collection the collection. - * @param the class type. + * @param the class type. * @return a list. */ public static List asTypedList( Collection collection, Class clazz ) === modified file 'dhis-2/dhis-api/src/test/java/org/hisp/dhis/organisationunit/OrganisationUnitHierarchyTest.java' --- dhis-2/dhis-api/src/test/java/org/hisp/dhis/organisationunit/OrganisationUnitHierarchyTest.java 2013-08-23 15:56:19 +0000 +++ dhis-2/dhis-api/src/test/java/org/hisp/dhis/organisationunit/OrganisationUnitHierarchyTest.java 2014-01-07 19:16:54 +0000 @@ -28,19 +28,17 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.assertNotNull; +import org.junit.Test; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; -import org.junit.Test; +import static org.junit.Assert.*; /** * @author Lars Helge Overland @@ -52,30 +50,30 @@ { OrganisationUnitGroup group = new OrganisationUnitGroup( "Group" ); group.setId( 1 ); - + OrganisationUnit unit2 = new OrganisationUnit( "Unit2" ); OrganisationUnit unit4 = new OrganisationUnit( "Unit4" ); OrganisationUnit unit6 = new OrganisationUnit( "Unit6" ); OrganisationUnit unit8 = new OrganisationUnit( "Unit8" ); OrganisationUnit unit10 = new OrganisationUnit( "Unit10" ); OrganisationUnit unit12 = new OrganisationUnit( "Unit12" ); - + unit2.setId( 2 ); unit4.setId( 4 ); unit6.setId( 6 ); unit8.setId( 8 ); unit10.setId( 10 ); unit12.setId( 12 ); - + group.addOrganisationUnit( unit2 ); group.addOrganisationUnit( unit4 ); group.addOrganisationUnit( unit6 ); group.addOrganisationUnit( unit8 ); group.addOrganisationUnit( unit10 ); group.addOrganisationUnit( unit12 ); - + List relationships = new ArrayList(); - + relationships.add( new OrganisationUnitRelationship( 1, 2 ) ); relationships.add( new OrganisationUnitRelationship( 1, 3 ) ); relationships.add( new OrganisationUnitRelationship( 2, 4 ) ); @@ -89,7 +87,7 @@ relationships.add( new OrganisationUnitRelationship( 4, 12 ) ); OrganisationUnitHierarchy hierarchy = new OrganisationUnitHierarchy( relationships ); - + assertEquals( 6, hierarchy.getChildren( 1, group ).size() ); assertEquals( 5, hierarchy.getChildren( 2, group ).size() ); @@ -105,48 +103,48 @@ assertEquals( 3, hierarchy.getChildren( 4, group ).size() ); assertTrue( hierarchy.getChildren( 4, group ).contains( 4 ) ); assertTrue( hierarchy.getChildren( 4, group ).contains( 10 ) ); - assertTrue( hierarchy.getChildren( 4, group ).contains( 12 ) ); + assertTrue( hierarchy.getChildren( 4, group ).contains( 12 ) ); assertEquals( 0, hierarchy.getChildren( 11, group ).size() ); - + assertFalse( hierarchy.getChildren( 5, group ).contains( 10 ) ); assertFalse( hierarchy.getChildren( 3, group ).contains( 11 ) ); } - + @Test public void testGetChildren() { Set parentIds = new HashSet(); - + List relations = new ArrayList(); int parentMax = 1000; // Increase to stress-test int childMax = 4; int childId = 0; - + for ( int parentId = 0; parentId < parentMax; parentId++ ) { parentIds.add( parentId ); - + for ( int j = 0; j < childMax; j++ ) { relations.add( new OrganisationUnitRelationship( parentId, ++childId ) ); } } - + OrganisationUnitHierarchy hierarchy = new OrganisationUnitHierarchy( relations ); - + Set children = hierarchy.getChildren( parentIds ); - + assertNotNull( children ); - assertEquals( ( parentMax * childMax ) + 1, children.size() ); + assertEquals( (parentMax * childMax) + 1, children.size() ); } - + @Test public void testGetChildrenA() { List relationships = new ArrayList(); - + relationships.add( new OrganisationUnitRelationship( 1, 2 ) ); relationships.add( new OrganisationUnitRelationship( 1, 3 ) ); relationships.add( new OrganisationUnitRelationship( 2, 4 ) ); @@ -158,31 +156,31 @@ relationships.add( new OrganisationUnitRelationship( 4, 10 ) ); relationships.add( new OrganisationUnitRelationship( 4, 11 ) ); relationships.add( new OrganisationUnitRelationship( 4, 12 ) ); - + OrganisationUnitHierarchy hierarchy = new OrganisationUnitHierarchy( relationships ); - + testHierarchy( hierarchy ); } - + @Test public void testGetChildrenB() { Map> relationships = new HashMap>(); - + relationships.put( 1, getSet( 2, 3 ) ); relationships.put( 2, getSet( 4, 5, 6 ) ); relationships.put( 3, getSet( 7, 8, 9 ) ); relationships.put( 4, getSet( 10, 11, 12 ) ); OrganisationUnitHierarchy hierarchy = new OrganisationUnitHierarchy( relationships ); - + testHierarchy( hierarchy ); } - + private void testHierarchy( OrganisationUnitHierarchy hierarchy ) { assertEquals( 12, hierarchy.getChildren( 1 ).size() ); - + assertEquals( 7, hierarchy.getChildren( 2 ).size() ); assertTrue( hierarchy.getChildren( 2 ).contains( 2 ) ); assertTrue( hierarchy.getChildren( 2 ).contains( 4 ) ); @@ -191,7 +189,7 @@ assertTrue( hierarchy.getChildren( 2 ).contains( 10 ) ); assertTrue( hierarchy.getChildren( 2 ).contains( 11 ) ); assertTrue( hierarchy.getChildren( 2 ).contains( 12 ) ); - + assertEquals( 4, hierarchy.getChildren( 3 ).size() ); assertTrue( hierarchy.getChildren( 3 ).contains( 3 ) ); assertTrue( hierarchy.getChildren( 3 ).contains( 7 ) ); @@ -206,20 +204,17 @@ assertEquals( 1, hierarchy.getChildren( 11 ).size() ); assertTrue( hierarchy.getChildren( 11 ).contains( 11 ) ); - + assertFalse( hierarchy.getChildren( 2 ).contains( 3 ) ); assertFalse( hierarchy.getChildren( 2 ).contains( 8 ) ); } - + private Set getSet( Integer... ints ) { Set set = new HashSet(); - - for ( Integer i : ints ) - { - set.add( i ); - } - + + Collections.addAll( set, ints ); + return set; } }