|
| 1 | + |
| 2 | +package org.scijava.ops; |
| 3 | + |
| 4 | +import static org.junit.Assert.assertEquals; |
| 5 | +import static org.junit.Assert.assertSame; |
| 6 | + |
| 7 | +import java.lang.reflect.Type; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.List; |
| 10 | +import java.util.function.BiFunction; |
| 11 | +import java.util.function.Function; |
| 12 | +import java.util.stream.Stream; |
| 13 | + |
| 14 | +import net.imglib2.type.numeric.RealType; |
| 15 | +import org.junit.Test; |
| 16 | +import org.scijava.param.Parameter; |
| 17 | +import org.scijava.param.ValidityException; |
| 18 | +import org.scijava.struct.ItemIO; |
| 19 | +import org.scijava.struct.Member; |
| 20 | +import org.scijava.struct.Struct; |
| 21 | + |
| 22 | +/** |
| 23 | + * Playing with method parameters. |
| 24 | + * |
| 25 | + * @author Curtis Rueden |
| 26 | + */ |
| 27 | +public class MethodParameterTest<I extends RealType<I>, O extends RealType<O>, T extends RealType<T>> { |
| 28 | + |
| 29 | + // Calling a static method via reflection is slow. |
| 30 | + // Wrapping a static method as a lambda is potentially faster. |
| 31 | + // Static methods in imglib2-algorithm can be lambdaed using :: syntax. |
| 32 | + // May want to make TriFunction, Function4, Function5, etc. ifaces. |
| 33 | + // Can we write new ops as lambdas? |
| 34 | + |
| 35 | + //@SomeAnnotationHere? |
| 36 | + public BiFunction<T, T, T> adder = (x, y) -> { |
| 37 | + final T result = x.copy(); |
| 38 | + result.add(y); |
| 39 | + return result; |
| 40 | + }; |
| 41 | + |
| 42 | + // The above is reasonably concise, but does not implement e.g. MathAddOp. |
| 43 | + // What if we get rid of the need to type it that way? Why do we need it? |
| 44 | + // The string name of the op should be enough, no? |
| 45 | + // There is no benefit to the MathAddOp interface; we don't use it. |
| 46 | + // Will be simpler, and less convoluted and confusing, without it. |
| 47 | + |
| 48 | + // To get type safety back, we could do: |
| 49 | + // ops.op(MathOps.ADD, new Nil<...>() {}); |
| 50 | + |
| 51 | + // Another challenge is: how can/do we annotate the parameters? |
| 52 | + // It's fine if everything can be inferred with defaults, but in some |
| 53 | + // cases the programmer will want to define some of the attributes. |
| 54 | + |
| 55 | + //@SomeAnnotationHere? |
| 56 | + @Parameter(key = "y", description = "The Y thing. It has some properties.") |
| 57 | + @Parameter(key = "x") |
| 58 | + @Parameter(key = "result", type = ItemIO.BOTH) |
| 59 | + public BiComputerOp<T, T, T> adderComputer = (x, y, result) -> { |
| 60 | + result.set(x); |
| 61 | + result.add(y); |
| 62 | + }; |
| 63 | + |
| 64 | + // Above is a computer. Also concise. Does it make sense to reuse @Parameter? |
| 65 | + |
| 66 | + // Also: how do we discover this? The OpEnvironment will need its own |
| 67 | + // op cache. Its currency needs to be Structs. And we need new logic for |
| 68 | + // structifying the above field. When a new instance is requested, |
| 69 | + // it could call clone() on the existing instance (?), rather than |
| 70 | + // using the no-args constructor. |
| 71 | + |
| 72 | + // And what about static methods from e.g. imglib2-algorithm? |
| 73 | + // Here comes a method. Let's pretend it resides in a different class |
| 74 | + // somewhere we don't control, but we want it as an op: |
| 75 | + |
| 76 | + public static <V extends RealType<V>> V myAwesomeAdderFunction( |
| 77 | + final V x, |
| 78 | + final V y) |
| 79 | + { |
| 80 | + final V result = x.copy(); |
| 81 | + result.add(y); |
| 82 | + return result; |
| 83 | + } |
| 84 | + |
| 85 | + // The above method is not a functional interface, but can become one: |
| 86 | + |
| 87 | + //@SomeAnnotationHere? |
| 88 | + public BiFunction<T, T, T> adderFromMethod = |
| 89 | + MethodParameterTest::myAwesomeAdderFunction; |
| 90 | + |
| 91 | + // What about extra/secondary parameters? Can we do that, too? |
| 92 | + // Yes, but the syntax becomes much less concise: |
| 93 | + |
| 94 | + //@SomeAnnotationHere? |
| 95 | + public BiComputerOp<T, T, T> addAndScale = new BiComputerOp<T, T, T>() { |
| 96 | + |
| 97 | + @Parameter |
| 98 | + private T scale; |
| 99 | + |
| 100 | + @Override |
| 101 | + public void accept( |
| 102 | + final @Parameter(key = "x") T x, |
| 103 | + final @Parameter(key = "y") T y, |
| 104 | + final @Parameter(key = "result") T result) |
| 105 | + { |
| 106 | + result.set(x); |
| 107 | + result.add(y); |
| 108 | + result.mul(scale); |
| 109 | + } |
| 110 | + }; |
| 111 | + |
| 112 | + // At that point, it is just another class. May as well define it |
| 113 | + // explicitly as such using "public static class" instead of as a field. |
| 114 | + // Same number of lines of code! |
| 115 | + |
| 116 | + @Test |
| 117 | + public void testStuff() throws Exception { |
| 118 | + BiFunction<String, String, String> stringFunc = UsefulMethods::concat; |
| 119 | + System.out.println(stringFunc.apply("x", "y")); |
| 120 | + |
| 121 | + BiFunction<Integer, Integer, Integer> intFunc = UsefulMethods::concat; |
| 122 | + System.out.println(intFunc.getClass()); |
| 123 | + Stream.of(intFunc.getClass().getDeclaredMethods()).forEach(System.out::println); |
| 124 | + System.out.println(intFunc.getClass().getSuperclass()); |
| 125 | + Stream.of(intFunc.getClass().getInterfaces()).forEach(System.out::println); |
| 126 | + System.out.println(intFunc.apply(5, 6)); |
| 127 | + |
| 128 | + @Parameter |
| 129 | + final Function<String, String> doubler = v -> v + v; |
| 130 | + |
| 131 | + final Struct p = // |
| 132 | + MethodStructs.function2(UsefulMethods::concat); |
| 133 | + final List<Member<?>> items = p.members(); |
| 134 | + assertParam("a", int.class, ItemIO.INPUT, items.get(0)); |
| 135 | + assertParam("b", Double.class, ItemIO.INPUT, items.get(1)); |
| 136 | + assertParam("c", byte.class, ItemIO.INPUT, items.get(2)); |
| 137 | + assertParam("d", Object.class, ItemIO.INPUT, items.get(3)); |
| 138 | + assertParam("o", double.class, ItemIO.OUTPUT, items.get(4)); |
| 139 | + assertParam("p", String.class, ItemIO.OUTPUT, items.get(5)); |
| 140 | + } |
| 141 | + |
| 142 | + // -- Helper methods -- |
| 143 | + |
| 144 | + private void assertParam(final String key, final Type type, |
| 145 | + final ItemIO ioType, final Member<?> pMember) |
| 146 | + { |
| 147 | + assertEquals(key, pMember.getKey()); |
| 148 | + assertEquals(type, pMember.getType()); |
| 149 | + assertSame(ioType, pMember.getIOType()); |
| 150 | + } |
| 151 | + |
| 152 | + // -- Helper classes -- |
| 153 | + |
| 154 | + public static class UsefulMethods { |
| 155 | + |
| 156 | + public static String concat(final String s, final String t) { |
| 157 | + return "String:" + s + t; |
| 158 | + } |
| 159 | + |
| 160 | + public static Integer concat(final Integer s, final Integer t) { |
| 161 | + return s + t + 1; |
| 162 | + } |
| 163 | + |
| 164 | + public static int concat(final int s, final int t) { |
| 165 | + return s + t; |
| 166 | + } |
| 167 | + |
| 168 | + public static String concat(final int a, final Double b, final byte c, final Object d) { |
| 169 | + return "" + a + b + c + d; |
| 170 | + } |
| 171 | + } |
| 172 | +} |
0 commit comments