Skip to content

Commit a753c33

Browse files
committed
ScriptLanguageIndex: clarify add behavior cases
When adding a script language to the index, behavior depends on a couple of factors: - whether each key is already in the map - whether that key points to the same value, or different one - whether the gently flag is set We now log what happens in more situations, especially in debug mode.
1 parent f29650e commit a753c33

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

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

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,32 @@ public boolean add(final ScriptLanguage language) {
132132
private boolean put(final String type, final Map<String, ScriptLanguage> map,
133133
final String key, final ScriptLanguage value, final boolean gently)
134134
{
135-
if (gently && map.containsKey(key)) {
136-
if (log != null) {
137-
log.warn("Not registering " + type + " '" + key +
138-
"' for scripting language " + value.getClass().getName() +
139-
": existing=" + map.get(key).getClass().getName());
135+
final ScriptLanguage existing = map.get(key);
136+
137+
if (existing == value) {
138+
// Duplicate key/value pair; do not overwrite.
139+
if (log != null && log.isDebug()) {
140+
// In debug mode, warn about the duplicate (since it is atypical).
141+
log.debug(overwriteMessage(false, type, key, value, existing));
140142
}
141143
return false;
142144
}
145+
146+
if (existing != null) {
147+
// Conflicting value; behavior depends on mode.
148+
if (gently) {
149+
// Do not overwrite the previous value.
150+
if (log != null && log.isWarn()) {
151+
log.warn(overwriteMessage(false, type, key, value, existing));
152+
}
153+
return false;
154+
}
155+
if (log != null && log.isDebug()) {
156+
// In debug mode, warn about overwriting.
157+
log.debug(overwriteMessage(true, type, key, value, existing));
158+
}
159+
}
160+
143161
map.put(key, value);
144162
return true;
145163
}
@@ -149,4 +167,14 @@ private ScriptLanguage wrap(final ScriptEngineFactory factory) {
149167
return new AdaptedScriptLanguage(factory);
150168
}
151169

170+
/** Helper method of {@link #put}. */
171+
private String overwriteMessage(final boolean overwrite, final String type,
172+
final String key, final ScriptLanguage proposed,
173+
final ScriptLanguage existing)
174+
{
175+
return (overwrite ? "Overwriting " : "Not overwriting ") + type + //
176+
" '" + key + "':\n\tproposed = " + proposed.getClass().getName() +
177+
"\n\texisting = " + existing.getClass().getName();
178+
}
179+
152180
}

0 commit comments

Comments
 (0)