Skip to content

Commit e008a8d

Browse files
Treiblesschorlectrueden
authored andcommitted
Change matching API
1 parent aca1943 commit e008a8d

7 files changed

Lines changed: 68 additions & 43 deletions

File tree

src/main/java/org/scijava/ops/base/OpRef.java

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,30 @@ public class OpRef {
6363

6464
// -- Static construction methods --
6565

66-
/**
67-
* Creates a new op reference.
68-
*
69-
* @param type1
70-
* first type constraint of the op.
71-
* @param type2
72-
* second type constraint of the op.
73-
* @param outType
74-
* the type of the op's primary output, or null for any type.
75-
* @param args
76-
* arguments to the op.
77-
*/
78-
public static OpRef fromTypes(final Type type1, final Type type2, final Type outType, final Type[] args) {
66+
public static OpRef fromTypes(final Type[] types, final Type outType, final Type... args) {
67+
return new OpRef(null, filterNulls(types), filterNulls(outType), filterNulls(args));
68+
}
69+
70+
public static OpRef fromTypes(final Type type, final Type outType, final Type... args) {
71+
return new OpRef(null, filterNulls(type), filterNulls(outType), filterNulls(args));
72+
}
73+
74+
public static OpRef fromTypes(final Type type1, final Type type2, final Type outType, final Type... args) {
7975
return new OpRef(null, filterNulls(type1, type2), filterNulls(outType), filterNulls(args));
8076
}
77+
78+
public static OpRef fromTypes(final Class<? extends Op> opClass, final Type outType, final Type... args) {
79+
return new OpRef(null, filterNulls(opClass), filterNulls(outType), filterNulls(args));
80+
}
81+
82+
public static OpRef fromType(final Type opType, final Type... args) {
83+
return new OpRef(null, filterNulls(opType), null, filterNulls(args));
84+
}
85+
86+
public static OpRef fromClass(final Class<? extends Op> opClass, final Type... args) {
87+
return new OpRef(null, filterNulls(opClass), null, filterNulls(args));
88+
}
89+
8190

8291
// -- Constructor --
8392

@@ -205,7 +214,8 @@ public int hashCode() {
205214
// -- Utility methods --
206215

207216
public static Type[] filterNulls(final Type... types) {
208-
return Arrays.stream(types).filter(t -> t != null).toArray(Type[]::new);
217+
Type[] ts = Arrays.stream(types).filter(t -> t != null).toArray(Type[]::new);
218+
return ts == null ? null : ts;
209219
}
210220

211221
// -- Helper methods --

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

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,19 +84,34 @@ public Collection<OpInfo> infos() {
8484
return infos;
8585
}
8686

87-
public <T> StructInstance<T> findOpInstance(final Nil<T> opType, final Type[] opAdditionalTypes,
88-
final Type[] inTypes, final Type outType) {
89-
// FIXME - createTypes does not support multiple additional types,
90-
// or multiple output types. We will need to generalize this.
91-
final OpRef ref = OpRef.fromTypes(opType.getType(), //
92-
opAdditionalTypes[0], outType, inTypes);
87+
public <T> StructInstance<T> findOpInstance(final Class<? extends Op> opClass, final Nil<T> specialType, final Type[] inTypes,
88+
final Type outType) {
89+
// FIXME - multiple output types? We will need to generalize this.
90+
final OpRef ref = OpRef.fromTypes(merge(opClass, specialType == null ? null : specialType.getType()), outType, inTypes);
9391
@SuppressWarnings("unchecked")
9492
final StructInstance<T> op = (StructInstance<T>) op(ref);
9593
return op;
9694
}
97-
98-
public <T> T findOp(final Nil<T> opType, final Type[] opAdditionalTypes, final Type[] inTypes,
95+
96+
public <O extends Op> StructInstance<O> findOpInstance(final Class<O> opClass, final Type[] inTypes, final Type outType) {
97+
return findOpInstance(opClass, null, inTypes, outType);
98+
}
99+
100+
public <T> T findOp(final Class<? extends Op> opClass, final Nil<T> specialType, final Type[] inTypes,
99101
final Type outType) {
100-
return findOpInstance(opType, opAdditionalTypes, inTypes, outType).object();
102+
return findOpInstance(opClass, specialType, inTypes, outType).object();
103+
}
104+
105+
public <O extends Op> O findOp(final Class<O> opClass, final Type[] inTypes, final Type outType) {
106+
return findOpInstance(opClass, inTypes, outType).object();
107+
}
108+
109+
private Type[] merge(Type in1, Type... ins) {
110+
Type[] merged = new Type[ins.length + 1];
111+
merged[0] = in1;
112+
for (int i = 0; i < ins.length; i++) {
113+
merged[i+1] = ins[i];
114+
}
115+
return merged;
101116
}
102117
}

src/main/java/org/scijava/ops/util/Computers.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@ private Computers() {
2020
public static <I, O> Computer<I, O> unary(final OpService ops, final Class<? extends Op> opClass,
2121
final Class<I> inputType, final Class<O> outputType) {
2222
return ops.findOp( //
23+
opClass, //
2324
new Nil<Computer<I, O>>() {
2425
}, //
25-
new Type[] { opClass }, //
2626
new Type[] { inputType, outputType }, //
2727
outputType);
2828
}
2929

3030
public static <I1, I2, O> BiComputer<I1, I2, O> binary(final OpService ops, final Class<? extends Op> opClass,
3131
final Class<I1> input1Type, final Class<I2> input2Type, final Class<O> outputType) {
3232
return ops.findOp( //
33+
opClass, //
3334
new Nil<BiComputer<I1, I2, O>>() {
3435
}, //
35-
new Type[] { opClass }, //
3636
new Type[] { input1Type, input2Type, outputType }, //
3737
outputType);
3838
}

src/main/java/org/scijava/ops/util/Functions.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@ private Functions() {
2020
public static <I, O> Function<I, O> unary(final OpService ops, final Class<? extends Op> opClass,
2121
final Class<I> inputType, final Class<O> outputType) {
2222
return ops.findOp( //
23+
opClass,
2324
new Nil<Function<I, O>>() {
2425
}, //
25-
new Type[] { opClass }, //
2626
new Type[] { inputType }, //
2727
outputType);
2828
}
2929

3030
public static <I1, I2, O> BiFunction<I1, I2, O> binary(final OpService ops, final Class<? extends Op> opClass,
3131
final Class<I1> input1Type, final Class<I2> input2Type, final Class<O> outputType) {
3232
return ops.findOp( //
33+
opClass,
3334
new Nil<BiFunction<I1, I2, O>>() {
3435
}, //
35-
new Type[] { opClass }, //
3636
new Type[] { input1Type, input2Type }, //
3737
outputType);
3838
}

src/main/java/org/scijava/ops/util/Inplaces.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@ private Inplaces() {
2020
public static <IO> Inplace<IO> unary(final OpService ops, final Class<? extends Op> opClass,
2121
final Class<IO> inputOutputType) {
2222
return ops.findOp( //
23+
opClass,
2324
new Nil<Inplace<IO>>() {
2425
}, //
25-
new Type[] { opClass }, //
2626
new Type[] { inputOutputType }, //
2727
inputOutputType);
2828
}
2929

3030
public static <IO, I2> BiInplace1<IO, I2> binary1(final OpService ops, final Class<? extends Op> opClass,
3131
final Class<IO> inputOutputType, final Class<I2> input2Type) {
3232
return ops.findOp( //
33+
opClass,
3334
new Nil<BiInplace1<IO, I2>>() {
3435
}, //
35-
new Type[] { opClass }, //
3636
new Type[] { inputOutputType, input2Type}, //
3737
inputOutputType);
3838
}

src/test/java/org/scijava/ops/AdaptersTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ public class AdaptersTest extends AbstractTestEnvironment {
4646
public void testFunctionAsCommand() {
4747
Class<Double> c = Double.class;
4848
Function<Double, Double> sqrtFunction = ops().findOp( //
49+
MathSqrtOp.class,
4950
new Nil<Function<Double, Double>>() {
5051
}, //
51-
new Type[] { MathSqrtOp.class }, //
5252
new Type[] { c }, //
5353
c//
5454
);
@@ -63,9 +63,9 @@ public void testFunctionAsCommand() {
6363
public void testComputerAsCommand() {
6464
Class<double[]> cArray = double[].class;
6565
Computer<double[], double[]> sqrtComputer = ops().findOp( //
66+
MathSqrtOp.class,
6667
new Nil<Computer<double[], double[]>>() {
6768
}, //
68-
new Type[] { MathSqrtOp.class }, //
6969
new Type[] { cArray, cArray }, //
7070
cArray//
7171
);
@@ -81,9 +81,9 @@ public void testComputerAsCommand() {
8181
public void testComputerAsFunction() {
8282
Class<double[]> cArray = double[].class;
8383
final BiComputer<double[], double[], double[]> computer = ops().findOp( //
84+
MathAddOp.class,
8485
new Nil<BiComputer<double[], double[], double[]>>() {
8586
}, //
86-
new Type[] { MathAddOp.class }, //
8787
new Type[] { cArray, cArray, cArray }, //
8888
cArray//
8989
);
@@ -103,9 +103,9 @@ public void testFunctionAsComputer() {
103103
Class<double[]> c = double[].class;
104104
// look up a function: Double result = math.add(Double v1, Double v2)
105105
BiFunction<double[], double[], double[]> function = ops().findOp( //
106+
MathAddOp.class,
106107
new Nil<BiFunction<double[], double[], double[]>>() {
107108
}, //
108-
new Type[] { MathAddOp.class }, //
109109
new Type[] { c, c }, //
110110
c//
111111
);

src/test/java/org/scijava/ops/OpsTest.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ public class OpsTest extends AbstractTestEnvironment {
4949
public void unaryFunction() {
5050
Class<Double> c = Double.class;
5151
Function<Double, Double> sqrtFunction = ops().findOp( //
52+
MathSqrtOp.class,
5253
new Nil<Function<Double, Double>>() {
5354
}, //
54-
new Type[] { MathSqrtOp.class }, //
5555
new Type[] { c }, //
5656
c//
5757
);
@@ -65,9 +65,9 @@ public void binaryFunction() {
6565
Class<Double> c = Double.class;
6666
// look up a function: Double result = math.add(Double v1, Double v2)
6767
BiFunction<Double, Double, Double> function = ops().findOp( //
68+
MathAddOp.class,
6869
new Nil<BiFunction<Double, Double, Double>>() {
6970
}, //
70-
new Type[] { MathAddOp.class }, //
7171
new Type[] { c, c }, //
7272
c//
7373
);
@@ -77,9 +77,9 @@ public void binaryFunction() {
7777

7878
// look up a specific implementation
7979
function = ops().findOp( //
80+
MathAddDoublesFunction.class,
8081
new Nil<BiFunction<Double, Double, Double>>() {
8182
}, //
82-
new Type[] { MathAddDoublesFunction.class }, //
8383
new Type[] { c, c }, //
8484
c//
8585
);
@@ -91,9 +91,9 @@ public void binaryFunction() {
9191
public void unaryComputer() {
9292
Class<double[]> cArray = double[].class;
9393
Computer<double[], double[]> sqrtComputer = ops().findOp( //
94+
MathSqrtOp.class,
9495
new Nil<Computer<double[], double[]>>() {
9596
}, //
96-
new Type[] { MathSqrtOp.class }, //
9797
new Type[] { cArray, cArray }, //
9898
cArray//
9999
);
@@ -109,9 +109,9 @@ public void binaryComputer() {
109109
// double[] v2)
110110
Class<double[]> cArray = double[].class;
111111
final BiComputer<double[], double[], double[]> computer = ops().findOp( //
112+
MathAddOp.class,
112113
new Nil<BiComputer<double[], double[], double[]>>() {
113114
}, //
114-
new Type[] { MathAddOp.class }, //
115115
new Type[] { cArray, cArray, cArray }, //
116116
cArray//
117117
);
@@ -126,9 +126,9 @@ public void binaryComputer() {
126126
public void unaryInplace() {
127127
Class<double[]> cArray = double[].class;
128128
final Inplace<double[]> inplaceSqrt = ops().findOp( //
129+
MathSqrtOp.class,
129130
new Nil<Inplace<double[]>>() {
130131
}, //
131-
new Type[] { MathSqrtOp.class }, //
132132
new Type[] { cArray }, //
133133
cArray//
134134
);
@@ -141,9 +141,9 @@ public void unaryInplace() {
141141
public void binaryInplace() {
142142
Class<double[]> cArray = double[].class;
143143
final BiInplace1<double[], double[]> inplaceAdd = ops().findOp( //
144+
MathAddOp.class,
144145
new Nil<BiInplace1<double[], double[]>>() {
145146
}, //
146-
new Type[] { MathAddOp.class }, //
147147
new Type[] { cArray, cArray }, //
148148
cArray//
149149
);
@@ -157,9 +157,9 @@ public void binaryInplace() {
157157
public void testSecondaryInputs() {
158158
Class<Double> c = Double.class;
159159
StructInstance<Function<Double, Double>> powerConstantFunctionStructInstance = ops().findOpInstance( //
160+
MathPowerOp.class,
160161
new Nil<Function<Double, Double>>() {
161162
}, //
162-
new Type[] { MathPowerOp.class }, //
163163
new Type[] { c, c }, //
164164
c//
165165
);
@@ -168,19 +168,19 @@ public void testSecondaryInputs() {
168168
assert power3.apply(2.0).equals(8.0);
169169

170170
BiFunction<Double, Double, Double> powerFunction = ops().findOp( //
171+
MathPowerOp.class,
171172
new Nil<BiFunction<Double, Double, Double>>() {
172173
}, //
173-
new Type[] { MathPowerOp.class }, //
174174
new Type[] { c, c }, //
175175
c//
176176
);
177177
assert powerFunction.apply(2.0, 3.0).equals(8.0);
178178

179179
Class<double[]> cArray = double[].class;
180180
StructInstance<Computer<double[], double[]>> powerConstantComputerStructInstance = ops().findOpInstance( //
181+
MathPowerOp.class,
181182
new Nil<Computer<double[], double[]>>() {
182183
}, //
183-
new Type[] { MathPowerOp.class }, //
184184
new Type[] { cArray, cArray, c }, //
185185
cArray//
186186
);

0 commit comments

Comments
 (0)