Skip to content

Commit e13d21f

Browse files
committed
Merge branch 'default-methods'
This branch migrates a bunch of method implementations from abstract and default implementation classes to the implemented interfaces themselves. Starting with Java 8, it is possible to provide default method implementations directly within the interfaces when possible; doing this is nice for a lot of reasons.
2 parents 10bbc86 + bf7c0c6 commit e13d21f

File tree

77 files changed

+944
-1338
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+944
-1338
lines changed

src/main/java/org/scijava/AbstractContextual.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,4 @@ public Context context() {
6262
public Context getContext() {
6363
return context;
6464
}
65-
66-
@Override
67-
public void setContext(final Context context) {
68-
context.inject(this);
69-
}
70-
7165
}

src/main/java/org/scijava/AbstractUIDetails.java

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131

3232
package org.scijava;
3333

34-
import org.scijava.util.ClassUtils;
35-
import org.scijava.util.MiscUtils;
3634
import org.scijava.util.StringMaker;
3735

3836
/**
@@ -88,31 +86,6 @@ public String toString() {
8886

8987
// -- UIDetails methods --
9088

91-
@Override
92-
public String getTitle() {
93-
// use object label, if available
94-
if (getLabel() != null && !getLabel().isEmpty()) return getLabel();
95-
96-
// use name of leaf menu item, if available
97-
if (menuPath != null && menuPath.size() > 0) {
98-
final MenuEntry menuLeaf = menuPath.getLeaf();
99-
final String menuName = menuLeaf.getName();
100-
if (menuName != null && !menuName.isEmpty()) return menuName;
101-
}
102-
103-
// use object name, if available
104-
if (getName() != null && !getName().isEmpty()) return getName();
105-
106-
// use the unique identifier, if available
107-
if (this instanceof Identifiable) {
108-
final String id = ((Identifiable) this).getIdentifier();
109-
if (id != null) return id;
110-
}
111-
112-
// use class name as a last resort
113-
return getClass().getSimpleName();
114-
}
115-
11689
@Override
11790
public MenuPath getMenuPath() {
11891
return menuPath;
@@ -209,34 +182,4 @@ public double getPriority() {
209182
public void setPriority(final double priority) {
210183
this.priority = priority;
211184
}
212-
213-
// -- Comparable methods --
214-
215-
@Override
216-
public int compareTo(final Prioritized that) {
217-
if (that == null) return 1;
218-
219-
// compare priorities
220-
final int priorityCompare = Priority.compare(this, that);
221-
if (priorityCompare != 0) return priorityCompare;
222-
223-
// compare classes
224-
final int classCompare = ClassUtils.compare(getClass(), that.getClass());
225-
if (classCompare != 0) return classCompare;
226-
227-
if (!(that instanceof UIDetails)) return 1;
228-
final UIDetails uiDetails = (UIDetails) that;
229-
230-
// compare names
231-
final String thisName = getName();
232-
final String thatName = uiDetails.getName();
233-
final int nameCompare = MiscUtils.compare(thisName, thatName);
234-
if (nameCompare != 0) return nameCompare;
235-
236-
// compare titles
237-
final String thisTitle = getTitle();
238-
final String thatTitle = uiDetails.getTitle();
239-
return MiscUtils.compare(thisTitle, thatTitle);
240-
}
241-
242185
}

src/main/java/org/scijava/Contextual.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,14 @@ public interface Contextual {
6969
* </p>
7070
*
7171
* @see Context#inject(Object)
72-
* @see AbstractContextual for an example of how to implement this interface
7372
* @throws IllegalStateException If the object already has a context.
7473
* @throws IllegalArgumentException If the object has a required
7574
* {@link Service} parameter (see {@link Parameter#required()})
7675
* which is not available from the context.
7776
*/
78-
void setContext(Context context);
77+
default void setContext(final Context context) {
78+
context.inject(this);
79+
}
80+
7981

8082
}

src/main/java/org/scijava/Disposable.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public interface Disposable {
4242
* Performs any needed cleanup of the object's services, in preparation for
4343
* the object being retired (e.g., to make garbage collection possible).
4444
*/
45-
void dispose();
46-
45+
default void dispose() {
46+
// NB: Do nothing by default.
47+
}
4748
}

src/main/java/org/scijava/UIDetails.java

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131

3232
package org.scijava;
3333

