3636
3737import org .scijava .Named ;
3838import org .scijava .event .EventService ;
39+ import org .scijava .object .event .ObjectsAddedEvent ;
40+ import org .scijava .object .event .ObjectsRemovedEvent ;
3941import org .scijava .service .SciJavaService ;
4042
4143/**
@@ -50,10 +52,15 @@ default EventService eventService() {
5052 }
5153
5254 /** Gets the index of available objects. */
53- ObjectIndex <Object > getIndex ();
55+ NamedObjectIndex <Object > getIndex ();
5456
5557 /** Gets a list of all registered objects compatible with the given type. */
56- <T > List <T > getObjects (Class <T > type );
58+ default <T > List <T > getObjects (final Class <T > type ) {
59+ final List <Object > list = getIndex ().get (type );
60+ @ SuppressWarnings ("unchecked" )
61+ final List <T > result = (List <T >) list ;
62+ return result ;
63+ }
5764
5865 /**
5966 * Gets the name belonging to a given object.
@@ -63,17 +70,36 @@ default EventService eventService() {
6370 * {@link Named}, or from the {@link Object#toString()} otherwise. It is
6471 * guaranteed that this method will not return {@code null}.
6572 * </p>
66- */
67- String getName (Object obj );
73+ **/
74+ default String getName (final Object obj ) {
75+ if (obj == null ) throw new NullPointerException ();
76+ final String name = getIndex ().getName (obj );
77+ if (name != null ) return name ;
78+ if (obj instanceof Named ) {
79+ final String n = ((Named ) obj ).getName ();
80+ if (n != null ) return n ;
81+ }
82+ final String s = obj .toString ();
83+ if (s != null ) return s ;
84+ return obj .getClass ().getName () + "@" + Integer .toHexString (obj .hashCode ());
85+ }
6886
6987 /** Registers an object with the object service. */
70- void addObject (Object obj );
88+ default void addObject (Object obj ) {
89+ addObject (obj , null );
90+ }
7191
7292 /** Registers a named object with the object service. */
73- void addObject (Object obj , String name );
93+ default void addObject (final Object obj , final String name ) {
94+ getIndex ().add (obj , name );
95+ eventService ().publish (new ObjectsAddedEvent (obj ));
96+ }
7497
7598 /** Deregisters an object with the object service. */
76- void removeObject (Object obj );
99+ default void removeObject (final Object obj ) {
100+ getIndex ().remove (obj );
101+ eventService ().publish (new ObjectsRemovedEvent (obj ));
102+ }
77103
78104 // -- Deprecated methods --
79105
0 commit comments