Skip to content

Commit c2d607a

Browse files
committed
Push default Service method behavior to interface
Thanks, Java 8.
1 parent 075f96d commit c2d607a

2 files changed

Lines changed: 11 additions & 19 deletions

File tree

‎src/main/java/org/scijava/service/AbstractService.java‎

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
package org.scijava.service;
3333

3434
import org.scijava.Context;
35-
import org.scijava.event.EventService;
3635
import org.scijava.plugin.AbstractRichPlugin;
3736

3837
/**
@@ -59,22 +58,6 @@ public abstract class AbstractService extends AbstractRichPlugin implements
5958
*/
6059
private Context context;
6160

62-
// -- Service methods --
63-
64-
@Override
65-
public void initialize() {
66-
// NB: Do nothing by default.
67-
}
68-
69-
@Override
70-
public void registerEventHandlers() {
71-
// TODO: Consider removing this method in scijava-common 3.0.0.
72-
// Instead, the ServiceHelper could just invoke the lines below directly,
73-
// and there would be one less boilerplate Service method to implement.
74-
final EventService eventService = context().getService(EventService.class);
75-
if (eventService != null) eventService.subscribe(this);
76-
}
77-
7861
// -- Contextual methods --
7962

8063
@Override

‎src/main/java/org/scijava/service/Service.java‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
package org.scijava.service;
3333

3434
import org.scijava.Disposable;
35+
import org.scijava.event.EventService;
3536
import org.scijava.plugin.Plugin;
3637
import org.scijava.plugin.RichPlugin;
3738

@@ -58,7 +59,9 @@ public interface Service extends RichPlugin, Disposable {
5859
* when initializing the service. It should not be called a second time.
5960
* </p>
6061
*/
61-
void initialize();
62+
default void initialize() {
63+
// NB: Do nothing by default.
64+
}
6265

6366
/**
6467
* Registers the service's event handler methods.
@@ -68,6 +71,12 @@ public interface Service extends RichPlugin, Disposable {
6871
* when initializing the service. It should not be called a second time.
6972
* </p>
7073
*/
71-
void registerEventHandlers();
74+
default void registerEventHandlers() {
75+
// TODO: Consider removing this method in scijava-common 3.0.0.
76+
// Instead, the ServiceHelper could just invoke the lines below directly,
77+
// and there would be one less boilerplate Service method to implement.
78+
final EventService eventService = context().getService(EventService.class);
79+
if (eventService != null) eventService.subscribe(this);
80+
}
7281

7382
}

0 commit comments

Comments
 (0)