Skip to content

Commit 46f64db

Browse files
committed
Allow filtering instances before they become singletons
The DefaultSingletonService used to instantiate all the plugins matching the given type, but one might want to narrow down what becomes a singleton even further, e.g. when ImageJ2 is looking for the current platform: it wants to instantiate all plugins implementing the Platform class, but then only keep the one that claims that it matches the current platform. Therefore, let's introduce the filterInstances(List<PT>) method that returns the unmodified list of instances by default, but can be overridden in subclasses. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent a483943 commit 46f64db

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/main/java/org/scijava/plugin/AbstractSingletonService.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ public ArrayList<Object> get() {
9999

100100
private void createInstances() {
101101
instances =
102-
Collections.unmodifiableList(getPluginService().createInstancesOfType(
103-
getPluginType()));
102+
Collections.unmodifiableList(filterInstances(getPluginService()
103+
.createInstancesOfType(getPluginType())));
104104

105105
log.info("Found " + instances.size() + " " +
106106
getPluginType().getSimpleName() + " plugins.");
@@ -111,4 +111,14 @@ private void createInstances() {
111111
}
112112
}
113113

114+
/**
115+
* Allows subclasses to exclude instances.
116+
*
117+
* @param list the initial list of instances
118+
* @return the filtered list of instances
119+
*/
120+
protected List<? extends PT> filterInstances(List<PT> list) {
121+
return list;
122+
}
123+
114124
}

0 commit comments

Comments
 (0)