Skip to content

Commit 1551934

Browse files
committed
OpEnvironment: rename opify to bakeLambdaType
Opify sounds like you are taking a Function and are making an Op; this is not the case (you are only receiving something that knows its type); bakeLambdaType is a better name for what this method is doing. This commit also adds a test for expected behavior of bakeLambdaType
1 parent 7704f17 commit 1551934

6 files changed

Lines changed: 54 additions & 12 deletions

File tree

‎imagej/imagej-ops2/src/test/java/net/imagej/ops2/coloc/pValue/DefaultPValueTest.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ private void assertColoc(double expectedPValue, double expectedColocValue, doubl
9898
});
9999

100100
BiFunction<RandomAccessibleInterval<FloatType>, RandomAccessibleInterval<FloatType>, Double> wrapped =
101-
ops.env().opify(op,
101+
ops.env().bakeLambdaType(op,
102102
new Nil<BiFunction<RandomAccessibleInterval<FloatType>, RandomAccessibleInterval<FloatType>, Double>>()
103103
{}.getType());
104104

‎imagej/imagej-ops2/src/test/java/net/imagej/ops2/image/equation/CoordinateEquationTest.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public void testEquation2DOp() {
7777
final Function<long[], Double> op = (coords) -> Math.pow(start[0] + coords[0] * spacing[0], 2)
7878
+ Math.pow(start[1] + coords[1] * spacing[1], 2);
7979

80-
final Function<long[], Double> wrapped = ops.env().opify(op,
80+
final Function<long[], Double> wrapped = ops.env().bakeLambdaType(op,
8181
new Nil<Function<long[], Double>>()
8282
{}.getType());
8383

@@ -113,7 +113,7 @@ public void testEquation4DOp() {
113113
return result;
114114
};
115115

116-
Function<long[], Double> wrapped = ops.env().opify(op,
116+
Function<long[], Double> wrapped = ops.env().bakeLambdaType(op,
117117
new Nil<Function<long[], Double>>()
118118
{}.getType());
119119

‎imagej/imagej-ops2/src/test/java/net/imagej/ops2/slice/SliceTest.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public void testXYCropping() {
9797
final int[] xyAxis = new int[] { 0, 1 };
9898

9999
Computers.Arity1<RandomAccessibleInterval<ByteType>, RandomAccessibleInterval<ByteType>> wrapped =
100-
ops.env().opify(test,
100+
ops.env().bakeLambdaType(test,
101101
new Nil<Computers.Arity1<RandomAccessibleInterval<ByteType>, RandomAccessibleInterval<ByteType>>>()
102102
{}.getType());
103103
ops.op("slice").input(in, wrapped, xyAxis, true).output(out).compute();
@@ -135,7 +135,7 @@ private void testXYZCropping(int t) {
135135
final int[] xyAxis = new int[] { 0, 1, 2 };
136136

137137
Computers.Arity1<RandomAccessibleInterval<ByteType>, RandomAccessibleInterval<ByteType>> wrapped =
138-
ops.env().opify(test,
138+
ops.env().bakeLambdaType(test,
139139
new Nil<Computers.Arity1<RandomAccessibleInterval<ByteType>, RandomAccessibleInterval<ByteType>>>()
140140
{}.getType());
141141
ops.op("slice").input(inSequence, wrapped, xyAxis, true).output(outSequence)

‎scijava/scijava-ops/src/main/java/org/scijava/ops/OpEnvironment.java‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.lang.reflect.Type;
3333

3434
import org.scijava.ops.core.builder.OpBuilder;
35+
import org.scijava.ops.matcher.OpMatcher;
3536
import org.scijava.ops.matcher.OpRef;
3637
import org.scijava.types.Nil;
3738

@@ -89,16 +90,19 @@ default OpBuilder op(final String opName) {
8990
*
9091
* <pre>
9192
* <code>
92-
* op("adapt").input(computer).outType(new Nil&lt;Computers.Arity1&lt;Iterable&lt;Double&gt;, Iterable&lt;Double&gt;&gt;&gt;() {}).apply()
93+
* op("adapt")
94+
* .input(computer)
95+
* .outType(new Nil&lt;Computers.Arity1&lt;Iterable&lt;Double&gt;, Iterable&lt;Double&gt;&gt;&gt;() {})
96+
* .apply()
9397
* </code>
9498
* </pre>
9599
*
96100
* since the type parameters of {@code computer} are not retained at runtime.
97101
* <p>
98-
* {@code opify} should be used as a method of retaining that fully reified
99-
* lambda type so that it can be used by the {@link OpMatcher}.
102+
* {@code bakeLambdaType} should be used as a method of retaining that fully
103+
* reified lambda type so that the lambda can be used
100104
* <p>
101-
* Note: {@code opify} <b>does not</b> need to be used with anonymous
105+
* Note: {@code bakeLambdaType} <b>does not</b> need to be used with anonymous
102106
* subclasses; these retain their type parameters at runtime. It is only
103107
* lambda expressions that need to be passed to this method.
104108
*
@@ -108,5 +112,5 @@ default OpBuilder op(final String opName) {
108112
* @return An enriched version of the object with full knowledge of its
109113
* generic type.
110114
*/
111-
<T> T opify(T op, Type type);
115+
<T> T bakeLambdaType(T op, Type type);
112116
}

‎scijava/scijava-ops/src/main/java/org/scijava/ops/impl/DefaultOpEnvironment.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public Type genericType(Object obj) {
154154
}
155155

156156
@Override
157-
public <T> T opify(final T op, Type reifiedType) {
157+
public <T> T bakeLambdaType(final T op, Type reifiedType) {
158158
if (wrappers == null) initWrappers();
159159
@SuppressWarnings("unchecked")
160160
final OpWrapper<T> wrapper = (OpWrapper<T>) wrappers.get(Types.raw(reifiedType));
@@ -275,7 +275,7 @@ private <T> T wrapOp(T op, OpInfo opInfo, Map<TypeVariable<?>, Type> typeVarAssi
275275
Type exactSuperType = Types.getExactSuperType(opType, wrapper);
276276
Type reifiedSuperType = Types.substituteTypeVariables(exactSuperType, typeVarAssigns);
277277
// wrap the Op
278-
return opify(op, reifiedSuperType);
278+
return bakeLambdaType(op, reifiedSuperType);
279279
} catch (IllegalArgumentException | SecurityException exc) {
280280
log.error(exc.getMessage() != null ? exc.getMessage() : "Cannot wrap " + op.getClass());
281281
return op;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.scijava.ops;
2+
3+
import java.lang.reflect.Type;
4+
import java.util.function.Function;
5+
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
import org.scijava.types.GenericTyped;
9+
import org.scijava.types.Nil;
10+
11+
/**
12+
* Test class for {@link OpEnvironment} methods. NB this class does not test any
13+
* <em>particular</em> implementation of {@link OpEnvironment}, but instead
14+
* ensures expected behavior in the {@link OpEnvironment} returned by the
15+
* {@link OpService} (which will be nearly exclusively the only OpEnvironment
16+
* implementation used)
17+
*
18+
* @author Gabriel Selzer
19+
*/
20+
public class OpEnvironmentTest extends AbstractTestEnvironment{
21+
22+
@Test
23+
public void testBakeType() {
24+
Function<Double, Double> func = (in) -> in * 2;
25+
Type funcType = new Nil<Function<Double, Double>>() {}.getType();
26+
27+
Function<Double, Double> wrappedFunction = ops.env().bakeLambdaType(func,
28+
funcType);
29+
30+
Assert.assertTrue("wrappedFunction should be a GenericTyped but is not!",
31+
wrappedFunction instanceof GenericTyped);
32+
Type type = ((GenericTyped) wrappedFunction).getType();
33+
Assert.assertEquals("wrappedFunction type " + type +
34+
"is not equivalent to the provided type " + funcType + "!", funcType,
35+
type);
36+
}
37+
38+
}

0 commit comments

Comments
 (0)