Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
ObjectIndex: only resolve relevant pending objects
If a pending object will not be of a type compatible with the
requested type, then skip resolution of that pending object.

This improves performance of the get(Class) and getAll() methods in
cases where there are a large number of pending objects in the index.

It also helps to eliminate difficulties like those in issue #90.
  • Loading branch information
ctrueden committed May 12, 2015
commit 29ff80b02b4d51803bb397c928caf792324ea878
25 changes: 21 additions & 4 deletions src/main/java/org/scijava/object/ObjectIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public List<E> getAll() {
*/
public List<E> get(final Class<?> type) {
// lazily register any pending objects
if (!pending.isEmpty()) resolvePending();
if (!pending.isEmpty()) resolvePending(type);

List<E> list = retrieveList(type);
// NB: Return a copy of the data, to facilitate thread safety.
Expand Down Expand Up @@ -380,15 +380,32 @@ protected List<E> retrieveList(final Class<?> type) {
return list;
}

private void resolvePending() {
private void resolvePending(final Class<?> type) {
synchronized (pending) {
while (!pending.isEmpty()) {
final LazyObjects<? extends E> c = pending.remove(0);
for (int p = pending.size() - 1; p >= 0; p--) {
final LazyObjects<? extends E> 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<? extends E> 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 {
Expand Down