Skip to content

Commit a5d0fc0

Browse files
committed
Add named object index and improve ObjectService
1 parent fee2644 commit a5d0fc0

File tree

6 files changed

+186
-5
lines changed

6 files changed

+186
-5
lines changed

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
import java.util.List;
3636

37+
import org.scijava.Named;
3738
import org.scijava.event.EventHandler;
3839
import org.scijava.event.EventService;
3940
import org.scijava.object.event.ObjectCreatedEvent;
@@ -68,7 +69,7 @@ public final class DefaultObjectService extends AbstractService implements
6869
private EventService eventService;
6970

7071
/** Index of registered objects. */
71-
private ObjectIndex<Object> objectIndex;
72+
private NamedObjectIndex<Object> objectIndex;
7273

7374
// -- ObjectService methods --
7475

@@ -92,7 +93,12 @@ public <T> List<T> getObjects(final Class<T> type) {
9293

9394
@Override
9495
public void addObject(final Object obj) {
95-
objectIndex.add(obj);
96+
addObject(obj, null);
97+
}
98+
99+
@Override
100+
public void addObject(Object obj, String name) {
101+
objectIndex.add(obj, name);
96102
eventService.publish(new ObjectsAddedEvent(obj));
97103
}
98104

@@ -102,11 +108,23 @@ public void removeObject(final Object obj) {
102108
eventService.publish(new ObjectsRemovedEvent(obj));
103109
}
104110

111+
@Override
112+
public String getName(Object obj) {
113+
String name = objectIndex.getName(obj);
114+
if (name != null) {
115+
return name;
116+
}
117+
if (obj instanceof Named) {
118+
return ((Named) obj).getName();
119+
}
120+
return obj.toString();
121+
}
122+
105123
// -- Service methods --
106124

107125
@Override
108126
public void initialize() {
109-
objectIndex = new ObjectIndex<>(Object.class);
127+
objectIndex = new NamedObjectIndex<>(Object.class);
110128
}
111129

112130
// -- Event handlers --
@@ -120,5 +138,4 @@ protected void onEvent(final ObjectCreatedEvent event) {
120138
protected void onEvent(final ObjectDeletedEvent event) {
121139
removeObject(event.getObject());
122140
}
123-
124141
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.scijava.object;
2+
3+
import java.util.WeakHashMap;
4+
5+
public class NamedObjectIndex<E> extends ObjectIndex<E> {
6+
7+
private WeakHashMap<Object, String> nameMap;
8+
9+
public NamedObjectIndex(final Class<E> baseClass) {
10+
super(baseClass);
11+
nameMap = new WeakHashMap<>();
12+
}
13+
14+
public boolean add(E object, String name) {
15+
if (name != null)
16+
nameMap.put(object, name);
17+
return add(object);
18+
}
19+
20+
public boolean add(E object, Class<?> type, String name, boolean batch) {
21+
if (name != null)
22+
nameMap.put(object, name);
23+
return add(object, type, batch);
24+
}
25+
26+
public String getName(E object) {
27+
return nameMap.get(object);
28+
}
29+
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
import java.util.List;
3636

37+
import org.scijava.Named;
3738
import org.scijava.event.EventService;
3839
import org.scijava.service.SciJavaService;
3940

@@ -54,9 +55,21 @@ default EventService eventService() {
5455
/** Gets a list of all registered objects compatible with the given type. */
5556
<T> List<T> getObjects(Class<T> type);
5657

58+
/**
59+
* Gets the name belonging to a given object.
60+
*
61+
* If no explicit name was provided at registration time, the name will be
62+
* derived from {@link Named#getName()} if the object implements {@link Named},
63+
* or from the {@link Object#toString()} otherwise
64+
**/
65+
String getName(Object obj);
66+
5767
/** Registers an object with the object service. */
5868
void addObject(Object obj);
5969

70+
/** Registers a named object with the object service. */
71+
void addObject(Object obj, String name);
72+
6073
/** Deregisters an object with the object service. */
6174
void removeObject(Object obj);
6275

src/main/java/org/scijava/widget/DefaultWidgetModel.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.scijava.module.Module;
4848
import org.scijava.module.ModuleItem;
4949
import org.scijava.module.ModuleService;
50+
import org.scijava.object.ObjectService;
5051
import org.scijava.plugin.Parameter;
5152
import org.scijava.thread.ThreadService;
5253
import org.scijava.util.NumberUtils;
@@ -74,6 +75,9 @@ public class DefaultWidgetModel extends AbstractContextual implements WidgetMode
7475
@Parameter
7576
private ModuleService moduleService;
7677

78+
@Parameter
79+
private ObjectService objectService;
80+
7781
@Parameter(required = false)
7882
private LogService log;
7983

@@ -227,7 +231,7 @@ public String[] getChoices() {
227231
final List<?> choicesList = item.getChoices();
228232
final String[] choices = new String[choicesList.size()];
229233
for (int i = 0; i < choices.length; i++) {
230-
choices[i] = choicesList.get(i).toString();
234+
choices[i] = objectService.getName(choicesList.get(i));
231235
}
232236
return choices;
233237
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.scijava.object;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertFalse;
5+
import static org.junit.Assert.assertTrue;
6+
import static org.junit.Assert.fail;
7+
8+
import org.junit.Test;
9+
10+
public class NamedObjectIndexTest {
11+
12+
@Test
13+
public void testNamedObjects() {
14+
NamedObjectIndex<String> index = new NamedObjectIndex<>(String.class);
15+
String obj1 = "obj1";
16+
String name1 = "name1";
17+
String obj2 = "obj1";
18+
String name2 = "name1";
19+
assertTrue(index.add(obj1, name1));
20+
assertTrue(index.add(obj2, String.class, name2, false));
21+
assertTrue(index.contains(obj1));
22+
assertTrue(index.contains(obj2));
23+
assertEquals(name1, index.getName(obj1));
24+
assertEquals(name2, index.getName(obj2));
25+
assertTrue(index.remove(obj1));
26+
assertTrue(index.remove(obj2));
27+
assertFalse(index.contains(obj1));
28+
assertFalse(index.contains(obj2));
29+
}
30+
31+
@Test
32+
public void testNullNames() {
33+
NamedObjectIndex<String> index = new NamedObjectIndex<>(String.class);
34+
String obj1 = "object1";
35+
String name1 = null;
36+
String obj2 = "object2";
37+
String name2 = "";
38+
assertTrue(index.add(obj1, name1));
39+
assertTrue(index.add(obj2, name2));
40+
assertEquals(name1, index.getName(obj1));
41+
assertEquals(name2, index.getName(obj2));
42+
}
43+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package org.scijava.object;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertFalse;
5+
import static org.junit.Assert.assertNotNull;
6+
import static org.junit.Assert.assertTrue;
7+
8+
import org.junit.After;
9+
import org.junit.Before;
10+
import org.junit.Test;
11+
import org.scijava.Context;
12+
import org.scijava.plugin.PluginInfo;
13+
import org.scijava.plugin.SciJavaPlugin;
14+
15+
public class ObjectServiceTest {
16+
17+
private Context context;
18+
private ObjectService objectService;
19+
20+
@Before
21+
public void setUp() {
22+
context = new Context(ObjectService.class);
23+
objectService = context.getService(ObjectService.class);
24+
}
25+
26+
@After
27+
public void tearDown() {
28+
context.dispose();
29+
}
30+
31+
@Test
32+
public void testAddRemoveObjects() {
33+
Object obj1 = new Object();
34+
String name1 = "Object 1";
35+
Object obj2 = "";
36+
Object obj3 = new Double(0.3);
37+
PluginInfo<SciJavaPlugin> obj4 = PluginInfo.create(TestPlugin.class, SciJavaPlugin.class);
38+
obj4.setName("TestPlugin name");
39+
40+
objectService.addObject(obj1, name1);
41+
assertEquals("Name of object 1", name1, objectService.getName(obj1));
42+
objectService.addObject(obj2);
43+
assertEquals("Name of object 2", obj2.toString(), objectService.getName(obj2));
44+
objectService.addObject(obj3, null);
45+
assertEquals("Name of object 3", obj3.toString(), objectService.getName(obj3));
46+
objectService.addObject(obj4);
47+
assertNotNull(objectService.getName(obj4));
48+
assertEquals("Name of object 4", obj4.getName(), objectService.getName(obj4));
49+
50+
assertTrue("Object 1 registered", objectService.getObjects(Object.class).contains(obj1));
51+
assertTrue("Object 2 registered", objectService.getObjects(Object.class).contains(obj2));
52+
assertTrue("Object 3 registered", objectService.getObjects(Object.class).contains(obj3));
53+
assertTrue("Object 4 registered", objectService.getObjects(Object.class).contains(obj4));
54+
55+
objectService.removeObject(obj1);
56+
objectService.removeObject(obj2);
57+
objectService.removeObject(obj3);
58+
objectService.removeObject(obj4);
59+
60+
assertFalse("Object 1 removed", objectService.getObjects(Object.class).contains(obj1));
61+
assertFalse("Object 2 removed", objectService.getObjects(Object.class).contains(obj2));
62+
assertFalse("Object 3 removed", objectService.getObjects(Object.class).contains(obj3));
63+
assertFalse("Object 4 removed", objectService.getObjects(Object.class).contains(obj4));
64+
}
65+
66+
@Test
67+
public void testNamedObjectIndex() {
68+
ObjectIndex<Object> index = objectService.getIndex();
69+
assertTrue(index instanceof NamedObjectIndex);
70+
}
71+
72+
private class TestPlugin implements SciJavaPlugin {
73+
74+
}
75+
}

0 commit comments

Comments
 (0)