diff --git a/src/main/java/org/scijava/object/ObjectIndex.java b/src/main/java/org/scijava/object/ObjectIndex.java index b8e46bab7..5dcd1f1e4 100644 --- a/src/main/java/org/scijava/object/ObjectIndex.java +++ b/src/main/java/org/scijava/object/ObjectIndex.java @@ -129,7 +129,7 @@ public List getAll() { */ public List get(final Class type) { // lazily register any pending objects - if (!pending.isEmpty()) resolvePending(); + if (!pending.isEmpty()) resolvePending(type); List list = retrieveList(type); // NB: Return a copy of the data, to facilitate thread safety. @@ -380,15 +380,32 @@ protected List retrieveList(final Class type) { return list; } - private void resolvePending() { + private void resolvePending(final Class type) { synchronized (pending) { - while (!pending.isEmpty()) { - final LazyObjects c = pending.remove(0); + for (int p = pending.size() - 1; p >= 0; p--) { + final LazyObjects c = pending.get(p); + + // NB: If this pending callback returns objects of a different + // type than the one we are interested in, it can be skipped. + if (!isCompatibleType(c, type)) continue; + + // trigger the callback and add the results + pending.remove(p); addAll(c.get()); } } } + // -- Helper methods -- + + private boolean isCompatibleType(final LazyObjects c, + final Class type) + { + if (type == All.class) return true; + final Class cType = c.getType(); + return cType.isAssignableFrom(type) || type.isAssignableFrom(cType); + } + // -- Helper classes -- private static class All {