protected List < CmsResource > getTopFolders ( List < CmsResource > folders ) { List < String > folderPaths = new ArrayList < String > ( ) ; List < CmsResource > topFolders = new ArrayList < CmsResource > ( ) ; Map < String , CmsResource > foldersByPath = new HashMap < String , CmsResource > ( ) ; for ( CmsResource folder : folders ) { folderPaths . add ( folder . getRootPath ( ) ) ; foldersByPath . put ( folder . getRootPath ( ) , folder ) ; } Collections . sort ( folderPaths ) ; Set < String > topFolderPaths = new HashSet < String > ( folderPaths ) ; for ( int i = 0 ; i < folderPaths . size ( ) ; i ++ ) { for ( int j = i + 1 ; j < folderPaths . size ( ) ; j ++ ) { if ( folderPaths . get ( j ) . startsWith ( ( folderPaths . get ( i ) ) ) ) { topFolderPaths . remove ( folderPaths . get ( j ) ) ; } else { break ; } } } for ( String path : topFolderPaths ) { topFolders . add ( foldersByPath . get ( path ) ) ; } return topFolders ; }
Gives the roots of a list of folders i . e . the list of folders which are not descendants of any other folders in the original list