Skip to content

Commit 74ae9fc

Browse files
Treiblesschorlectrueden
authored andcommitted
Always use Nil for type checking
1 parent 8ce0454 commit 74ae9fc

10 files changed

Lines changed: 121 additions & 125 deletions

File tree

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

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -63,30 +63,9 @@ public class OpRef {
6363

6464
// -- Static construction methods --
6565

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));
66+
public static OpRef fromTypes(final Type[] types, final Type[] outTypes, final Type... args) {
67+
return new OpRef(null, filterNulls(types), filterNulls(outTypes), filterNulls(args));
6868
}
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) {
75-
return new OpRef(null, filterNulls(type1, type2), filterNulls(outType), filterNulls(args));
76-
}
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-
9069

9170
// -- Constructor --
9271

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030

3131
import java.lang.reflect.Type;
3232
import java.util.ArrayList;
33+
import java.util.Arrays;
3334
import java.util.Collection;
34-
import java.util.Optional;
3535

3636
import org.scijava.InstantiableException;
3737
import org.scijava.log.LogService;
@@ -80,10 +80,9 @@ public Collection<OpInfo> infos() {
8080
}
8181

8282
public <T> StructInstance<T> findOpInstance(final Class<? extends Op> opClass, final Nil<T> specialType,
83-
final Type[] inTypes, final Type outType, final Object... secondaryArgs) {
83+
final Nil<?>[] inTypes, final Nil<?>[] outTypes, final Object... secondaryArgs) {
8484
// FIXME - multiple output types? We will need to generalize this.
85-
final OpRef ref = OpRef.fromTypes(merge(opClass, specialType == null ? null : specialType.getType()), outType,
86-
inTypes);
85+
final OpRef ref = OpRef.fromTypes(merge(opClass, toTypes(specialType)), toTypes(outTypes), toTypes(inTypes));
8786

8887
// Find single match which matches the specified types
8988
@SuppressWarnings("unchecked")
@@ -104,9 +103,14 @@ public <T> StructInstance<T> findOpInstance(final Class<? extends Op> opClass, f
104103
return op;
105104
}
106105

107-
public <T> T findOp(final Class<? extends Op> opClass, final Nil<T> specialType, final Type[] inTypes,
108-
final Type outType, final Object... secondaryArgs) {
109-
return findOpInstance(opClass, specialType, inTypes, outType, secondaryArgs).object();
106+
public <T> T findOp(final Class<? extends Op> opClass, final Nil<T> specialType, final Nil<?>[] inTypes,
107+
final Nil<?>[] outTypes, final Object... secondaryArgs) {
108+
return findOpInstance(opClass, specialType, inTypes, outTypes, secondaryArgs).object();
109+
}
110+
111+
public <T> T findOp(final Class<? extends Op> opClass, final Nil<T> specialType, final Nil<?>[] inTypes,
112+
final Nil<?> outType, final Object... secondaryArgs) {
113+
return findOpInstance(opClass, specialType, inTypes, new Nil[] { outType }, secondaryArgs).object();
110114
}
111115

112116
public MatchingResult findTypeMatches(final OpRef ref) {
@@ -125,4 +129,8 @@ private Type[] merge(Type in1, Type... ins) {
125129
}
126130
return merged;
127131
}
132+
133+
private Type[] toTypes(Nil<?>... nils) {
134+
return Arrays.stream(nils).filter(n -> n != null).map(n -> n.getType()).toArray(Type[]::new);
135+
}
128136
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package org.scijava.ops.util;
22

3-
import java.lang.reflect.Type;
4-
53
import org.scijava.ops.BiComputer;
64
import org.scijava.ops.Computer;
75
import org.scijava.ops.Op;
@@ -18,24 +16,26 @@ private Computers() {
1816
}
1917

2018
public static <I, O> Computer<I, O> unary(final OpService ops, final Class<? extends Op> opClass,
21-
final Class<I> inputType, final Class<O> outputType, final Object... secondaryArgs) {
19+
final Nil<I> inputType, final Nil<O> outputType, final Object... secondaryArgs) {
2220
return ops.findOp( //
2321
opClass, //
2422
new Nil<Computer<I, O>>() {
2523
}, //
26-
new Type[] { inputType, outputType }, //
27-
outputType, secondaryArgs);
24+
new Nil[] { inputType, outputType }, //
25+
new Nil[] { outputType }, //
26+
secondaryArgs);
2827
}
2928

3029
public static <I1, I2, O> BiComputer<I1, I2, O> binary(final OpService ops, final Class<? extends Op> opClass,
31-
final Class<I1> input1Type, final Class<I2> input2Type, final Class<O> outputType,
30+
final Nil<I1> input1Type, final Nil<I2> input2Type, final Nil<O> outputType,
3231
final Object... secondaryArgs) {
3332
return ops.findOp( //
3433
opClass, //
3534
new Nil<BiComputer<I1, I2, O>>() {
3635
}, //
37-
new Type[] { input1Type, input2Type, outputType }, //
38-
outputType, secondaryArgs);
36+
new Nil[] { input1Type, input2Type, outputType }, //
37+
new Nil[] { outputType }, //
38+
secondaryArgs);
3939
}
4040

