mirror of
https://github.com/jenkinsci/jenkins.git
synced 2026-11-14 04:22:16 +00:00
What I hope is a more efficient Items.getAllItems, using a simpler sorting method.
Cf.: https://github.com/jenkinsci/metrics-plugin/commit/b7a2f58c977c8d1053d702e36dcc36bb1348e1aa
This commit is contained in:
1 parent
d4b6430d11
commit
1197e0ceaf
1 file changed
+31
-31
@@ -38,7 +38,13 @@ import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
import java.util.StringTokenizer;
|
||||
import javax.annotation.CheckForNull;
|
||||
import jenkins.model.DirectlyModifiableTopLevelItemGroup;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
@@ -296,39 +302,33 @@ public class Items {
|
||||
*/
|
||||
public static <T extends Item> List<T> getAllItems(final ItemGroup root, Class<T> type) {
|
||||
List<T> r = new ArrayList<T>();
|
||||
|
||||
Stack<ItemGroup> q = new Stack<ItemGroup>();
|
||||
q.push(root);
|
||||
|
||||
while(!q.isEmpty()) {
|
||||
ItemGroup<?> parent = q.pop();
|
||||
for (Item i : parent.getItems()) {
|
||||
if(type.isInstance(i)) {
|
||||
if (i.hasPermission(Item.READ))
|
||||
r.add(type.cast(i));
|
||||
getAllItems(root, type, r);
|
||||
return r;
|
||||
}
|
||||
private static <T extends Item> void getAllItems(final ItemGroup root, Class<T> type, List<T> r) {
|
||||
List<Item> items = new ArrayList<Item>(((ItemGroup<?>) root).getItems());
|
||||
Collections.sort(items, new Comparator<Item>() {
|
||||
@Override public int compare(Item i1, Item i2) {
|
||||
return name(i1).compareToIgnoreCase(name(i2));
|
||||
}
|
||||
String name(Item i) {
|
||||
String n = i.getName();
|
||||
if (i instanceof ItemGroup) {
|
||||
n += '/';
|
||||
}
|
||||
return n;
|
||||
}
|
||||
});
|
||||
for (Item i : items) {
|
||||
if (i instanceof ItemGroup) {
|
||||
getAllItems((ItemGroup) i, type, r);
|
||||
}
|
||||
if (type.isInstance(i)) {
|
||||
if (i.hasPermission(Item.READ)) {
|
||||
r.add(type.cast(i));
|
||||
}
|
||||
if(i instanceof ItemGroup)
|
||||
q.push((ItemGroup)i);
|
||||
}
|
||||
}
|
||||
// sort by relative name, ignoring case
|
||||
Collections.sort(r, new Comparator<T>() {
|
||||
@Override
|
||||
public int compare(T o1, T o2) {
|
||||
if (o1 == null) {
|
||||
if (o2 == null) {
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
if (o2 == null) {
|
||||
return -1;
|
||||
}
|
||||
return o1.getRelativeNameFrom(root).compareToIgnoreCase(o2.getRelativeNameFrom(root));
|
||||
}
|
||||
|
||||
});
|
||||
return r;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user