Skip to content

Commit 28a7e7c

Browse files
committed
ObjectIndex: allow lazy registration of objects
1 parent 9a367f1 commit 28a7e7c

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2013 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
*
30+
* The views and conclusions contained in the software and documentation are
31+
* those of the authors and should not be interpreted as representing official
32+
* policies, either expressed or implied, of any organization.
33+
* #L%
34+
*/
35+
36+
package org.scijava.object;
37+
38+
import java.util.Collection;
39+
40+
/**
41+
* Interface for objects created lazily. This interface provides a mechanism to
42+
* register a callback of sorts, so that the {@link ObjectIndex} can request
43+
* creation of objects only when they are needed.
44+
*
45+
* @author Curtis Rueden
46+
*/
47+
public interface LazyObjects<T> {
48+
49+
/** Gets the collection of objects. */
50+
Collection<T> get();
51+
52+
}

src/main/java/org/scijava/object/ObjectIndex.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import java.util.HashMap;
4343
import java.util.Iterator;
4444
import java.util.LinkedHashSet;
45+
import java.util.LinkedList;
4546
import java.util.List;
4647
import java.util.Map;
4748
import java.util.Set;
@@ -87,6 +88,10 @@ public class ObjectIndex<E> implements Collection<E> {
8788

8889
private final Class<E> baseClass;
8990

91+
/** List of objects to add later as needed (i.e., lazily). */
92+
private final List<LazyObjects<? extends E>> pending =
93+
new LinkedList<LazyObjects<? extends E>>();
94+
9095
public ObjectIndex(final Class<E> baseClass) {
9196
this.baseClass = baseClass;
9297
}
@@ -127,12 +132,30 @@ public List<E> getAll() {
127132
* list if no such objects exist (this method never returns null).
128133
*/
129134
public List<E> get(final Class<?> type) {
135+
// lazily register any pending objects
136+
if (!pending.isEmpty()) resolvePending();
137+
130138
List<E> list = retrieveList(type);
131139
// NB: Return a copy of the data, to facilitate thread safety.
132140
list = new ArrayList<E>(list);
133141
return list;
134142
}
135143

144+
/**
145+
* Registers objects which will be created lazily as needed.
146+
* <p>
147+
* This is useful if creation of the objects is expensive for some reason. In
148+
* that case, the object index can wait to actually request and register the
149+
* objects until the next accessor method invocation (i.e.,
150+
* {@link #get(Class)} or {@link #getAll()}).
151+
* </p>
152+
*/
153+
public void addLater(final LazyObjects<? extends E> c) {
154+
synchronized (pending) {
155+
pending.add(c);
156+
}
157+
}
158+
136159
// -- Collection methods --
137160

138161
@Override
@@ -361,6 +384,15 @@ protected List<E> retrieveList(final Class<?> type) {
361384
return list;
362385
}
363386

387+
private void resolvePending() {
388+
synchronized (pending) {
389+
while (!pending.isEmpty()) {
390+
final LazyObjects<? extends E> c = pending.remove(0);
391+
addAll(c.get());
392+
}
393+
}
394+
}
395+
364396
// -- Helper classes --
365397

366398
private static class All {

0 commit comments

Comments
 (0)