4141
}

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.scijava.ops.util;
22

3-
import java.lang.reflect.Type;
43
import java.util.function.BiFunction;
54
import java.util.function.Function;
65

@@ -18,25 +17,25 @@ private Functions() {
1817
}
1918

2019
public static <I, O> Function<I, O> unary(final OpService ops, final Class<? extends Op> opClass,
21-
final Class<I> inputType, final Class<O> outputType, final Object... secondaryArgs) {
20+
final Nil<I> inputType, final Nil<O> outputType, final Object... secondaryArgs) {
2221
return ops.findOp( //
2322
opClass, //
2423
new Nil<Function<I, O>>() {
2524
}, //
26-
new Type[] { inputType }, //
27-
outputType, //
25+
new Nil[] { inputType }, //
26+
new Nil[] { outputType }, //
2827
secondaryArgs);
2928
}
3029

3130
public static <I1, I2, O> BiFunction<I1, I2, O> binary(final OpService ops, final Class<? extends Op> opClass,
32-
final Class<I1> input1Type, final Class<I2> input2Type, final Class<O> outputType,
31+
final Nil<I1> input1Type, final Nil<I2> input2Type, final Nil<O> outputType,
3332
final Object... secondaryArgs) {
3433
return ops.findOp( //
3534
opClass, //
3635
new Nil<BiFunction<I1, I2, O>>() {
3736
}, //
38-
new Type[] { input1Type, input2Type }, //
39-
outputType, //
37+
new Nil[] { input1Type, input2Type }, //
38+
new Nil[] { outputType }, //
4039
secondaryArgs);
4140
}
4241

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package org.scijava.ops.util;
22

3-
import java.lang.reflect.Type;
4-
53
import org.scijava.ops.BiInplace1;
64
import org.scijava.ops.Inplace;
75
import org.scijava.ops.Op;
@@ -18,24 +16,24 @@ private Inplaces() {
1816
}
1917

2018
public static <IO> Inplace<IO> unary(final OpService ops, final Class<? extends Op> opClass,
21-
final Class<IO> inputOutputType, final Object... secondaryArgs) {
19+
final Nil<IO> inputOutputType, final Object... secondaryArgs) {
2220
return ops.findOp( //
2321
opClass, //
2422
new Nil<Inplace<IO>>() {
2523
}, //
26-
new Type[] { inputOutputType }, //
27-
inputOutputType, //
24+
new Nil[] { inputOutputType }, //
25+
new Nil[] { inputOutputType }, //
2826
secondaryArgs);
2927
}
3028

3129
public static <IO, I2> BiInplace1<IO, I2> binary1(final OpService ops, final Class<? extends Op> opClass,
32-
final Class<IO> inputOutputType, final Class<I2> input2Type, final Object... secondaryArgs) {
30+
final Nil<IO> inputOutputType, final Nil<I2> input2Type, final Object... secondaryArgs) {
3331
return ops.findOp( //
3432
opClass, //
3533
new Nil<BiInplace1<IO, I2>>() {
3634
}, //
37-
new Type[] { inputOutputType, input2Type }, //
38-
inputOutputType, //
35+
new Nil[] { inputOutputType, input2Type }, //
36+
new Nil[] { inputOutputType }, //
3937
secondaryArgs);
4038
}
4139

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929

