private static List<CategoryListItem> flattenRootCategory(CategoryListItem category){
  CategoryListItem[] subCategories=category.subItems;
  List<CategoryListItem> siblingCategories=new ArrayList<CategoryListItem>();
  for (int i=0; i < subCategories.length; i++) {
    CategoryListItem subCategory=subCategories[i];
    CategoryListItem[] subSubCategories=subCategory.subItems;
    if (subSubCategories.length != 0) {
      CategoryListItem[] newSubCategories=new CategoryListItem[subCategories.length - 1];
      System.arraycopy(subCategories,0,newSubCategories,0,i);
      System.arraycopy(subCategories,i + 1,newSubCategories,i,subCategories.length - i - 1);
      category=new CategoryListItem(category.name,category.fullName,category.key,newSubCategories);
      CategoryListItem siblingCategory=new CategoryListItem(category.name + " / " + subCategory.name,null,subCategory.key,subSubCategories);
      List<CategoryListItem> flattenedSiblingCategory=flattenRootCategory(siblingCategory);
      siblingCategories.addAll(flattenedSiblingCategory);
    }
  }
  if (!siblingCategories.isEmpty()) {
    Log.v("FlattenCategories",category.fullName + " had " + siblingCategories.size()+ " "+ "sub-categories moved up by one level");
  }
  siblingCategories.add(0,category);
  return siblingCategories;
}

private static List < CategoryListItem > flattenRootCategory ( CategoryListItem category ) { CategoryListItem [ ] subCategories = category . subItems ; List < CategoryListItem > siblingCategories = new ArrayList < CategoryListItem > ( ) ; for ( int i = 0 ; i < subCategories . length ; i ++ ) { CategoryListItem subCategory = subCategories [ i ] ; CategoryListItem [ ] subSubCategories = subCategory . subItems ; if ( subSubCategories . length != 0 ) { CategoryListItem [ ] newSubCategories = new CategoryListItem [ subCategories . length - 1 ] ; System . arraycopy ( subCategories , 0 , newSubCategories , 0 , i ) ; System . arraycopy ( subCategories , i + 1 , newSubCategories , i , subCategories . length - i - 1 ) ; category = new CategoryListItem ( category . name , category . fullName , category . key , newSubCategories ) ; CategoryListItem siblingCategory = new CategoryListItem ( category . name + " / " + subCategory . name , null , subCategory . key , subSubCategories ) ; List < CategoryListItem > flattenedSiblingCategory = flattenRootCategory ( siblingCategory ) ; siblingCategories . addAll ( flattenedSiblingCategory ) ; } } if ( ! siblingCategories . isEmpty ( ) ) { Log . v ( "FlattenCategories" , category . fullName + " had " + siblingCategories . size ( ) + " " + "sub-categories moved up by one level" ) ; } siblingCategories . add ( 0 , category ) ; return siblingCategories ; }
flattens a category so that it is at most two levels deep .
