|
| 1 | +package org.scijava.ops.util; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | +import java.util.function.BiFunction; |
| 6 | +import java.util.function.Function; |
| 7 | + |
| 8 | +import org.junit.Test; |
| 9 | +import org.scijava.ops.AbstractTestEnvironment; |
| 10 | +import org.scijava.ops.OpField; |
| 11 | +import org.scijava.ops.core.Op; |
| 12 | +import org.scijava.ops.core.OpCollection; |
| 13 | +import org.scijava.ops.types.Nil; |
| 14 | +import org.scijava.param.Parameter; |
| 15 | +import org.scijava.plugin.Plugin; |
| 16 | +import org.scijava.struct.ItemIO; |
| 17 | + |
| 18 | +@Plugin(type = OpCollection.class) |
| 19 | +public class OpsAsParametersTest extends AbstractTestEnvironment { |
| 20 | + |
| 21 | + @OpField(names = "test.parameter.computer") |
| 22 | + @Parameter(key = "input") |
| 23 | + @Parameter(key = "output", type = ItemIO.OUTPUT) |
| 24 | + public final Function<Number, Double> func = (x) -> x.doubleValue(); |
| 25 | + |
| 26 | + @OpField(names = "test.parameter.op") |
| 27 | + @Parameter(key = "inputList") |
| 28 | + @Parameter(key = "op") |
| 29 | + @Parameter(key = "outputList", type = ItemIO.OUTPUT) |
| 30 | + public final BiFunction<List<Number>, Function<Number, Double>, List<Double>> biFunc = (x, op) -> { |
| 31 | + List<Double> output = new ArrayList<>(); |
| 32 | + for (Number n : x) |
| 33 | + output.add(op.apply(n)); |
| 34 | + return output; |
| 35 | + }; |
| 36 | + |
| 37 | + @Test |
| 38 | + public void TestOpWithOpField() { |
| 39 | + |
| 40 | + List<Number> list = new ArrayList<>(); |
| 41 | + list.add(40l); |
| 42 | + list.add(20.5); |
| 43 | + list.add(4.0d); |
| 44 | + |
| 45 | + List<Double> output = (List<Double>) ops.run("test.parameter.op", list, func); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void TestOpWithOpFieldWithoutRun() { |
| 50 | + |
| 51 | + List<Number> list = new ArrayList<>(); |
| 52 | + list.add(40l); |
| 53 | + list.add(20.5); |
| 54 | + list.add(4.0d); |
| 55 | + |
| 56 | + BiFunction<List<Number>, Function<Number, Double>, List<Double>> thing = Functions.binary(ops, |
| 57 | + "test.parameter.op", new Nil<List<Number>>() { |
| 58 | + }, new Nil<Function<Number, Double>>() { |
| 59 | + }, new Nil<List<Double>>() { |
| 60 | + }); |
| 61 | + |
| 62 | + |
| 63 | + List<Double> output = thing.apply(list, func); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void TestOpWithOpClass() { |
| 68 | + |
| 69 | + List<Number> list = new ArrayList<>(); |
| 70 | + list.add(40l); |
| 71 | + list.add(20.5); |
| 72 | + list.add(4.0d); |
| 73 | + |
| 74 | + Function<Number, Double> funcClass = Functions.unary(ops, "test.parameter.class", new Nil<Number>() { |
| 75 | + }, new Nil<Double>() { |
| 76 | + }); |
| 77 | + |
| 78 | + List<Double> output = (List<Double>) ops.run("test.parameter.op", list, funcClass); |
| 79 | + } |
| 80 | + |
| 81 | +} |
| 82 | + |
| 83 | +@Plugin(type = Op.class, name = "test.parameter.class") |
| 84 | +@Parameter(key = "input") |
| 85 | +@Parameter(key = "output", type = ItemIO.OUTPUT) |
| 86 | +class FuncClass implements Function<Number, Double> { |
| 87 | + |
| 88 | + @Override |
| 89 | + public Double apply(Number t) { |
| 90 | + return t.doubleValue() + 1; |
| 91 | + } |
| 92 | + |
| 93 | +} |
0 commit comments