|
1 | 1 | package org.scijava.ops.util; |
2 | 2 |
|
3 | 3 | import java.lang.reflect.Type; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.Collections; |
| 6 | +import java.util.HashMap; |
| 7 | +import java.util.LinkedHashMap; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Map; |
| 10 | +import java.util.Map.Entry; |
| 11 | +import java.util.stream.Collectors; |
4 | 12 |
|
5 | 13 | import org.scijava.ops.OpService; |
6 | 14 | import org.scijava.ops.core.Op; |
|
25 | 33 | import org.scijava.ops.core.inplace.Inplace6Second; |
26 | 34 | import org.scijava.ops.core.inplace.Inplace6Sixth; |
27 | 35 | import org.scijava.ops.core.inplace.Inplace6Third; |
| 36 | +import org.scijava.ops.core.inplace.Inplace7Second; |
28 | 37 | import org.scijava.ops.types.Nil; |
| 38 | +import org.scijava.ops.util.Inplaces.InplaceInfo; |
29 | 39 | import org.scijava.util.Types; |
30 | 40 |
|
31 | | -/** |
32 | | - * Utility providing adaptation between {@link Op} types. |
33 | | - */ |
34 | 41 | public class Inplaces { |
35 | 42 |
|
| 43 | + /** |
| 44 | + * All known inplace types and their arities and mutable positions. The |
| 45 | + * entries are sorted by arity and mutable position. |
| 46 | + */ |
| 47 | + public static final Map<Class<?>, InplaceInfo> ALL_INPLACES; |
| 48 | + |
| 49 | + static { |
| 50 | + final Map<Class<?>, InplaceInfo> inplaces = new LinkedHashMap<>(22); |
| 51 | + inplaces.put(Inplace.class, new InplaceInfo(1, 0)); |
| 52 | + inplaces.put(BiInplaceFirst.class, new InplaceInfo(2, 0)); |
| 53 | + inplaces.put(BiInplaceSecond.class, new InplaceInfo(2, 1)); |
| 54 | + inplaces.put(Inplace3First.class, new InplaceInfo(3, 0)); |
| 55 | + inplaces.put(Inplace3Second.class, new InplaceInfo(3, 1)); |
| 56 | + inplaces.put(Inplace3Third.class, new InplaceInfo(3, 2)); |
| 57 | + inplaces.put(Inplace4First.class, new InplaceInfo(4, 0)); |
| 58 | + inplaces.put(Inplace4Second.class, new InplaceInfo(4, 1)); |
| 59 | + inplaces.put(Inplace4Third.class, new InplaceInfo(4, 2)); |
| 60 | + inplaces.put(Inplace4Fourth.class, new InplaceInfo(4, 3)); |
| 61 | + inplaces.put(Inplace5First.class, new InplaceInfo(5, 0)); |
| 62 | + inplaces.put(Inplace5Second.class, new InplaceInfo(5, 1)); |
| 63 | + inplaces.put(Inplace5Third.class, new InplaceInfo(5, 2)); |
| 64 | + inplaces.put(Inplace5Fourth.class, new InplaceInfo(5, 3)); |
| 65 | + inplaces.put(Inplace5Fifth.class, new InplaceInfo(5, 4)); |
| 66 | + inplaces.put(Inplace6First.class, new InplaceInfo(6, 0)); |
| 67 | + inplaces.put(Inplace6Second.class, new InplaceInfo(6, 1)); |
| 68 | + inplaces.put(Inplace6Third.class, new InplaceInfo(6, 2)); |
| 69 | + inplaces.put(Inplace6Fourth.class, new InplaceInfo(6, 3)); |
| 70 | + inplaces.put(Inplace6Fifth.class, new InplaceInfo(6, 4)); |
| 71 | + inplaces.put(Inplace6Sixth.class, new InplaceInfo(6, 5)); |
| 72 | + inplaces.put(Inplace7Second.class, new InplaceInfo(7, 1)); |
| 73 | + ALL_INPLACES = Collections.unmodifiableMap(inplaces); |
| 74 | + } |
| 75 | + |
36 | 76 | private Inplaces() { |
37 | 77 | // NB: Prevent instantiation of utility class. |
38 | 78 | } |
39 | 79 |
|
| 80 | + /** |
| 81 | + * @return {@code true} if the given type is a {@link #ALL_INPLACES known} |
| 82 | + * inplace type, {@code false} otherwise. <br> |
| 83 | + * Note that only the type itself and not its type hierarchy is |
| 84 | + * considered. |
| 85 | + * @throws NullPointerException If {@code type} is {@code null}. |
| 86 | + */ |
| 87 | + public static boolean isInplace(Type type) { |
| 88 | + return ALL_INPLACES.containsKey(Types.raw(type)); |
| 89 | + } |
| 90 | + |
| 91 | + public static List<Class<?>> getInplacesOfArity(final int arity) { |
| 92 | + return Inplaces.ALL_INPLACES.entrySet().stream() // |
| 93 | + .filter(e -> e.getValue().arity() == arity) // |
| 94 | + .map(Entry<Class<?>, InplaceInfo>::getKey) // |
| 95 | + .collect(Collectors.toList()); |
| 96 | + } |
| 97 | + |
40 | 98 | public static <IO> Inplace<IO> unary(final OpService ops, final String opName, final Nil<IO> inputOutputType, |
41 | 99 | final Object... secondaryArgs) { |
42 | 100 |
|
@@ -458,4 +516,23 @@ public Type getType() { |
458 | 516 | inputOutputType, // |
459 | 517 | secondaryArgs); |
460 | 518 | } |
| 519 | + |
| 520 | + public static class InplaceInfo { |
| 521 | + |
| 522 | + private final int arity; |
| 523 | + private final int mutablePosition; |
| 524 | + |
| 525 | + public InplaceInfo(final int arity, final int mutablePosition) { |
| 526 | + this.arity = arity; |
| 527 | + this.mutablePosition = mutablePosition; |
| 528 | + } |
| 529 | + |
| 530 | + public int arity() { |
| 531 | + return arity; |
| 532 | + } |
| 533 | + |
| 534 | + public int mutablePosition() { |
| 535 | + return mutablePosition; |
| 536 | + } |
| 537 | + } |
461 | 538 | } |
0 commit comments