Skip to content

Commit b933f00

Browse files
gselzerctrueden
authored andcommitted
Add Op creation helpers
1 parent c211d1a commit b933f00

3 files changed

Lines changed: 511 additions & 5 deletions

File tree

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

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
import org.scijava.ops.core.Op;
77
import org.scijava.ops.core.computer.BiComputer;
88
import org.scijava.ops.core.computer.Computer;
9+
import org.scijava.ops.core.computer.Computer3;
10+
import org.scijava.ops.core.computer.Computer4;
11+
import org.scijava.ops.core.computer.Computer5;
12+
import org.scijava.ops.core.computer.NullaryComputer;
913
import org.scijava.ops.types.Nil;
1014
import org.scijava.util.Types;
1115

@@ -18,8 +22,25 @@ private Computers() {
1822
// NB: Prevent instantiation of utility class.
1923
}
2024

21-
public static <I, O> Computer<I, O> unary(final OpService ops, final String opName,
22-
final Nil<I> inputType, final Nil<O> outputType, final Object... secondaryArgs) {
25+
public static <O> NullaryComputer<O> nullary(final OpService ops, final String opName, final Nil<O> outputType,
26+
final Object... secondaryArgs) {
27+
Nil<NullaryComputer<O>> computerNil = new Nil<NullaryComputer<O>>() {
28+
@Override
29+
public Type getType() {
30+
return Types.parameterize(NullaryComputer.class, new Type[] { outputType.getType() });
31+
}
32+
};
33+
34+
return ops.findOp( //
35+
opName, //
36+
computerNil, //
37+
new Nil[] { outputType }, //
38+
new Nil[] { outputType }, //
39+
secondaryArgs);
40+
}
41+
42+
public static <I, O> Computer<I, O> unary(final OpService ops, final String opName, final Nil<I> inputType,
43+
final Nil<O> outputType, final Object... secondaryArgs) {
2344

2445
Nil<Computer<I, O>> computerNil = new Nil<Computer<I, O>>() {
2546
@Override
@@ -56,4 +77,65 @@ public Type getType() {
5677
secondaryArgs);
5778
}
5879

80+
public static <I1, I2, I3, O> Computer3<I1, I2, I3, O> ternary(final OpService ops, final String opName,
81+
final Nil<I1> input1Type, final Nil<I2> input2Type, final Nil<I3> input3Type, final Nil<O> outputType,
82+
final Object... secondaryArgs) {
83+
84+
Nil<Computer3<I1, I2, I3, O>> computerNil = new Nil<Computer3<I1, I2, I3, O>>() {
85+
@Override
86+
public Type getType() {
87+
return Types.parameterize(Computer3.class, new Type[] { input1Type.getType(), input2Type.getType(),
88+
input3Type.getType(), outputType.getType() });
89+
}
90+
};
91+
92+
return ops.findOp( //
93+
opName, //
94+
computerNil, //
95+
new Nil[] { input1Type, input2Type, input3Type, outputType }, //
96+
new Nil[] { outputType }, //
97+
secondaryArgs);
98+
}
99+
100+
public static <I1, I2, I3, I4, O> Computer4<I1, I2, I3, I4, O> quaternary(final OpService ops, final String opName,
101+
final Nil<I1> input1Type, final Nil<I2> input2Type, final Nil<I3> input3Type, final Nil<I4> input4Type,
102+
final Nil<O> outputType, final Object... secondaryArgs) {
103+
104+
Nil<Computer4<I1, I2, I3, I4, O>> computerNil = new Nil<Computer4<I1, I2, I3, I4, O>>() {
105+
@Override
106+
public Type getType() {
107+
return Types.parameterize(Computer4.class, new Type[] { input1Type.getType(), input2Type.getType(),
108+
input3Type.getType(), input4Type.getType(), outputType.getType() });
109+
}
110+
};
111+
112+
return ops.findOp( //
113+
opName, //
114+
computerNil, //
115+
new Nil[] { input1Type, input2Type, input3Type, input4Type, outputType }, //
116+
new Nil[] { outputType }, //
117+
secondaryArgs);
118+
}
119+
120+
public static <I1, I2, I3, I4, I5, O> Computer5<I1, I2, I3, I4, I5, O> quinary(final OpService ops,
121+
final String opName, final Nil<I1> input1Type, final Nil<I2> input2Type, final Nil<I3> input3Type,
122+
final Nil<I4> input4Type, final Nil<I5> input5Type, final Nil<O> outputType,
123+
final Object... secondaryArgs) {
124+
125+
Nil<Computer5<I1, I2, I3, I4, I5, O>> computerNil = new Nil<Computer5<I1, I2, I3, I4, I5, O>>() {
126+
@Override
127+
public Type getType() {
128+
return Types.parameterize(Computer5.class, new Type[] { input1Type.getType(), input2Type.getType(),
129+
input3Type.getType(), input4Type.getType(), input5Type.getType(), outputType.getType() });
130+
}
131+
};
132+
133+
return ops.findOp( //
134+
opName, //
135+
computerNil, //
136+
new Nil[] { input1Type, input2Type, input3Type, input4Type, input5Type, outputType }, //
137+
new Nil[] { outputType }, //
138+
secondaryArgs);
139+
}
140+
59141
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.scijava.ops.core.Op;
99
import org.scijava.ops.core.function.Function3;
1010
import org.scijava.ops.core.function.Function4;
11+
import org.scijava.ops.core.function.Function5;
1112
import org.scijava.ops.types.Nil;
1213
import org.scijava.util.Types;
1314

@@ -97,4 +98,24 @@ public Type getType() {
9798
new Nil[] { outputType }, //
9899
secondaryArgs);
99100
}
101+
102+
public static <I1, I2, I3, I4, I5, O> Function5<I1, I2, I3, I4, I5, O> quinary(final OpService ops,
103+
final String opName, final Nil<I1> input1Type, final Nil<I2> input2Type, final Nil<I3> input3Type,
104+
final Nil<I4> input4Type, final Nil<I5> input5Type, final Nil<O> outputType, final Object... secondaryArgs) {
105+
106+
Nil<Function5<I1, I2, I3, I4, I5, O>> functionNil = new Nil<Function5<I1, I2, I3, I4, I5, O>>() {
107+
@Override
108+
public Type getType() {
109+
return Types.parameterize(Function5.class, new Type[] { input1Type.getType(), input2Type.getType(),
110+
input3Type.getType(), input4Type.getType(), input5Type.getType(), outputType.getType() });
111+
}
112+
};
113+
114+
return ops.findOp( //
115+
opName, //
116+
functionNil, //
117+
new Nil[] { input1Type, input2Type, input3Type, input4Type, input5Type }, //
118+
new Nil[] { outputType }, //
119+
secondaryArgs);
120+
}
100121
}

0 commit comments

Comments
 (0)