@Test public void testVisitor() { Group group1 = createGroup("employees", "Employees", null); Group group2 = createGroup("developers", "Developers", group1); Group group3 = createGroup("java-developers-uk", "Java Developers UK", group2); Set<Group> nestedGroups = new LinkedHashSet<>(); ParentGroupVisitor visitor = new ParentGroupVisitor(); try { visitor.visit(group3, nestedGroups); for (Group group : nestedGroups) { System.out.println(group.getName()); } } catch (NotSupportedException e) { e.printStackTrace(); } }
@Override public void visit(Group group, Set<Group> hierarchy) throws NotSupportedException { if (group.getParent() != null) { hierarchy.add(group); visit(group.getParent(), hierarchy); } else { hierarchy.add(group); endVisit(group, hierarchy); } }
ParentGroupVisitor implements Visitor { @Override public void visit(Group group, Set<Group> hierarchy) throws NotSupportedException { if (group.getParent() != null) { hierarchy.add(group); visit(group.getParent(), hierarchy); } else { hierarchy.add(group); endVisit(group, hierarchy); } } }
ParentGroupVisitor implements Visitor { @Override public void visit(Group group, Set<Group> hierarchy) throws NotSupportedException { if (group.getParent() != null) { hierarchy.add(group); visit(group.getParent(), hierarchy); } else { hierarchy.add(group); endVisit(group, hierarchy); } }  }
ParentGroupVisitor implements Visitor { @Override public void visit(Group group, Set<Group> hierarchy) throws NotSupportedException { if (group.getParent() != null) { hierarchy.add(group); visit(group.getParent(), hierarchy); } else { hierarchy.add(group); endVisit(group, hierarchy); } }  @Override void visit(Group group, Set<Group> hierarchy); @Override void endVisit(Group group, Set<Group> hierarchy); }
ParentGroupVisitor implements Visitor { @Override public void visit(Group group, Set<Group> hierarchy) throws NotSupportedException { if (group.getParent() != null) { hierarchy.add(group); visit(group.getParent(), hierarchy); } else { hierarchy.add(group); endVisit(group, hierarchy); } }  @Override void visit(Group group, Set<Group> hierarchy); @Override void endVisit(Group group, Set<Group> hierarchy);  }