◐ Shell
reader mode source ↗
Skip to content
Open
Show file tree
Changes from all commits
File filter
Conversations
Jump to
Diff view
Apply and reload
Show whitespace
Diff view
Apply and reload
12 changes: 12 additions & 0 deletions src/main/java/org/scijava/names/NameProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
27 changes: 27 additions & 0 deletions src/main/java/org/scijava/names/NameService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
6 changes: 6 additions & 0 deletions src/main/java/org/scijava/object/ObjectService.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import org.scijava.Named;
import org.scijava.event.EventService;
import org.scijava.object.event.ObjectsAddedEvent;
import org.scijava.object.event.ObjectsRemovedEvent;
import org.scijava.service.SciJavaService;
Expand Down Expand Up @@ -72,10 +73,15 @@ default String getName(final Object obj) {
if (obj == null) throw new NullPointerException();
final String name = getIndex().getName(obj);
if (name != null) return name;
if (obj instanceof Named) {
final String n = ((Named) obj).getName();
if (n != null) return n;
}
final String s = obj.toString();
if (s != null) return s;
return obj.getClass().getName() + "@" + Integer.toHexString(obj.hashCode());
Expand Down
70 changes: 70 additions & 0 deletions src/test/java/org/scijava/names/NameProviderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
13 changes: 12 additions & 1 deletion src/test/java/org/scijava/object/ObjectServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@
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 {
@@ -47,7 +52,7 @@ public class ObjectServiceTest {

@Before
public void setUp() {
context = new Context(ObjectService.class);
objectService = context.getService(ObjectService.class);
}

@@ -64,6 +69,8 @@ public void testAddRemoveObjects() {
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));
Expand All @@ -74,6 +81,10 @@ public void testAddRemoveObjects() {
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));
Expand Down
Toggle all file notes Toggle all file annotations