Skip to content

Commit 34c47d0

Browse files
committed
ScriptService: move default method impls to iface
1 parent 63824f7 commit 34c47d0

File tree

2 files changed

+46
-82
lines changed

2 files changed

+46
-82
lines changed

src/main/java/org/scijava/script/DefaultScriptService.java

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

3434
import java.io.File;
35-
import java.io.Reader;
36-
import java.io.StringReader;
3735
import java.math.BigDecimal;
3836
import java.math.BigInteger;
3937
import java.util.ArrayList;
@@ -120,21 +118,6 @@ public ScriptLanguageIndex getIndex() {
120118
return scriptLanguageIndex();
121119
}
122120

123-
@Override
124-
public List<ScriptLanguage> getLanguages() {
125-
return new ArrayList<>(getIndex());
126-
}
127-
128-
@Override
129-
public ScriptLanguage getLanguageByExtension(final String extension) {
130-
return getIndex().getByExtension(extension);
131-
}
132-
133-
@Override
134-
public ScriptLanguage getLanguageByName(final String name) {
135-
return getIndex().getByName(name);
136-
}
137-
138121
// -- ScriptService methods - scripts --
139122

140123
@Override
@@ -189,34 +172,6 @@ public Future<ScriptModule> run(final File file, final boolean process,
189172
return run(getOrCreate(file), process, inputMap);
190173
}
191174

192-
@Override
193-
public Future<ScriptModule> run(final String path, final String script,
194-
final boolean process, final Object... inputs)
195-
{
196-
return run(path, new StringReader(script), process, inputs);
197-
}
198-
199-
@Override
200-
public Future<ScriptModule> run(final String path, final String script,
201-
final boolean process, final Map<String, Object> inputMap)
202-
{
203-
return run(path, new StringReader(script), process, inputMap);
204-
}
205-
206-
@Override
207-
public Future<ScriptModule> run(final String path, final Reader reader,
208-
final boolean process, final Object... inputs)
209-
{
210-
return run(new ScriptInfo(getContext(), path, reader), process, inputs);
211-
}
212-
213-
@Override
214-
public Future<ScriptModule> run(final String path, final Reader reader,
215-
final boolean process, final Map<String, Object> inputMap)
216-
{
217-
return run(new ScriptInfo(getContext(), path, reader), process, inputMap);
218-
}
219-
220175
@Override
221176
public Future<ScriptModule> run(final ScriptInfo info, final boolean process,
222177
final Object... inputs)
@@ -231,21 +186,6 @@ public Future<ScriptModule> run(final ScriptInfo info, final boolean process,
231186
return cast(moduleService.run(info, process, inputMap));
232187
}
233188

234-
@Override
235-
public boolean canHandleFile(final File file) {
236-
return getIndex().canHandleFile(file);
237-
}
238-
239-
@Override
240-
public boolean canHandleFile(final String fileName) {
241-
return getIndex().canHandleFile(fileName);
242-
}
243-
244-
@Override
245-
public void addAlias(final Class<?> type) {
246-
addAlias(type.getSimpleName(), type);
247-
}
248-
249189
@Override
250190
public void addAlias(final String alias, final Class<?> type) {
251191
aliasMap().put(alias, type);
@@ -274,13 +214,6 @@ public synchronized Class<?> lookupClass(final String alias)
274214
}
275215
}
276216

277-
// -- PTService methods --
278-
279-
@Override
280-
public Class<ScriptLanguage> getPluginType() {
281-
return ScriptLanguage.class;
282-
}
283-
284217
// -- Service methods --
285218

286219
@Override

src/main/java/org/scijava/script/ScriptService.java

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@
3333

