FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add NameService and NameProvider by imagejan · Pull Request #425 · scijava/scijava-common · GitHub

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .java  (6) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
12 changes: 12 additions & 0 deletions src/main/java/org/scijava/names/NameProvider.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,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
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,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
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
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
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
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
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,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;
}
}
}
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
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

Back | FazBrowse Home | New Git URL