Skip to content

Commit 5d91cd2

Browse files
committed
ModuleService: add getDefaultValue(ModuleItem)
This logic goes into the ModuleService because it needs context: when the item does not specify a min or max value, it leans on the ConvertService to convert from zero (specifically: "0") to the ModuleItem's associated type.
1 parent 3e110e9 commit 5d91cd2

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</parent>
1111

1212
<artifactId>scijava-common</artifactId>
13-
<version>2.40.1-SNAPSHOT</version>
13+
<version>2.41.0-SNAPSHOT</version>
1414

1515
<name>SciJava Common</name>
1616
<description>SciJava Common is a shared library for SciJava software. It provides a plugin framework, with an extensible mechanism for service discovery, backed by its own annotation processor, so that plugins can be loaded dynamically. It is used by both ImageJ and SCIFIO.</description>

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,22 @@ public <T> T load(final ModuleItem<T> item) {
300300
return convertService.convert(sValue, item.getType());
301301
}
302302

303+
@Override
304+
public <T> T getDefaultValue(final ModuleItem<T> item) {
305+
final T min = item.getMinimumValue();
306+
if (min != null) return min;
307+
final T softMin = item.getSoftMinimum();
308+
if (softMin != null) return softMin;
309+
final T max = item.getMaximumValue();
310+
if (max != null) return max;
311+
final T softMax = item.getSoftMaximum();
312+
if (softMax != null) return softMax;
313+
final T zero = convertService.convert("0", item.getType());
314+
if (zero != null) return zero;
315+
// no known default value
316+
return null;
317+
}
318+
303319
// -- Service methods --
304320

305321
@Override

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,4 +285,8 @@ <M extends Module> Future<M> run(M module,
285285
* {@link ModuleItem}.
286286
*/
287287
<T> T load(ModuleItem<T> item);
288+
289+
/** Gets the default value of the given {@link ModuleItem}. */
290+
<T> T getDefaultValue(final ModuleItem<T> item);
291+
288292
}

0 commit comments

Comments
 (0)