Skip to content

Commit 6c190de

Browse files
committed
App: push default method impls to iface
Thank you Java 8.
1 parent 8e23240 commit 6c190de

File tree

2 files changed

+54
-80
lines changed

2 files changed

+54
-80
lines changed

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)