3434
import java.io.File;
3535
import java.io.FileNotFoundException;
36-
import java.io.IOException;
3736
import java.io.Reader;
37+
import java.io.StringReader;
38+
import java.util.ArrayList;
3839
import java.util.Collection;
3940
import java.util.List;
4041
import java.util.Map;
@@ -83,13 +84,19 @@ public interface ScriptService extends SingletonService<ScriptLanguage>,
8384
* This method does the same thing as {@link #getInstances()}.
8485
* </p>
8586
*/
86-
List<ScriptLanguage> getLanguages();
87+
default List<ScriptLanguage> getLanguages() {
88+
return new ArrayList<>(getIndex());
89+
}
8790

8891
/** Gets the scripting language that handles the given file extension. */
89-
ScriptLanguage getLanguageByExtension(String extension);
92+
default ScriptLanguage getLanguageByExtension(final String extension) {
93+
return getIndex().getByExtension(extension);
94+
}
9095

9196
/** Gets the scripting language with the given name. */
92-
ScriptLanguage getLanguageByName(String name);
97+
default ScriptLanguage getLanguageByName(final String name) {
98+
return getIndex().getByName(name);
99+
}
93100

94101
// -- Scripts --
95102

@@ -180,8 +187,11 @@ Future<ScriptModule> run(File file, boolean process,
180187
* @return {@link Future} of the module instance being executed. Calling
181188
* {@link Future#get()} will block until execution is complete.
182189
*/
183-
Future<ScriptModule> run(String path, String script, boolean process,
184-
Object... inputs) throws IOException, ScriptException;
190+
default Future<ScriptModule> run(final String path, final String script,
191+
final boolean process, final Object... inputs)
192+
{
193+
return run(path, new StringReader(script), process, inputs);
194+
}
185195

186196
/**
187197
* Executes the given script.
@@ -201,8 +211,11 @@ Future<ScriptModule> run(String path, String script, boolean process,
201211
* @return {@link Future} of the module instance being executed. Calling
202212
* {@link Future#get()} will block until execution is complete.
203213
*/
204-
Future<ScriptModule> run(String path, String script, boolean process,
205-
Map<String, Object> inputMap) throws IOException, ScriptException;
214+
default Future<ScriptModule> run(final String path, final String script,
215+
final boolean process, final Map<String, Object> inputMap)
216+
{
217+
return run(path, new StringReader(script), process, inputMap);
218+
}
206219

207220
/**
208221
* Executes the given script.
@@ -224,8 +237,11 @@ Future<ScriptModule> run(String path, String script, boolean process,
224237
* @return {@link Future} of the module instance being executed. Calling
225238
* {@link Future#get()} will block until execution is complete.
226239
*/
227-
Future<ScriptModule> run(String path, Reader reader, boolean process,
228-
Object... inputs) throws IOException, ScriptException;
240+
default Future<ScriptModule> run(final String path, final Reader reader,
241+
final boolean process, final Object... inputs)
242+
{
243+
return run(new ScriptInfo(getContext(), path, reader), process, inputs);
244+
}
229245

230246
/**
231247
* Executes the given script.
@@ -245,8 +261,11 @@ Future<ScriptModule> run(String path, Reader reader, boolean process,
245261
* @return {@link Future} of the module instance being executed. Calling
246262
* {@link Future#get()} will block until execution is complete.
247263
*/
248-
Future<ScriptModule> run(String path, Reader reader, boolean process,
249-
Map<String, Object> inputMap) throws IOException, ScriptException;
264+
default Future<ScriptModule> run(final String path, final Reader reader,
265+
final boolean process, final Map<String, Object> inputMap)
266+
{
267+
return run(new ScriptInfo(getContext(), path, reader), process, inputMap);
268+
}
250269

251270
/**
252271
* Executes the given script.
@@ -286,18 +305,30 @@ Future<ScriptModule> run(ScriptInfo info, boolean process,
286305
Map<String, Object> inputMap);
287306

288307
/** TODO */
289-
boolean canHandleFile(File file);
308+
default boolean canHandleFile(final File file) {
309+
return getIndex().canHandleFile(file);
310+
}
290311

291312
/** TODO */
292-
boolean canHandleFile(String fileName);
313+
default boolean canHandleFile(final String fileName) {
314+
return getIndex().canHandleFile(fileName);
315+
}
293316

294317
/** TODO */
295-
void addAlias(Class<?> type);
318+
default void addAlias(final Class<?> type) {
319+
addAlias(type.getSimpleName(), type);
320+
}
296321

297322
/** TODO */
298323
void addAlias(String alias, Class<?> type);
299324

300325
/** TODO */
301326
Class<?> lookupClass(String typeName) throws ScriptException;
302327

328+
// -- PTService methods --
329+
330+
@Override
331+
default Class<ScriptLanguage> getPluginType() {
332+
return ScriptLanguage.class;
333+
}
303334
}

0 commit comments

Comments
 (0)