Skip to content

Commit eb53ac4

Browse files
gselzerctrueden
authored andcommitted
Extract OpService interface
1 parent 078f56a commit eb53ac4

File tree

8 files changed

+104
-44
lines changed

8 files changed

+104
-44
lines changed

scijava/scijava-ops/src/main/java/org/scijava/ops/OpInfo.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,6 @@ public interface OpInfo extends Comparable<OpInfo> {
3434
/** Gets the hints declared in the {@link OpHints} annotation */
3535
Hints declaredHints();
3636

37-
default Hints formHints(OpHints h) {
38-
if (h == null) return new ImmutableHints(new String[0]);
39-
return new ImmutableHints(h.hints());
40-
}
41-
4237
/** Gets the op's input parameters. */
4338
default List<Member<?>> inputs() {
4439
return OpUtils.inputs(struct());

scijava/scijava-ops/src/main/java/org/scijava/ops/OpService.java

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,19 @@
2626
* POSSIBILITY OF SUCH DAMAGE.
2727
* #L%
2828
*/
29+
2930
package org.scijava.ops;
3031

31-
import org.scijava.ops.impl.DefaultOpEnvironment;
32-
import org.scijava.plugin.Plugin;
33-
import org.scijava.service.AbstractService;
3432
import org.scijava.service.SciJavaService;
35-
import org.scijava.service.Service;
3633

3734
/**
3835
* Service to provide a list of available ops structured in a prefix tree and to
3936
* search for ops matching specified types.
4037
*
38+
* @author Gabriel Selzer
4139
* @author David Kolb
4240
*/
43-
@Plugin(type = Service.class)
44-
public class OpService extends AbstractService implements SciJavaService {
45-
46-
private OpEnvironment env;
41+
public interface OpService extends SciJavaService {
4742

4843
/**
4944
* Begins declaration of an op matching request for locating an op with a
@@ -55,20 +50,9 @@ public class OpService extends AbstractService implements SciJavaService {
5550
* @return An {@link OpBuilder} for refining the search criteria for an op.
5651
* @see OpBuilder
5752
*/
58-
public OpBuilder op(final String opName) {
59-
return env().op(opName);
60-
}
53+
public OpBuilder op(final String opName);
6154

6255
/** Retrieves the motherlode of available ops. */
63-
public OpEnvironment env() {
64-
if (env == null) initEnv();
65-
return env;
66-
}
67-
68-
// -- Helper methods - lazy initialization --
56+
public OpEnvironment env();
6957

70-
private synchronized void initEnv() {
71-
if (env != null) return;
72-
env = new DefaultOpEnvironment(context());
73-
}
7458
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* #%L
3+
* SciJava Operations: a framework for reusable algorithms.
4+
* %%
5+
* Copyright (C) 2018 SciJava developers.
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* #L%
28+
*/
29+
30+
package org.scijava.ops.impl;
31+
32+
import org.scijava.ops.OpBuilder;
33+
import org.scijava.ops.OpEnvironment;
34+
import org.scijava.ops.OpService;
35+
import org.scijava.plugin.Plugin;
36+
import org.scijava.service.AbstractService;
37+
import org.scijava.service.Service;
38+
39+
/**
40+
* Service to provide a list of available ops structured in a prefix tree and to
41+
* search for ops matching specified types.
42+
*
43+
* @author David Kolb
44+
*/
45+
@Plugin(type = Service.class)
46+
public class DefaultOpService extends AbstractService implements OpService {
47+
48+
private OpEnvironment env;
49+
50+
/**
51+
* Begins declaration of an op matching request for locating an op with a
52+
* particular name. Additional criteria are specified as chained method calls
53+
* on the returned {@link OpBuilder} object. See {@link OpBuilder} for
54+
* examples.
55+
*
56+
* @param opName The name of the op to be matched.
57+
* @return An {@link OpBuilder} for refining the search criteria for an op.
58+
* @see OpBuilder
59+
*/
60+
@Override
61+
public OpBuilder op(final String opName) {
62+
return env().op(opName);
63+
}
64+
65+
/** Retrieves the motherlode of available ops. */
66+
@Override
67+
public OpEnvironment env() {
68+
if (env == null) initEnv();
69+
return env;
70+
}
71+
72+
// -- Helper methods - lazy initialization --
73+
74+
private synchronized void initEnv() {
75+
if (env != null) return;
76+
env = new DefaultOpEnvironment(context());
77+
}
78+
}

scijava/scijava-ops/src/main/java/org/scijava/ops/matcher/OpAdaptationInfo.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,6 @@ public OpAdaptationInfo(OpInfo srcInfo, Type type, Function<Object, Object> adap
5656
this.hints = new ImmutableHints(hintList.toArray(String[]::new));
5757
}
5858

59-
@Override
60-
public Hints formHints(OpHints h) {
61-
// NB we don't use Arrays.toList() here because we cannot add to that list!
62-
List<String> hintList = Arrays.stream(h.hints()).collect(Collectors.toList());
63-
hintList.remove(Adaptation.ALLOWED);
64-
hintList.add(Adaptation.FORBIDDEN);
65-
return new ImmutableHints(hintList.toArray(String[]::new));
66-
}
67-
6859
@Override
6960
public List<OpDependencyMember<?>> dependencies() {
7061
return srcInfo.dependencies();

scijava/scijava-ops/src/main/java/org/scijava/ops/matcher/OpClassInfo.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.scijava.ops.OpHints;
4242
import org.scijava.ops.OpInfo;
4343
import org.scijava.ops.OpUtils;
44+
import org.scijava.ops.hint.ImmutableHints;
4445
import org.scijava.param.ParameterStructs;
4546
import org.scijava.param.ValidityException;
4647
import org.scijava.plugin.Plugin;
@@ -190,4 +191,9 @@ private static double priorityFromAnnotation(Class<?> annotationBearer) {
190191
return opAnnotation == null ? Priority.NORMAL : opAnnotation.priority();
191192
}
192193

194+
private Hints formHints(OpHints h) {
195+
if (h == null) return new ImmutableHints(new String[0]);
196+
return new ImmutableHints(h.hints());
197+
}
198+
193199
}

scijava/scijava-ops/src/main/java/org/scijava/ops/matcher/OpFieldInfo.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.scijava.ops.OpHints;
4343
import org.scijava.ops.OpInfo;
4444
import org.scijava.ops.OpUtils;
45+
import org.scijava.ops.hint.ImmutableHints;
4546
import org.scijava.param.ParameterStructs;
4647
import org.scijava.param.ValidityException;
4748
import org.scijava.param.ValidityProblem;
@@ -186,4 +187,11 @@ public String toString() {
186187
return OpUtils.opString(this);
187188
}
188189

190+
// -- Helper methods -- //
191+
192+
private Hints formHints(OpHints h) {
193+
if (h == null) return new ImmutableHints(new String[0]);
194+
return new ImmutableHints(h.hints());
195+
}
196+
189197
}

scijava/scijava-ops/src/main/java/org/scijava/ops/matcher/OpMethodInfo.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.scijava.ops.OpInfo;
5151
import org.scijava.ops.OpMethod;
5252
import org.scijava.ops.OpUtils;
53+
import org.scijava.ops.hint.ImmutableHints;
5354
import org.scijava.ops.util.Adapt;
5455
import org.scijava.param.ParameterStructs;
5556
import org.scijava.param.ValidityException;
@@ -359,4 +360,10 @@ public AnnotatedElement getAnnotationBearer() {
359360
return method;
360361
}
361362

363+
// -- Helper methods -- //
364+
365+
private Hints formHints(OpHints h) {
366+
if (h == null) return new ImmutableHints(new String[0]);
367+
return new ImmutableHints(h.hints());
368+
}
362369
}

scijava/scijava-ops/src/main/java/org/scijava/ops/simplify/SimplifiedOpInfo.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,6 @@ public SimplifiedOpInfo(OpInfo info, OpEnvironment env, SimplificationMetadata m
6161
this.hints = new ImmutableHints(hintList.toArray(String[]::new));
6262
}
6363

64-
@Override
65-
public Hints formHints(OpHints h) {
66-
// NB we don't use Arrays.toList() here because we cannot add to that list!
67-
List<String> hintList = Arrays.stream(h.hints()).collect(Collectors.toList());
68-
hintList.remove(Adaptation.ALLOWED);
69-
hintList.add(Adaptation.FORBIDDEN);
70-
return new ImmutableHints(hintList.toArray(String[]::new));
71-
}
72-
7364
public OpInfo srcInfo() {
7465
return srcInfo;
7566
}

0 commit comments

Comments
 (0)