|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * ImageJ1 software for multidimensional image processing and analysis. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2013 - 2023 ImageJ2 developers. |
| 6 | + * %% |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions are met: |
| 9 | + * |
| 10 | + * 0. Redistributions of source code must retain the above copyright notice, |
| 11 | + * this list of conditions and the following disclaimer. |
| 12 | + * 1. Redistributions in binary form must reproduce the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer in the documentation |
| 14 | + * and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 20 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | + * POSSIBILITY OF SUCH DAMAGE. |
| 27 | + * #L% |
| 28 | + */ |
| 29 | + |
| 30 | +package net.imagej.ops2.tutorial; |
| 31 | + |
| 32 | +import java.util.ArrayList; |
| 33 | +import java.util.Arrays; |
| 34 | +import java.util.List; |
| 35 | +import java.util.SortedSet; |
| 36 | +import java.util.TreeSet; |
| 37 | +import java.util.function.Function; |
| 38 | + |
| 39 | +import org.scijava.ops.api.OpEnvironment; |
| 40 | +import org.scijava.ops.spi.OpCollection; |
| 41 | +import org.scijava.ops.spi.OpField; |
| 42 | +import org.scijava.priority.Priority; |
| 43 | + |
| 44 | +/** |
| 45 | + * A tutorial showing how you can use Op priorities to implement Ops that take |
| 46 | + * precedence over others in certain scenarios |
| 47 | + * |
| 48 | + * @author Gabriel Selzer |
| 49 | + */ |
| 50 | +public class OpPriorities implements OpCollection { |
| 51 | + |
| 52 | + @OpField(names = "tutorial.priority") |
| 53 | + public final Function<Iterable<Integer>, String> iterableFunc = // |
| 54 | + n -> { |
| 55 | + int max = Integer.MIN_VALUE; |
| 56 | + for (Integer object : n) { |
| 57 | + if (object > max) max = object; |
| 58 | + } |
| 59 | + return "This maximum (Iterable Op): " + max; |
| 60 | + }; |
| 61 | + |
| 62 | + @OpField(names = "tutorial.priority", priority = Priority.HIGH) |
| 63 | + public final Function<SortedSet<Integer>, String> listFunc = // |
| 64 | + n -> "This maximum (SortedSet Op): " + n.last(); |
| 65 | + |
| 66 | + /** |
| 67 | + * Many algorithms are able to achieve performance improvements on a |
| 68 | + * particular task by making some assumptions. For example, we might write a |
| 69 | + * baseline "maximum" Op that works on java.lang.Iterable, which covers a lot |
| 70 | + * of types, but we could do a lot better a TreeSet by using the |
| 71 | + * SortedSet.last() method. Thus, we can write two different Ops, and get |
| 72 | + * different outputs depending on whether the input is a SortedSet or |
| 73 | + * something else. |
| 74 | + */ |
| 75 | + public static void main(String... args) { |
| 76 | + OpEnvironment ops = OpEnvironment.getEnvironment(); |
| 77 | + // Say we have a Collection of numbers |
| 78 | + var ourNumbers = Arrays.asList(4, 8, 2, 3); |
| 79 | + |
| 80 | + // ArrayList is not a SortedSet, so it will use our Iterable Op |
| 81 | + List<Integer> list = new ArrayList<>(ourNumbers); |
| 82 | + String resultForAllIterables = ops.unary("tutorial.priority") // |
| 83 | + .input(list) // |
| 84 | + .outType(String.class) // |
| 85 | + .apply(); |
| 86 | + System.out.println(resultForAllIterables); |
| 87 | + |
| 88 | + // TreeSet is a SortedSet, so it could use both our Iterable Op and our |
| 89 | + // SortedSet Op. The SortedSet Op doesn't have to loop through the data, so |
| 90 | + // we want to use that one for performance. To do that, we set the priority |
| 91 | + // of that Op higher! |
| 92 | + TreeSet<Integer> set = new TreeSet<>(ourNumbers); |
| 93 | + String resultForATreeSet = ops.unary("tutorial.priority") // |
| 94 | + .input(set) // |
| 95 | + .outType(String.class) // |
| 96 | + .apply(); |
| 97 | + System.out.println(resultForATreeSet); |
| 98 | + } |
| 99 | +} |
0 commit comments