Skip to content

Commit 87d902c

Browse files
committed
ObjectIndex: support PluginIndex better
We used to require overriding the add() method for only one reason: the type by which the objects are indexed in PluginIndex are not obtained by getClass(), but by getPluginType(). This suggests that the base class actually needs a more flexible way to obtain the types by which to index, so let's do that instead. This will help with the upcoming performance enhancements which will bypass the add() method completely for SortedObjectIndex' addAll(). Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent 6b878da commit 87d902c

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

src/main/java/org/scijava/object/ObjectIndex.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ public boolean isEmpty() {
147147

148148
@Override
149149
public boolean contains(final Object o) {
150-
return get(o.getClass()).contains(o);
150+
if (!getBaseClass().isAssignableFrom(o.getClass())) return false;
151+
return get(getType((E)o)).contains(o);
151152
}
152153

153154
@Override
@@ -245,12 +246,18 @@ public int compare(final Class<?> c1, final Class<?> c2) {
245246

246247
/** Adds the object to all compatible type lists. */
247248
protected boolean add(final E o, final boolean batch) {
248-
return add(o, o.getClass(), batch);
249+
return add(o, getType(o), batch);
250+
}
251+
252+
/** Return the type by which to index the object. */
253+
protected Class<?> getType(final E o) {
254+
return o.getClass();
249255
}
250256

251257
/** Removes the object from all compatible type lists. */
252258
protected boolean remove(final Object o, final boolean batch) {
253-
return remove(o, o.getClass(), batch);
259+
if (!getBaseClass().isAssignableFrom(o.getClass())) return false;
260+
return remove(o, getType((E)o), batch);
254261
}
255262

256263
private Map<Class<?>, List<List<?>>> type2Lists = new HashMap<Class<?>, List<List<?>>>();

src/main/java/org/scijava/plugin/PluginIndex.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,19 +135,13 @@ public <PT extends SciJavaPlugin> List<PluginInfo<PT>> getPlugins(
135135
// -- Internal methods --
136136

137137
/**
138-
* Adds the plugin to all type lists compatible with its plugin type.
139-
* <p>
140-
* NB: This behavior differs from the default
141-
* {@link org.scijava.object.ObjectIndex} behavior in that the {@code info}
142-
* object's actual type hierarchy is not used for classification, but rather
143-
* the object is classified according to {@link PluginInfo#getPluginType()}.
144-
* </p>
138+
* Overrides the type by which the entries are indexed.
145139
*
146140
* @see PluginInfo#getPluginType()
147141
*/
148142
@Override
149-
protected boolean add(final PluginInfo<?> info, final boolean batch) {
150-
return add(info, info.getPluginType(), batch);
143+
protected Class<?> getType(final PluginInfo<?> info) {
144+
return info.getPluginType();
151145
}
152146

153147
/**

0 commit comments

Comments
 (0)