-
Notifications
You must be signed in to change notification settings - Fork 53
Add named object index and improve ObjectService #374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a5d0fc0
Add named object index and improve ObjectService
imagejan 765d6ef
ObjectService: ensure getName() returns non-null
ctrueden ce965b9
NamedObjectIndex: add missing javadoc
ctrueden d5dfb5d
ObjectService: push default impls to interface
ctrueden File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package org.scijava.object; | ||
|
||
import java.util.WeakHashMap; | ||
|
||
/** | ||
* An {@link ObjectIndex} where each object can have an associated name. | ||
* | ||
* @author Jan Eglinger | ||
*/ | ||
public class NamedObjectIndex<E> extends ObjectIndex<E> { | ||
|
||
private WeakHashMap<Object, String> nameMap; | ||
|
||
public NamedObjectIndex(final Class<E> baseClass) { | ||
super(baseClass); | ||
nameMap = new WeakHashMap<>(); | ||
} | ||
|
||
public boolean add(E object, String name) { | ||
if (name != null) | ||
nameMap.put(object, name); | ||
return add(object); | ||
} | ||
|
||
public boolean add(E object, Class<?> type, String name, boolean batch) { | ||
if (name != null) | ||
nameMap.put(object, name); | ||
return add(object, type, batch); | ||
} | ||
|
||
public String getName(E object) { | ||
return nameMap.get(object); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/test/java/org/scijava/object/NamedObjectIndexTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.scijava.object; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.junit.Assert.fail; | ||
|
||
import org.junit.Test; | ||
|
||
public class NamedObjectIndexTest { | ||
|
||
@Test | ||
public void testNamedObjects() { | ||
NamedObjectIndex<String> index = new NamedObjectIndex<>(String.class); | ||
String obj1 = "obj1"; | ||
String name1 = "name1"; | ||
String obj2 = "obj1"; | ||
String name2 = "name1"; | ||
assertTrue(index.add(obj1, name1)); | ||
assertTrue(index.add(obj2, String.class, name2, false)); | ||
assertTrue(index.contains(obj1)); | ||
assertTrue(index.contains(obj2)); | ||
assertEquals(name1, index.getName(obj1)); | ||
assertEquals(name2, index.getName(obj2)); | ||
assertTrue(index.remove(obj1)); | ||
assertTrue(index.remove(obj2)); | ||
assertFalse(index.contains(obj1)); | ||
assertFalse(index.contains(obj2)); | ||
} | ||
|
||
@Test | ||
public void testNullNames() { | ||
NamedObjectIndex<String> index = new NamedObjectIndex<>(String.class); | ||
String obj1 = "object1"; | ||
String name1 = null; | ||
String obj2 = "object2"; | ||
String name2 = ""; | ||
assertTrue(index.add(obj1, name1)); | ||
assertTrue(index.add(obj2, name2)); | ||
assertEquals(name1, index.getName(obj1)); | ||
assertEquals(name2, index.getName(obj2)); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package org.scijava.object; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.scijava.Context; | ||
import org.scijava.plugin.PluginInfo; | ||
import org.scijava.plugin.SciJavaPlugin; | ||
|
||
public class ObjectServiceTest { | ||
|
||
private Context context; | ||
private ObjectService objectService; | ||
|
||
@Before | ||
public void setUp() { | ||
context = new Context(ObjectService.class); | ||
objectService = context.getService(ObjectService.class); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
context.dispose(); | ||
} | ||
|
||
@Test | ||
public void testAddRemoveObjects() { | ||
Object obj1 = new Object(); | ||
String name1 = "Object 1"; | ||
Object obj2 = ""; | ||
Object obj3 = new Double(0.3); | ||
PluginInfo<SciJavaPlugin> obj4 = PluginInfo.create(TestPlugin.class, SciJavaPlugin.class); | ||
obj4.setName("TestPlugin name"); | ||
|
||
objectService.addObject(obj1, name1); | ||
assertEquals("Name of object 1", name1, objectService.getName(obj1)); | ||
objectService.addObject(obj2); | ||
assertEquals("Name of object 2", obj2.toString(), objectService.getName(obj2)); | ||
objectService.addObject(obj3, null); | ||
assertEquals("Name of object 3", obj3.toString(), objectService.getName(obj3)); | ||
objectService.addObject(obj4); | ||
assertNotNull(objectService.getName(obj4)); | ||
assertEquals("Name of object 4", obj4.getName(), objectService.getName(obj4)); | ||
|
||
assertTrue("Object 1 registered", objectService.getObjects(Object.class).contains(obj1)); | ||
assertTrue("Object 2 registered", objectService.getObjects(Object.class).contains(obj2)); | ||
assertTrue("Object 3 registered", objectService.getObjects(Object.class).contains(obj3)); | ||
assertTrue("Object 4 registered", objectService.getObjects(Object.class).contains(obj4)); | ||
|
||
objectService.removeObject(obj1); | ||
objectService.removeObject(obj2); | ||
objectService.removeObject(obj3); | ||
objectService.removeObject(obj4); | ||
|
||
assertFalse("Object 1 removed", objectService.getObjects(Object.class).contains(obj1)); | ||
assertFalse("Object 2 removed", objectService.getObjects(Object.class).contains(obj2)); | ||
assertFalse("Object 3 removed", objectService.getObjects(Object.class).contains(obj3)); | ||
assertFalse("Object 4 removed", objectService.getObjects(Object.class).contains(obj4)); | ||
} | ||
|
||
@Test | ||
public void testNamedObjectIndex() { | ||
ObjectIndex<Object> index = objectService.getIndex(); | ||
assertTrue(index instanceof NamedObjectIndex); | ||
} | ||
|
||
private class TestPlugin implements SciJavaPlugin { | ||
|
||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change to
DefaultWidgetModel
is probably superfluous. I figured that dropdown choices such as for images and other objects will be rendered byObjectWidget
and require changing the renderer for list cells as done in scijava/scijava-ui-swing@2411016.The
getChoices
method ofWidgetModel
seems to be exclusively used for thechoices
attribute ofParameter
s, right? What confused me was the generic typingList<?>
ofitem.getChoices()
.In summary, I am not sure whether we should change this line to use
ObjectService
, or leave it unchanged.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Intuitively, using the
ObjectService
seems less hacky, so let's change it for now, and revert if any problems arise.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Judging from:
scijava-common/src/main/java/org/scijava/module/AbstractModuleItem.java
Lines 276 to 280 in 9231219
... the
ModuleItem#getChoices()
seems to be used forenum
s, other than forString
choices.But I didn't find an actual application case where the choice widget is used when and
enum
parameter is requested. Is there any?In any case, the above line will probably never be called on
Named
objects or on objects registered to theObjectService
, so I'll remove it in a follow-up commit.