3030
package org.scijava.ops;
3131

32-
import java.lang.reflect.Type;
3332
import java.util.function.BiFunction;
3433
import java.util.function.Function;
3534

@@ -41,14 +40,20 @@
4140

4241
public class AdaptersTest extends AbstractTestEnvironment {
4342

43+
private static Nil<Double> nilDouble = new Nil<Double>() {
44+
};
45+
46+
private static Nil<double[]> nilDoubleArray = new Nil<double[]>() {
47+
};
48+
49+
4450
@Test
4551
public void testFunctionAsCommand() {
46-
Class<Double> c = Double.class;
4752
Function<Double, Double> sqrtFunction = ops().findOp( //
4853
MathSqrtOp.class, new Nil<Function<Double, Double>>() {
4954
}, //
50-
new Type[] { c }, //
51-
c//
55+
new Nil[] { nilDouble }, //
56+
nilDouble//
5257
);
5358

5459
OneToOneCommand<Double, Double> sqrtCommand = Adapt.Functions.asCommand(sqrtFunction, 25.0);
@@ -58,12 +63,11 @@ public void testFunctionAsCommand() {
5863

5964
@Test
6065
public void testComputerAsCommand() {
61-
Class<double[]> cArray = double[].class;
6266
Computer<double[], double[]> sqrtComputer = ops().findOp( //
6367
MathSqrtOp.class, new Nil<Computer<double[], double[]>>() {
6468
}, //
65-
new Type[] { cArray, cArray }, //
66-
cArray//
69+
new Nil[] { nilDoubleArray, nilDoubleArray }, //
70+
nilDoubleArray//
6771
);
6872

6973
OneToOneCommand<double[], double[]> sqrtCommand = Adapt.Computers.asCommand(sqrtComputer,
@@ -74,12 +78,11 @@ public void testComputerAsCommand() {
7478

7579
@Test
7680
public void testComputerAsFunction() {
77-
Class<double[]> cArray = double[].class;
7881
final BiComputer<double[], double[], double[]> computer = ops().findOp( //
7982
MathAddOp.class, new Nil<BiComputer<double[], double[], double[]>>() {
8083
}, //
81-
new Type[] { cArray, cArray, cArray }, //
82-
cArray//
84+
new Nil[] { nilDoubleArray, nilDoubleArray, nilDoubleArray }, //
85+
nilDoubleArray//
8386
);
8487

8588
BiFunction<double[], double[], double[]> computerAsFunction = Adapt.Computers.asBiFunction(computer,
@@ -95,13 +98,12 @@ public void testComputerAsFunction() {
9598

9699
@Test
97100
public void testFunctionAsComputer() {
98-
Class<double[]> c = double[].class;
99101
// look up a function: Double result = math.add(Double v1, Double v2)
100102
BiFunction<double[], double[], double[]> function = ops().findOp( //
101103
MathAddOp.class, new Nil<BiFunction<double[], double[], double[]>>() {
102104
}, //
103-
new Type[] { c, c }, //
104-
c//
105+
new Nil[] { nilDoubleArray, nilDoubleArray }, //
106+
nilDoubleArray//
105107
);
106108

107109
BiComputer<double[], double[], double[]> functionAsComputer = Adapt.Functions.asBiComputer(function,

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,17 @@
3333
import org.scijava.ops.math.Add.MathAddOp;
3434
import org.scijava.ops.math.Sqrt.MathSqrtOp;
3535
import org.scijava.ops.util.Computers;
36+
import org.scijava.ops.types.Nil;
3637

3738
public class ComputersTest extends AbstractTestEnvironment {
3839

40+
private static Nil<double[]> nilDoubleArray = new Nil<double[]>() {
41+
};
42+
3943
@Test
4044
public void testUnaryComputers() {
41-
Computer<double[], double[]> sqrtComputer = Computers.unary(ops(), MathSqrtOp.class, double[].class,
42-
double[].class);
45+
Computer<double[], double[]> sqrtComputer = Computers.unary(ops(), MathSqrtOp.class, nilDoubleArray,
46+
nilDoubleArray);
4347

4448
double[] result = new double[3];
4549
sqrtComputer.compute(new double[] { 4.0, 100.0, 25.0 }, result);
@@ -48,8 +52,8 @@ public void testUnaryComputers() {
4852

4953
@Test
5054
public void testBinaryComputers() {
51-
BiComputer<double[], double[], double[]> addComputer = Computers.binary(ops(), MathAddOp.class, double[].class,
52-
double[].class, double[].class);
55+
BiComputer<double[], double[], double[]> addComputer = Computers.binary(ops(), MathAddOp.class, nilDoubleArray,
56+
nilDoubleArray, nilDoubleArray);
5357

5458
double[] result = new double[3];
5559
addComputer.compute(new double[] { 4.0, 100.0, 25.0 }, new double[] { 4d, 10d, 1.5 }, result);

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,29 @@
3737
import org.scijava.ops.math.Power.MathPowerOp;
3838
import org.scijava.ops.math.Sqrt.MathSqrtOp;
3939
import org.scijava.ops.util.Functions;
40+
import org.scijava.ops.types.Nil;
4041

4142
public class FunctionsTest extends AbstractTestEnvironment {
4243

44+
private static Nil<Double> nilDouble = new Nil<Double>() {
45+
};
46+
4347
@Test
4448
public void testUnaryFunctions() {
45-
Function<Double, Double> sqrtFunction = Functions.unary(ops(), MathSqrtOp.class, Double.class, Double.class);
49+
Function<Double, Double> sqrtFunction = Functions.unary(ops(), MathSqrtOp.class, nilDouble, nilDouble);
4650
double answer = sqrtFunction.apply(16.0);
4751
assert 4.0 == answer;
4852
}
4953

5054
@Test
5155
public void testBinaryFunctions() {
52-
BiFunction<Double, Double, Double> addFunction = Functions.binary(ops(), MathAddOp.class, Double.class,
53-
Double.class, Double.class);
56+
BiFunction<Double, Double, Double> addFunction = Functions.binary(ops(), MathAddOp.class, nilDouble, nilDouble,
57+
nilDouble);
5458
double answer = addFunction.apply(16.0, 14.0);
5559
assert 30.0 == answer;
56-
57-
BiFunction<Double, Double, Double> powerFunction = Functions.binary(ops(), MathPowerOp.class, Double.class,
58-
Double.class, Double.class);
60+
61+
BiFunction<Double, Double, Double> powerFunction = Functions.binary(ops(), MathPowerOp.class, nilDouble,
62+
nilDouble, nilDouble);
5963
answer = powerFunction.apply(2.0, 10.0);
6064
assert 1024.0 == answer;
6165
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,25 @@
3333
import org.scijava.ops.math.Add.MathAddOp;
3434
import org.scijava.ops.math.Sqrt.MathSqrtOp;
3535
import org.scijava.ops.util.Inplaces;
36+
import org.scijava.ops.types.Nil;
3637

3738
public class InplacesTest extends AbstractTestEnvironment {
3839

40+
private static Nil<double[]> nilDoubleArray = new Nil<double[]>() {
41+
};
42+
3943
@Test
4044
public void testUnaryInplaces() {
41-
Inplace<double[]> inplaceSqrt = Inplaces.unary(ops(), MathSqrtOp.class, double[].class);
45+
Inplace<double[]> inplaceSqrt = Inplaces.unary(ops(), MathSqrtOp.class, nilDoubleArray);
4246
final double[] a1 = { 4, 100, 36 };
4347
inplaceSqrt.mutate(a1);
4448
assert arrayEquals(a1, 2.0, 10.0, 6.0);
4549
}
4650

4751
@Test
4852
public void testBinaryInplaces() {
49-
final BiInplace1<double[], double[]> inplaceAdd = Inplaces.binary1(ops(), MathAddOp.class, double[].class,
50-
double[].class);
53+
final BiInplace1<double[], double[]> inplaceAdd = Inplaces.binary1(ops(), MathAddOp.class, nilDoubleArray,
54+
nilDoubleArray);
5155
final double[] a1 = { 3, 5, 7 };
5256
final double[] a2 = { 2, 4, 9 };
5357
inplaceAdd.mutate(a1, a2);

0 commit comments

Comments
 (0)