Skip to content

Commit 1e9c40d

Browse files
committed
ScriptInfo: make assignAttribute code more compact
And hopefully easier to read. This also generalizes it to any Object values, not just String values, which will come in handy shortly.
1 parent ea8c647 commit 1e9c40d

File tree

1 file changed

+33
-68
lines changed

1 file changed

+33
-68
lines changed

src/main/java/org/scijava/script/ScriptInfo.java

Lines changed: 33 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -421,78 +421,43 @@ private <T> void addItem(final String name, final Class<T> type,
421421
}
422422

423423
private <T> void assignAttribute(final DefaultMutableModuleItem<T> item,
424-
final String key, final String value) throws ScriptException
424+
final String k, final Object v) throws ScriptException
425425
{
426426
// CTR: There must be an easier way to do this.
427427
// Just compile the thing using javac? Or parse via javascript, maybe?
428-
if ("callback".equalsIgnoreCase(key)) {
429-
item.setCallback(value);
430-
}
431-
else if ("choices".equalsIgnoreCase(key)) {
428+
if (is(k, "callback")) item.setCallback(as(v, String.class));
429+
else if (is(k, "choices")) {
432430
// FIXME: Regex above won't handle {a,b,c} syntax.
433-
// item.setChoices(choices);
434-
}
435-
else if ("columns".equalsIgnoreCase(key)) {
436-
item.setColumnCount(convertService.convert(value, int.class));
437-
}
438-
else if ("description".equalsIgnoreCase(key)) {
439-
item.setDescription(value);
440-
}
441-
else if ("initializer".equalsIgnoreCase(key)) {
442-
item.setInitializer(value);
443-
}
444-
else if ("type".equalsIgnoreCase(key)) {
445-
item.setIOType(convertService.convert(value, ItemIO.class));
446-
}
447-
else if ("label".equalsIgnoreCase(key)) {
448-
item.setLabel(value);
449-
}
450-
else if ("max".equalsIgnoreCase(key)) {
451-
item.setMaximumValue(convertService.convert(value, item.getType()));
452-
}
453-
else if ("min".equalsIgnoreCase(key)) {
454-
item.setMinimumValue(convertService.convert(value, item.getType()));
455-
}
456-
else if ("name".equalsIgnoreCase(key)) {
457-
item.setName(value);
458-
}
459-
else if ("persist".equalsIgnoreCase(key)) {
460-
item.setPersisted(convertService.convert(value, boolean.class));
461-
}
462-
else if ("persistKey".equalsIgnoreCase(key)) {
463-
item.setPersistKey(value);
464-
}
465-
else if ("required".equalsIgnoreCase(key)) {
466-
item.setRequired(convertService.convert(value, boolean.class));
467-
}
468-
else if ("softMax".equalsIgnoreCase(key)) {
469-
item.setSoftMaximum(convertService.convert(value, item.getType()));
470-
}
471-
else if ("softMin".equalsIgnoreCase(key)) {
472-
item.setSoftMinimum(convertService.convert(value, item.getType()));
473-
}
474-
else if ("stepSize".equalsIgnoreCase(key)) {
475-
try {
476-
final double stepSize = Double.parseDouble(value);
477-
item.setStepSize(stepSize);
478-
}
479-
catch (final NumberFormatException exc) {
480-
log.warn("Script parameter " + item.getName() +
481-
" has an invalid stepSize: " + value);
482-
}
483-
}
484-
else if ("style".equalsIgnoreCase(key)) {
485-
item.setWidgetStyle(value);
486-
}
487-
else if ("visibility".equalsIgnoreCase(key)) {
488-
item.setVisibility(convertService.convert(value, ItemVisibility.class));
489-
}
490-
else if ("value".equalsIgnoreCase(key)) {
491-
item.setDefaultValue(convertService.convert(value, item.getType()));
492-
}
493-
else {
494-
throw new ScriptException("Invalid attribute name: " + key);
495-
}
431+
// item.setChoices(list(v, item.getType()));
432+
}
433+
else if (is(k, "columns")) item.setColumnCount(as(v, int.class));
434+
else if (is(k, "description")) item.setDescription(as(v, String.class));
435+
else if (is(k, "initializer")) item.setInitializer(as(v, String.class));
436+
else if (is(k, "type")) item.setIOType(as(v, ItemIO.class));
437+
else if (is(k, "label")) item.setLabel(as(v, String.class));
438+
else if (is(k, "max")) item.setMaximumValue(as(v, item.getType()));
439+
else if (is(k, "min")) item.setMinimumValue(as(v, item.getType()));
440+
else if (is(k, "name")) item.setName(as(v, String.class));
441+
else if (is(k, "persist")) item.setPersisted(as(v, boolean.class));
442+
else if (is(k, "persistKey")) item.setPersistKey(as(v, String.class));
443+
else if (is(k, "required")) item.setRequired(as(v, boolean.class));
444+
else if (is(k, "softMax")) item.setSoftMaximum(as(v, item.getType()));
445+
else if (is(k, "softMin")) item.setSoftMinimum(as(v, item.getType()));
446+
else if (is(k, "stepSize")) item.setStepSize(as(v, double.class));
447+
else if (is(k, "style")) item.setWidgetStyle(as(v, String.class));
448+
else if (is(k, "visibility")) item.setVisibility(as(v, ItemVisibility.class));
449+
else if (is(k, "value")) item.setDefaultValue(as(v, item.getType()));
450+
else throw new ScriptException("Invalid attribute name: " + k);
451+
}
452+
453+
/** Super terse comparison helper method. */
454+
private boolean is(final String key, final String desired) {
455+
return desired.equalsIgnoreCase(key);
456+
}
457+
458+
/** Super terse conversion helper method. */
459+
private <T> T as(final Object v, final Class<T> type) {
460+
return convertService.convert(v, type);
496461
}
497462

498463
/**

0 commit comments

Comments
 (0)