-
Notifications
You must be signed in to change notification settings - Fork 6
Use Anys, not wildcards, for converter output types #139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,7 +45,7 @@ | |
* @author Gabriel Selzer | ||
* @param <T> | ||
*/ | ||
public class IdentityCollection<T, U extends T> implements OpCollection { | ||
public class IdentityCollection<T> implements OpCollection { | ||
|
||
/** | ||
* @input t the object to be converted | ||
|
@@ -55,7 +55,7 @@ public class IdentityCollection<T, U extends T> implements OpCollection { | |
@OpHints(hints = { Conversion.FORBIDDEN, | ||
BaseOpHints.DependencyMatching.FORBIDDEN }) | ||
@OpField(names = "engine.convert, engine.identity", priority = Priority.FIRST) | ||
public final Function<U, T> identity = (t) -> t; | ||
public final Function<T, T> identity = (t) -> t; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The parens are unnecessary; you can just write |
||
|
||
/** | ||
* @mutable t the object to be "mutated" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ | |
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.lang.reflect.Type; | ||
import java.lang.reflect.TypeVariable; | ||
import java.util.function.BiFunction; | ||
import java.util.function.Function; | ||
|
||
|
@@ -40,6 +41,7 @@ | |
import org.junit.jupiter.api.Test; | ||
import org.scijava.function.Computers; | ||
import org.scijava.function.Container; | ||
import org.scijava.ops.api.OpInfo; | ||
import org.scijava.ops.api.Ops; | ||
import org.scijava.ops.engine.AbstractTestEnvironment; | ||
import org.scijava.ops.engine.conversionLoss.impl.IdentityLossReporter; | ||
|
@@ -169,4 +171,43 @@ public void testConvertAnys() { | |
Assertions.assertInstanceOf(Integer.class, out); | ||
} | ||
|
||
/** | ||
* An Op, written as a method, whose type variable has multiple bounds. | ||
* <p> | ||
* Note that, for the purposes of this test, the {@link Number} bound is | ||
* necessary even though it is not needed for the functionality of the test | ||
* Op. | ||
* </p> | ||
*/ | ||
@OpMethod(names = "test.boundsConversion", type = BiFunction.class) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Once again I got confused by the name of this op. It's weird to me that we have test ops named after the functionality being tested rather than named for what they do like normal ops. In this case, the op |
||
public static <T extends Number & Comparable<T>> T foo(T in1, T in2) { | ||
return in1.compareTo(in2) > 0 ? in1 : in2; | ||
} | ||
|
||
/** | ||
* Tests that conversion is possible when {@link TypeVariable}s in the | ||
* {@link OpInfo} are bounded by multiple types. | ||
*/ | ||
@Test | ||
public void testConvertMultipleBounds() { | ||
// Assert that there's only one possible match for our Op call | ||
var name = "test.boundsConversion"; | ||
var infos = ops.infos(name); | ||
Assertions.assertEquals(1, infos.size()); | ||
// And its input types are TypeVariables with two upper bounds. | ||
var inType = infos.first().inputTypes().get(0); | ||
Assertions.assertInstanceOf(TypeVariable.class, inType); | ||
var numBounds = ((TypeVariable<?>) inType).getBounds().length; | ||
Assertions.assertEquals(2, numBounds); | ||
// Now, call it such that we need conversion | ||
Integer i1 = 1; | ||
Double i2 = 2.0; | ||
var result = ops.op("test.boundsConversion") // | ||
.arity2().input(i1, i2).apply(); | ||
// Assert the result is an Integer | ||
Assertions.assertInstanceOf(Integer.class, result); | ||
Integer intResult = (Integer) result; | ||
Assertions.assertEquals(2, intResult); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is no longer correct.