dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #27392
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 13638: simplified collections copy
------------------------------------------------------------
revno: 13638
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2014-01-07 20:16:54 +0100
message:
simplified collections copy
modified:
dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectUtils.java
dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/NameableObjectUtils.java
dhis-2/dhis-api/src/test/java/org/hisp/dhis/organisationunit/OrganisationUnitHierarchyTest.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/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<NameableObject> getList( NameableObject... objects )
{
List<NameableObject> list = new ArrayList<NameableObject>();
-
+
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 <T extends NameableObject> List<T> asTypedList( Collection<NameableObject> collection )
{
List<T> list = new ArrayList<T>();
-
+
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 <T extends NameableObject> List<T> asTypedList( Collection<NameableObject> collection, Class<T> 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<OrganisationUnitRelationship> relationships = new ArrayList<OrganisationUnitRelationship>();
-
+
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<Integer> parentIds = new HashSet<Integer>();
-
+
List<OrganisationUnitRelationship> relations = new ArrayList<OrganisationUnitRelationship>();
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<Integer> children = hierarchy.getChildren( parentIds );
-
+
assertNotNull( children );
- assertEquals( ( parentMax * childMax ) + 1, children.size() );
+ assertEquals( (parentMax * childMax) + 1, children.size() );
}
-
+
@Test
public void testGetChildrenA()
{
List<OrganisationUnitRelationship> relationships = new ArrayList<OrganisationUnitRelationship>();
-
+
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<Integer, Set<Integer>> relationships = new HashMap<Integer, Set<Integer>>();
-
+
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<Integer> getSet( Integer... ints )
{
Set<Integer> set = new HashSet<Integer>();
-
- for ( Integer i : ints )
- {
- set.add( i );
- }
-
+
+ Collections.addAll( set, ints );
+
return set;
}
}