Skip to content

Commit cd3a2e4

Browse files
committed
ModuleService: add getModuleById method
This looks up Identifiable ModuleInfos by their identifiers. Migrated from the ModuleUtils class of the imagej/imagej-omero project.
1 parent 6dff174 commit cd3a2e4

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/main/java/org/scijava/module/DefaultModuleService.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import java.util.concurrent.ExecutionException;
4242
import java.util.concurrent.Future;
4343

44+
import org.scijava.Identifiable;
4445
import org.scijava.MenuPath;
4546
import org.scijava.Priority;
4647
import org.scijava.convert.ConvertService;
@@ -140,6 +141,17 @@ public List<ModuleInfo> getModules() {
140141
return moduleIndex.getAll();
141142
}
142143

144+
@Override
145+
public ModuleInfo getModuleById(final String id) {
146+
// TODO: Cache identifiers in a hash?
147+
for (final ModuleInfo info : getModules()) {
148+
if (!(info instanceof Identifiable)) continue;
149+
final String infoID = ((Identifiable) info).getIdentifier();
150+
if (id.equals(infoID)) return info;
151+
}
152+
return null;
153+
}
154+
143155
@Override
144156
public ModuleInfo getModuleForAccelerator(final Accelerator acc) {
145157
for (final ModuleInfo info : getModules()) {

src/main/java/org/scijava/module/ModuleService.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.util.Map;
3737
import java.util.concurrent.Future;
3838

39+
import org.scijava.Identifiable;
3940
import org.scijava.Prioritized;
4041
import org.scijava.input.Accelerator;
4142
import org.scijava.module.process.ModulePostprocessor;
@@ -90,6 +91,14 @@ public interface ModuleService extends SciJavaService {
9091
/** Gets the list of available modules. */
9192
List<ModuleInfo> getModules();
9293

94+
/**
95+
* Gets the module with the given identifier string.
96+
*
97+
* @param id The identifier string corresponding to the desired module.
98+
* @return The {@link Identifiable} module with the given identifier.
99+
*/
100+
ModuleInfo getModuleById(String id);
101+
93102
/**
94103
* Gets the module for a given keyboard shortcut.
95104
*

0 commit comments

Comments
 (0)