@Test public void buildFromOrderedList() throws Exception { List<Forum> sourceList = new ArrayList<>(); Collections.addAll(sourceList, bookmarks, discussion, shsc, games, cobol, projectlog); ForumStructure forumStructure = ForumStructure.buildFromOrderedList(sourceList, TOP_LEVEL_ID); List<Forum> builtTree = forumStructure.getAsList().formatAs(FULL_TREE).build(); assertThat(builtTree, is(aForumTreeMatching(sourceTree))); }
@NonNull static ForumStructure buildFromOrderedList(List<Forum> orderedForums, @Nullable Integer topLevelParentId) { List<Forum> forumTree = new ArrayList<>(); Map<Integer, Forum> forumsById = new LinkedHashMap<>(); for (Forum forum : orderedForums) { Forum forumCopy = new Forum(forum); forumsById.put(forumCopy.id, forumCopy); } Forum parentForum; for (Forum forum : forumsById.values()) { parentForum = forumsById.get(forum.parentId); if (topLevelParentId == null && parentForum == null || topLevelParentId != null && forum.parentId == topLevelParentId) { forumTree.add(forum); } else { if (parentForum != null) { parentForum.subforums.add(forum); } else { Log.w(TAG, "Unable to find parent forum with ID: " + forum.parentId); } } } return new ForumStructure(forumTree); }
ForumStructure { @NonNull static ForumStructure buildFromOrderedList(List<Forum> orderedForums, @Nullable Integer topLevelParentId) { List<Forum> forumTree = new ArrayList<>(); Map<Integer, Forum> forumsById = new LinkedHashMap<>(); for (Forum forum : orderedForums) { Forum forumCopy = new Forum(forum); forumsById.put(forumCopy.id, forumCopy); } Forum parentForum; for (Forum forum : forumsById.values()) { parentForum = forumsById.get(forum.parentId); if (topLevelParentId == null && parentForum == null || topLevelParentId != null && forum.parentId == topLevelParentId) { forumTree.add(forum); } else { if (parentForum != null) { parentForum.subforums.add(forum); } else { Log.w(TAG, "Unable to find parent forum with ID: " + forum.parentId); } } } return new ForumStructure(forumTree); } }
ForumStructure { @NonNull static ForumStructure buildFromOrderedList(List<Forum> orderedForums, @Nullable Integer topLevelParentId) { List<Forum> forumTree = new ArrayList<>(); Map<Integer, Forum> forumsById = new LinkedHashMap<>(); for (Forum forum : orderedForums) { Forum forumCopy = new Forum(forum); forumsById.put(forumCopy.id, forumCopy); } Forum parentForum; for (Forum forum : forumsById.values()) { parentForum = forumsById.get(forum.parentId); if (topLevelParentId == null && parentForum == null || topLevelParentId != null && forum.parentId == topLevelParentId) { forumTree.add(forum); } else { if (parentForum != null) { parentForum.subforums.add(forum); } else { Log.w(TAG, "Unable to find parent forum with ID: " + forum.parentId); } } } return new ForumStructure(forumTree); } private  ForumStructure(List<Forum> forumTree); }
ForumStructure { @NonNull static ForumStructure buildFromOrderedList(List<Forum> orderedForums, @Nullable Integer topLevelParentId) { List<Forum> forumTree = new ArrayList<>(); Map<Integer, Forum> forumsById = new LinkedHashMap<>(); for (Forum forum : orderedForums) { Forum forumCopy = new Forum(forum); forumsById.put(forumCopy.id, forumCopy); } Forum parentForum; for (Forum forum : forumsById.values()) { parentForum = forumsById.get(forum.parentId); if (topLevelParentId == null && parentForum == null || topLevelParentId != null && forum.parentId == topLevelParentId) { forumTree.add(forum); } else { if (parentForum != null) { parentForum.subforums.add(forum); } else { Log.w(TAG, "Unable to find parent forum with ID: " + forum.parentId); } } } return new ForumStructure(forumTree); } private  ForumStructure(List<Forum> forumTree); int getNumberOfForums(); @NonNull ListBuilder getAsList(); }
ForumStructure { @NonNull static ForumStructure buildFromOrderedList(List<Forum> orderedForums, @Nullable Integer topLevelParentId) { List<Forum> forumTree = new ArrayList<>(); Map<Integer, Forum> forumsById = new LinkedHashMap<>(); for (Forum forum : orderedForums) { Forum forumCopy = new Forum(forum); forumsById.put(forumCopy.id, forumCopy); } Forum parentForum; for (Forum forum : forumsById.values()) { parentForum = forumsById.get(forum.parentId); if (topLevelParentId == null && parentForum == null || topLevelParentId != null && forum.parentId == topLevelParentId) { forumTree.add(forum); } else { if (parentForum != null) { parentForum.subforums.add(forum); } else { Log.w(TAG, "Unable to find parent forum with ID: " + forum.parentId); } } } return new ForumStructure(forumTree); } private  ForumStructure(List<Forum> forumTree); int getNumberOfForums(); @NonNull ListBuilder getAsList(); static final int FULL_TREE; static final int TWO_LEVEL; static final int FLAT; }