Skip to content
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

Add NameService and NameProvider #425

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
package org.scijava.names;

import org.scijava.plugin.HandlerPlugin;

public interface NameProvider extends HandlerPlugin<Object> {
public String getName(Object thing);

@Override
default Class<Object> getType() {
return Object.class;
}
}
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 @@
package org.scijava.names;

import org.scijava.plugin.AbstractHandlerService;
import org.scijava.plugin.HandlerService;
import org.scijava.plugin.Plugin;
import org.scijava.service.SciJavaService;
import org.scijava.service.Service;

@Plugin(type=Service.class)
public class NameService extends AbstractHandlerService<Object, NameProvider> implements HandlerService<Object, NameProvider>, SciJavaService {

@Override
public Class<NameProvider> getPluginType() {
return NameProvider.class;
}

@Override
public Class<Object> getType() {
return Object.class;
}

public String getName(Object thing) {
NameProvider handler = getHandler(thing);
if (handler == null) return null;
return handler.getName(thing);
}
}
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.names.NameService;
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;
// Instances of Named
if (obj instanceof Named) {
final String n = ((Named) obj).getName();
if (n != null) return n;
}
// Objects for which a NameProvider exists
final String name2 = context().service(NameService.class).getName(obj);
if (name2 != null) return name2;
// Fallback
final String s = obj.toString();
if (s != null) return s;
return obj.getClass().getName() + "@" + Integer.toHexString(obj.hashCode());
Expand Down
1 change: 1 addition & 0 deletions src/test/java/org/scijava/ContextCreationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public void testFull() {
org.scijava.main.DefaultMainService.class,
org.scijava.menu.DefaultMenuService.class,
org.scijava.module.DefaultModuleService.class,
org.scijava.names.NameService.class,
org.scijava.object.DefaultObjectService.class,
org.scijava.options.DefaultOptionsService.class,
org.scijava.parse.DefaultParseService.class,
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 @@
package org.scijava.names;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.scijava.Context;
import org.scijava.plugin.AbstractHandlerPlugin;
import org.scijava.plugin.Plugin;
import org.scijava.plugin.PluginInfo;
import org.scijava.plugin.PluginService;

public class NameProviderTest {

private Context context;

@Before
public void setUp() throws Exception {
context = new Context();
context.service(PluginService.class).addPlugin(PluginInfo.create(ThirdPartyObjectNameProvider.class, NameProvider.class));
}

@After
public void tearDown() throws Exception {
context.dispose();
}

@Test
public void test() {
String expected = "foo";
ThirdPartyObject foo = new ThirdPartyObject(expected);
NameProvider handler = context.service(NameService.class).getHandler(foo);
assertNotNull(handler);
assertEquals(expected, handler.getName(foo));
}

public static class ThirdPartyObjectNameProvider extends AbstractHandlerPlugin<Object> implements NameProvider {

@Override
public String getName(Object thing) {
return ((ThirdPartyObject) thing).someNonStandardMethod();
}

@Override
public boolean supports(Object thing) {
//System.err.println("NameProvider queried with " + thing + " of class " + thing.getClass());
return ThirdPartyObject.class.isAssignableFrom(thing.getClass());
}

}

public static class ThirdPartyObject {
private String name;

public ThirdPartyObject (String name) {
this.name = name;
}

public String someNonStandardMethod() {
return name;
}

@Override
public String toString() {
return null;
}
}
}
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.names.NameProviderTest.ThirdPartyObject;
import org.scijava.names.NameProviderTest.ThirdPartyObjectNameProvider;
import org.scijava.names.NameProvider;
import org.scijava.names.NameService;
import org.scijava.plugin.PluginInfo;
import org.scijava.plugin.PluginService;
import org.scijava.plugin.SciJavaPlugin;

public class ObjectServiceTest {
Expand All @@ -47,7 +52,7 @@ public class ObjectServiceTest {

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

Expand All @@ -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");
String name5 = "Third-party object";
ThirdPartyObject obj5 = new ThirdPartyObject(name5);

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));
assertNotNull(objectService.getName(obj5));
assertEquals("Name of object 5 before adding NameProvider", obj5.getClass().getName() + "@" + Integer.toHexString(obj5.hashCode()), objectService.getName(obj5));
context.service(PluginService.class).addPlugin(PluginInfo.create(ThirdPartyObjectNameProvider.class, NameProvider.class));
assertEquals("Name of object 5 after adding NameProvider", obj5.someNonStandardMethod(), objectService.getName(obj5));

assertTrue("Object 1 registered", objectService.getObjects(Object.class).contains(obj1));
assertTrue("Object 2 registered", objectService.getObjects(Object.class).contains(obj2));
Expand Down