34+
import org.scijava.util.ClassUtils;
35+
import org.scijava.util.MiscUtils;
36+
3437
/**
3538
* An interface defining details useful for generating relevant user interface
3639
* elements.
@@ -56,7 +59,30 @@ public interface UIDetails extends BasicDetails, Prioritized {
5659
* <li>Item's class name, without package prefix</li>
5760
* </ol>
5861
*/
59-
String getTitle();
62+
default String getTitle() {
63+
// use object label, if available
64+
if (getLabel() != null && !getLabel().isEmpty()) return getLabel();
65+
66+
// use name of leaf menu item, if available
67+
final MenuPath menuPath = getMenuPath();
68+
if (menuPath != null && menuPath.size() > 0) {
69+
final MenuEntry menuLeaf = menuPath.getLeaf();
70+
final String menuName = menuLeaf.getName();
71+
if (menuName != null && !menuName.isEmpty()) return menuName;
72+
}
73+
74+
// use object name, if available
75+
if (getName() != null && !getName().isEmpty()) return getName();
76+
77+
// use the unique identifier, if available
78+
if (this instanceof Identifiable) {
79+
final String id = ((Identifiable) this).getIdentifier();
80+
if (id != null) return id;
81+
}
82+
83+
// use class name as a last resort
84+
return getClass().getSimpleName();
85+
}
6086

6187
/** Gets the path to the object's suggested position in the menu structure. */
6288
MenuPath getMenuPath();
@@ -124,4 +150,32 @@ public interface UIDetails extends BasicDetails, Prioritized {
124150
*/
125151
void setSelected(boolean selected);
126152

153+
// -- Comparable methods --
154+
155+
@Override
156+
default int compareTo(final Prioritized that) {
157+
if (that == null) return 1;
158+
159+
// compare priorities
160+
final int priorityCompare = Priority.compare(this, that);
161+
if (priorityCompare != 0) return priorityCompare;
162+
163+
// compare classes
164+
final int classCompare = ClassUtils.compare(getClass(), that.getClass());
165+
if (classCompare != 0) return classCompare;
166+
167+
if (!(that instanceof UIDetails)) return 1;
168+
final UIDetails uiDetails = (UIDetails) that;
169+
170+
// compare names
171+
final String thisName = getName();
172+
final String thatName = uiDetails.getName();
173+
final int nameCompare = MiscUtils.compare(thisName, thatName);
174+
if (nameCompare != 0) return nameCompare;
175+
176+
// compare titles
177+
final String thisTitle = getTitle();
178+
final String thatTitle = uiDetails.getTitle();
179+
return MiscUtils.compare(thisTitle, thatTitle);
180+
}
127181
}

src/main/java/org/scijava/app/AbstractApp.java

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,9 @@
3131

3232
package org.scijava.app;
3333

34-
import java.io.File;
35-
3634
import org.scijava.log.LogService;
3735
import org.scijava.plugin.AbstractRichPlugin;
3836
import org.scijava.plugin.Parameter;
39-
import org.scijava.util.AppUtils;
4037
import org.scijava.util.Manifest;
4138
import org.scijava.util.POM;
4239

