Skip to content

Commit 41fccfb

Browse files
authored
Merge pull request #1650 from tulinkry/groups-ordering
displaying the subgroups in ascending order
2 parents 3eaf29a + 9fb195d commit 41fccfb

File tree

2 files changed

+46
-15
lines changed

2 files changed

+46
-15
lines changed

src/org/opensolaris/opengrok/configuration/Groups.java

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,15 @@
4040
*/
4141
public final class Groups {
4242

43+
/**
44+
* Interface used to perform an action to a single group.
45+
*/
4346
private interface Walker {
4447

4548
/**
4649
* @param g group
47-
* @return true if traversing should end, false otherwise
50+
* @return true if traversing should stop just after this group, false
51+
* otherwise
4852
*/
4953
boolean call(Group g);
5054
}
@@ -326,34 +330,54 @@ private static void deleteGroup(Set<Group> groups, String groupname) {
326330
}
327331
}
328332

329-
private static boolean treeTraverseGroups(Set<Group> groups, Walker f) {
333+
/**
334+
* Traverse the set of groups starting in top level groups (groups without a
335+
* parent group) and performing depth first search in the group's subgroups.
336+
*
337+
* @param groups set of groups (mixed top level and other groups)
338+
* @param walker an instance of {@link Walker} which is used for every
339+
* traversed group
340+
* @return true if {@code walker} emits true for any of the groups; false
341+
* otherwise
342+
*
343+
* @see Walker
344+
*/
345+
private static boolean treeTraverseGroups(Set<Group> groups, Walker walker) {
330346
LinkedList<Group> stack = new LinkedList<>();
331347
for (Group g : groups) {
348+
// the flag here represents the group's depth in the group tree
332349
g.setFlag(0);
333350
if (g.getParent() == null) {
334-
stack.add(g);
351+
stack.addLast(g);
335352
}
336353
}
337354

338355
while (!stack.isEmpty()) {
339356
Group g = stack.getFirst();
340357
stack.removeFirst();
341358

342-
if (f.call(g)) {
359+
if (walker.call(g)) {
343360
return true;
344361
}
345362

346-
for (Group x : g.getSubgroups()) {
347-
x.setFlag(g.getFlag() + 1);
348-
stack.addFirst(x);
349-
}
363+
g.getSubgroups().forEach((x) -> x.setFlag(g.getFlag() + 1));
364+
// add all the subgroups respecting the sorted order
365+
stack.addAll(0, g.getSubgroups());
350366
}
351367
return false;
352368
}
353369

354-
private static boolean linearTraverseGroups(Set<Group> groups, Walker f) {
370+
/**
371+
* Traverse the set of groups linearly based on the set's iterator.
372+
*
373+
* @param groups set of groups (mixed top level and other groups)
374+
* @param walker an instance of {@link Walker} which is used for every
375+
* traversed group
376+
* @return true if {@code walker} emits true for any of the groups; false
377+
*/
378+
private static boolean linearTraverseGroups(Set<Group> groups, Walker walker) {
355379
for (Group g : groups) {
356-
if (f.call(g)) {
380+
if (walker.call(g)) {
357381
return true;
358382
}
359383
}

web/repos.jspf

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ CDDL HEADER END
2121
Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
2222

2323
--%>
24+
<%@page import="java.util.TreeSet"%>
25+
<%@page import="java.util.Iterator"%>
2426
<%@page import="org.opensolaris.opengrok.web.Scripts"%>
2527
<%@page import="org.json.simple.JSONArray"%>
2628
<%@page import="org.opensolaris.opengrok.configuration.messages.Message"%>
2729
<%@page import="java.util.SortedSet"%>
28-
<%@page import="java.util.SortedSet"%>
2930
<%@page import="java.util.Set"%>
3031
<%@page import="java.text.ParseException"%>
3132
<%@page import="java.util.ArrayList"%>
@@ -80,6 +81,7 @@ Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
8081
LinkedList<Group> stack = new LinkedList<>();
8182
for ( Group x : groups ) {
8283
if (x.getParent() == null && (pHelper.hasAllowedSubgroup(x) || cfg.isAllowed(x))) {
84+
// the flag here represents the state of the group - open/close
8385
x.setFlag(0);
8486
stack.addLast(x);
8587
}
@@ -97,14 +99,19 @@ Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
9799
}
98100

99101
stack.element().setFlag(1);
100-
101-
for (Group x : pHelper.getSubgroups(group)) {
102+
103+
Set<Group> subgroups = new TreeSet<>(pHelper.getSubgroups(group));
104+
for (Iterator<Group> it = subgroups.iterator(); it.hasNext();) {
105+
Group x = it.next();
102106
if (cfg.isAllowed(x) || pHelper.hasAllowedSubgroup(x)) {
103107
x.setFlag(0);
104-
stack.addFirst(x);
108+
} else {
109+
it.remove();
105110
}
106111
}
107-
112+
// add all the subgroups to the beginning respecting the order
113+
stack.addAll(0, subgroups);
114+
108115
%><div class="panel">
109116
<div class="panel-heading-accordion">
110117

0 commit comments

Comments
 (0)