OrganisationUnitHierarchy { public Set<Long> getChildren( long parentId ) { Set<Long> preparedChildren = subTrees.get( parentId ); if ( preparedChildren != null ) { return new HashSet<>( preparedChildren ); } List<Long> children = new ArrayList<>(); children.add( 0, parentId ); int childCounter = 1; for ( int i = 0; i < childCounter; i++ ) { Set<Long> currentChildren = relationships.get( children.get( i ) ); if ( currentChildren != null ) { children.addAll( currentChildren ); childCounter += currentChildren.size(); } } return new HashSet<>( children ); } OrganisationUnitHierarchy( Map<Long, Set<Long>> relationships );  OrganisationUnitHierarchy( Collection<OrganisationUnitRelationship> relations ); OrganisationUnitHierarchy prepareChildren( OrganisationUnit parent ); OrganisationUnitHierarchy prepareChildren( Collection<OrganisationUnit> parents ); OrganisationUnitHierarchy prepareChildren( OrganisationUnit parent, OrganisationUnitGroup group ); OrganisationUnitHierarchy prepareChildren( Collection<OrganisationUnit> parents, Collection<OrganisationUnitGroup> groups ); Set<Long> getChildren( long parentId ); Set<Long> getChildren( Collection<Long> parentIds ); Set<Long> getChildren( long parentId, OrganisationUnitGroup group ); Set<Long> getChildren( Collection<Long> parentIds, Collection<OrganisationUnitGroup> groups );  }
@Test public void testGetGroupChildren() { 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<>(); relationships.add( new OrganisationUnitRelationship( 1, 2 ) ); relationships.add( new OrganisationUnitRelationship( 1, 3 ) ); relationships.add( new OrganisationUnitRelationship( 2, 4 ) ); relationships.add( new OrganisationUnitRelationship( 2, 5 ) ); relationships.add( new OrganisationUnitRelationship( 2, 6 ) ); relationships.add( new OrganisationUnitRelationship( 3, 7 ) ); relationships.add( new OrganisationUnitRelationship( 3, 8 ) ); relationships.add( new OrganisationUnitRelationship( 3, 9 ) ); relationships.add( new OrganisationUnitRelationship( 4, 10 ) ); relationships.add( new OrganisationUnitRelationship( 4, 11 ) ); 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() ); assertTrue( hierarchy.getChildren( 2, group ).contains( 2l ) ); assertTrue( hierarchy.getChildren( 2, group ).contains( 4l ) ); assertTrue( hierarchy.getChildren( 2, group ).contains( 6l ) ); assertTrue( hierarchy.getChildren( 2, group ).contains( 10l ) ); assertTrue( hierarchy.getChildren( 2, group ).contains( 12l ) ); assertEquals( 1, hierarchy.getChildren( 3, group ).size() ); assertTrue( hierarchy.getChildren( 3, group ).contains( 8l ) ); assertEquals( 3, hierarchy.getChildren( 4, group ).size() ); assertTrue( hierarchy.getChildren( 4, group ).contains( 4l ) ); assertTrue( hierarchy.getChildren( 4, group ).contains( 10l ) ); assertTrue( hierarchy.getChildren( 4, group ).contains( 12l ) ); assertEquals( 0, hierarchy.getChildren( 11, group ).size() ); assertFalse( hierarchy.getChildren( 5, group ).contains( 10l ) ); assertFalse( hierarchy.getChildren( 3, group ).contains( 11l ) ); }
@Test public void testGetChildren() { Set<Long> parentIds = new HashSet<>(); List<OrganisationUnitRelationship> relations = new ArrayList<>(); int parentMax = 1000; int childMax = 4; int childId = 0; for ( long 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<Long> children = hierarchy.getChildren( parentIds ); assertNotNull( children ); assertEquals( (parentMax * childMax) + 1, children.size() ); }