@@ -58,11 +55,6 @@ public abstract class AbstractApp extends AbstractRichPlugin implements App {
5855

5956
// -- App methods --
6057

61-
@Override
62-
public String getTitle() {
63-
return getInfo().getName();
64-
}
65-
6658
@Override
6759
public POM getPOM() {
6860
if (pom == null) {
@@ -81,72 +73,8 @@ public Manifest getManifest() {
8173
return manifest;
8274
}
8375

84-
@Override
85-
public String getInfo(boolean mem) {
86-
final String appTitle = getTitle();
87-
final String appVersion = getVersion();
88-
final String javaVersion = System.getProperty("java.version");
89-
final String osArch = System.getProperty("os.arch");
90-
final long maxMem = Runtime.getRuntime().maxMemory();
91-
final long totalMem = Runtime.getRuntime().totalMemory();
92-
final long freeMem = Runtime.getRuntime().freeMemory();
93-
final long usedMem = totalMem - freeMem;
94-
final long usedMB = usedMem / 1048576;
95-
final long maxMB = maxMem / 1048576;
96-
final StringBuilder sb = new StringBuilder();
97-
sb.append(appTitle + " " + appVersion);
98-
sb.append("; Java " + javaVersion + " [" + osArch + "]");
99-
if (mem) sb.append("; " + usedMB + "MB of " + maxMB + "MB");
100-
return sb.toString();
101-
}
102-
103-
@Override
104-
public String getSystemProperty() {
105-
return getInfo().getName().toLowerCase() + ".dir";
106-
}
107-
108-
@Override
109-
public File getBaseDirectory() {
110-
return AppUtils.getBaseDirectory(getSystemProperty(), getClass(), null);
111-
}
112-
11376
@Override
11477
public void about() {
11578
if (log != null) log.info(getInfo(false));
11679
}
117-
118-
@Override
119-
public void prefs() {
120-
// NB: Do nothing.
121-
}
122-
123-
@Override
124-
public void quit() {
125-
getContext().dispose();
126-
}
127-
128-
// -- Versioned methods --
129-
130-
@Override
131-
public String getVersion() {
132-
// NB: We do not use VersionUtils.getVersion(c, groupId, artifactId)
133-
// because that method does not cache the parsed Manifest and/or POM.
134-
// We might have them already parsed here, and if not, we want to
135-
// parse then cache locally, rather than discarding them afterwards.
136-
137-
// try the manifest first, since it might know its build number
138-
final Manifest m = getManifest();
139-
if (m != null) {
140-
final String v = m.getVersion();
141-
if (v != null) return v;
142-
}
143-
// try the POM
144-
final POM p = getPOM();
145-
if (p != null) {
146-
final String v = p.getVersion();
147-
if (v != null) return v;
148-
}
149-
return "Unknown";
150-
}
151-
15280
}

src/main/java/org/scijava/app/App.java

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.scijava.plugin.Plugin;
3737
import org.scijava.plugin.RichPlugin;
3838
import org.scijava.plugin.SingletonPlugin;
39+
import org.scijava.util.AppUtils;
3940
import org.scijava.util.Manifest;
4041
import org.scijava.util.POM;
4142

@@ -56,7 +57,10 @@
5657
public interface App extends RichPlugin, SingletonPlugin {
5758

5859
/** Gets the title of the application. */
59-
String getTitle();
60+
default String getTitle() {
61+
return getInfo().getName();
62+
}
63+
6064

6165
/** The Maven {@code groupId} of the application. */
6266
String getGroupId();
@@ -80,20 +84,40 @@ public interface App extends RichPlugin, SingletonPlugin {
8084
*
8185
* @param mem If true, memory usage information is included.
8286
*/
83-
String getInfo(boolean mem);
87+
default String getInfo(final boolean mem) {
88+
final String appTitle = getTitle();
89+
final String appVersion = getVersion();
90+
final String javaVersion = System.getProperty("java.version");
91+
final String osArch = System.getProperty("os.arch");
92+
final long maxMem = Runtime.getRuntime().maxMemory();
93+
final long totalMem = Runtime.getRuntime().totalMemory();
94+
final long freeMem = Runtime.getRuntime().freeMemory();
95+
final long usedMem = totalMem - freeMem;
96+
final long usedMB = usedMem / 1048576;
97+
final long maxMB = maxMem / 1048576;
98+
final StringBuilder sb = new StringBuilder();
99+
sb.append(appTitle + " " + appVersion);
100+
sb.append("; Java " + javaVersion + " [" + osArch + "]");
101+
if (mem) sb.append("; " + usedMB + "MB of " + maxMB + "MB");
102+
return sb.toString();
103+
}
84104

85105
/**
86106
* A system property which, if set, overrides the base directory of the
87107
* application.
88108
*/
89-
String getSystemProperty();
109+
default String getSystemProperty() {
110+
return getInfo().getName().toLowerCase() + ".dir";
111+
}
90112

91113
/**
92114
* Gets the application's root directory. If the application's system property
93115
* is set, it is used. Otherwise, we scan up the tree from this class for a
94116
* suitable directory.
95117
*/
96-
File getBaseDirectory();
118+
default File getBaseDirectory() {
119+
return AppUtils.getBaseDirectory(getSystemProperty(), getClass(), null);
120+
}
97121

98122
/**
99123
* Displays information about the application. Typically this action
@@ -105,13 +129,17 @@ public interface App extends RichPlugin, SingletonPlugin {
105129
* Displays application preferences. The behavior is application-specific, but
106130
* typically a preferences dialog will be shown onscreen.
107131
*/
108-
void prefs();
132+
default void prefs() {
133+
// NB: Do nothing.
134+
}
109135

110136
/**
111137
* Quits the application. Typically this action will prompt the user about any
112138
* unsaved work first.
113139
*/
114-
void quit();
140+
default void quit() {
141+
getContext().dispose();
142+
}
115143

116144
// -- Versioned methods --
117145

@@ -125,6 +153,24 @@ public interface App extends RichPlugin, SingletonPlugin {
125153
* @return The application version, in {@code major.minor.micro} format.
126154
*/
127155
@Override
128-
String getVersion();
129-
156+
default String getVersion() {
157+
// NB: We do not use VersionUtils.getVersion(c, groupId, artifactId)
158+
// because that method does not cache the parsed Manifest and/or POM.
159+
// We might have them already parsed here, and if not, we want to
160+
// parse then cache locally, rather than discarding them afterwards.
161+
162+
// try the manifest first, since it might know its build number
163+
final Manifest m = getManifest();
164+
if (m != null) {
165+
final String v = m.getVersion();
166+
if (v != null) return v;
167+
}
168+
// try the POM
169+
final POM p = getPOM();
170+
if (p != null) {
171+
final String v = p.getVersion();
172+
if (v != null) return v;
173+
}
174+
return "Unknown";
175+
}
130176
}

0 commit comments

Comments
 (0)