Skip to content

Commit 3fca49f

Browse files
committed
Merge pull request #162 from scijava/default-values
ModuleService: add getDefaultValue
2 parents ef3f1a0 + 5d91cd2 commit 3fca49f

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
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: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public class DefaultModuleService extends AbstractService implements
9292
private PrefService prefService;
9393

9494
@Parameter
95-
private ConvertService conversionService;
95+
private ConvertService convertService;
9696

9797
/** Index of registered modules. */
9898
private ModuleIndex moduleIndex;
@@ -269,7 +269,7 @@ public <T> void save(final ModuleItem<T> item, final T value) {
269269
final String sValue = value == null ? "" : value.toString();
270270

271271
// do not persist if object cannot be converted back from a string
272-
if (!conversionService.supports(sValue, item.getType())) return;
272+
if (!convertService.supports(sValue, item.getType())) return;
273273

274274
final String persistKey = item.getPersistKey();
275275
if (persistKey == null || persistKey.isEmpty()) {
@@ -297,9 +297,25 @@ public <T> T load(final ModuleItem<T> item) {
297297
// if persisted value has never been set before return null
298298
if (sValue == null) return null;
299299

300-
return conversionService.convert(sValue, item.getType());
300+
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
@@ -399,7 +415,7 @@ private void assignInputs(final Module module,
399415
}
400416
else {
401417
final Class<?> type = input.getType();
402-
converted = conversionService.convert(value, type);
418+
converted = convertService.convert(value, type);
403419
if (value != null && converted == null) {
404420
log.error("For input " + name + ": incompatible object " +
405421
value.getClass().getName() + " for type " + type.getName());

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)