Skip to content

Commit 975501a

Browse files
committed
Simpify match with matchHelper method
1 parent 91f68d2 commit 975501a

8 files changed

Lines changed: 480 additions & 1742 deletions

File tree

src/main/java/org/scijava/ops/function/Computers.java

Lines changed: 95 additions & 321 deletions
Large diffs are not rendered by default.

src/main/java/org/scijava/ops/function/Functions.java

Lines changed: 60 additions & 153 deletions
Large diffs are not rendered by default.

src/main/java/org/scijava/ops/function/Inplaces.java

Lines changed: 279 additions & 1224 deletions
Large diffs are not rendered by default.

templates/main/java/org/scijava/ops/function/Computers.list

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ generics = ```
99
```
1010
nilArgs = ```
1111
{ arity ->
12-
String.join(', ', nilNames(arity))
12+
names = nilNames(arity)
13+
names.swap(0, names.size() - 1)
14+
String.join(', ', names)
1315
}
1416
```
1517
matchParams = ```

templates/main/java/org/scijava/ops/function/Computers.vm

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -69,30 +69,25 @@ public final class Computers {
6969
public static boolean isComputer(Type type) {
7070
return ALL_COMPUTERS.containsKey(Types.raw(type));
7171
}
72-
#foreach($arity in $arities)
73-
#set($rawClass = "Arity$arity")
74-
#set($genericParams = $generics.call($arity))
75-
#set($gClass = "$rawClass$genericParams")
72+
73+
#foreach($arity in [0..$maxArity])
74+
@SuppressWarnings("unchecked")
75+
public static $generics.call($arity) $computerArity.call($arity)$generics.call($arity) match(final OpService ops, final String opName, $matchParams.call($arity)) {
76+
return matchHelper(ops, opName, ${computerArity.call($arity)}.class, $nilArgs.call($arity));
77+
}
7678

77-
public static $genericParams $gClass match(final OpService ops, final String opName,
78-
$matchParams.call($arity))
79-
{
80-
final Nil<$gClass> specialType =
81-
new Nil<$gClass>()
82-
{
83-
@Override
84-
public Type getType() {
85-
return Types.parameterize(${rawClass}.class, //
86-
new Type[] { $typeArgs.call($arity) });
87-
}
88-
};
89-
return ops.findOp( //
90-
opName, //
91-
specialType, //
92-
new Nil<?>[] { $nilArgs.call($arity) }, //
93-
outType);
79+
#end
80+
@SuppressWarnings({ "unchecked" })
81+
private static <T> T matchHelper(final OpService ops, final String opName, final Class<T> opClass, final Nil<?> outType, final Nil<?>... inTypes) {
82+
final Type[] types = new Type[inTypes.length + 1];
83+
for (int i=0; i<inTypes.length; i++) types[i] = inTypes[i].getType();
84+
types[types.length - 1] = outType.getType();
85+
final Type specialType = Types.parameterize(opClass, types);
86+
final Nil<?>[] nils = new Nil[inTypes.length + 1];
87+
System.arraycopy(inTypes, 0, nils, 0, inTypes.length);
88+
nils[nils.length - 1] = outType;
89+
return (T) ops.findOp(opName, Nil.of(specialType), nils, outType);
9490
}
95-
#end##foreach($arity)
9691

9792
// -- END TEMP --
9893
#foreach($arity in $arities)

templates/main/java/org/scijava/ops/function/Functions.list

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ typeArgs = ```
4141
nilArgs = ```
4242
{ arity ->
4343
names = genericsNamesList(arity)
44-
String.join(', ', names.subList(0, names.size() - 1).stream().map{a -> "${a}Type"}.collect())
44+
out = names.remove(names.size() - 1)
45+
names.add(0, out)
46+
String.join(', ', names.stream().map{a -> "${a}Type"}.collect())
4547
}
4648
```
4749
applyParams = ```

templates/main/java/org/scijava/ops/function/Functions.vm

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,21 +68,24 @@ public final class Functions {
6868
public static boolean isFunction(Type type) {
6969
return ALL_FUNCTIONS.containsKey(Types.raw(type));
7070
}
71-
71+
7272
#foreach($arity in [0..$maxArity])
73-
@SuppressWarnings({ "unchecked", "rawtypes" })
73+
@SuppressWarnings({ "unchecked" })
7474
public static $generics.call($arity) $arityClassName.call($arity)$generics.call($arity) match(final OpService ops, final String opName, $matchParams.call($arity)) {
75-
final Type specialType = Types.parameterize($arityClass.call($arity), //
76-
new Type[] { $typeArgs.call($arity) });
77-
return ($arityClassName.call($arity)) ops.findOp( //
78-
opName, //
79-
Nil.of(specialType), //
80-
new Nil[] { $nilArgs.call($arity) }, //
81-
outType);
75+
return matchHelper(ops, opName, ${arityClassName.call($arity)}.class, $nilArgs.call($arity));
8276
}
83-
77+
8478
#end
8579

80+
@SuppressWarnings({ "unchecked" })
81+
private static <T> T matchHelper(final OpService ops, final String opName, final Class<T> opClass, final Nil<?> outType, final Nil<?>... inTypes) {
82+
final Type[] types = new Type[inTypes.length + 1];
83+
for (int i=0; i<inTypes.length; i++) types[i] = inTypes[i].getType();
84+
types[types.length - 1] = outType.getType();
85+
final Type specialType = Types.parameterize(opClass, types);
86+
return (T) ops.findOp(opName, Nil.of(specialType), inTypes, outType);
87+
}
88+
8689

8790
#foreach($arity in $arities)
8891
/**

templates/main/java/org/scijava/ops/function/Inplaces.vm

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,23 +92,23 @@ public final class Inplaces {
9292
.map(Entry<Class<?>, InplaceInfo>::getKey) //
9393
.collect(Collectors.toList());
9494
}
95-
9695
#foreach($arity in $arities)
9796
#foreach($a in [1..$arity])
98-
@SuppressWarnings({ "unchecked", "rawtypes" })
97+
@SuppressWarnings({ "unchecked" })
9998
public static $generics.call($arity, $a) $simplifiedInplace.call($arity, $a)$generics.call($arity, $a) $matchName.call($arity, $a)(final OpService ops, final String opName, $matchParams.call($arity, $a)) {
100-
101-
Type inplaceType = Types.parameterize($simplifiedClass.call($arity, $a), new Type[] { $typeArgs.call($arity, $a)});
102-
103-
return (${simplifiedInplace.call($arity, $a)})ops.findOp( //
104-
opName, //
105-
Nil.of(inplaceType), //
106-
new Nil[] { $basicParams.call($arity, $a) }, //
107-
ioType);
99+
return matchHelper(ops, opName, ${simplifiedInplace.call($arity, $a)}.class, ioType, new Nil[] {${basicParams.call($arity, $a)}});
108100
}
109101
#end
110102
#end
111103

104+
@SuppressWarnings({ "unchecked" })
105+
private static <T> T matchHelper(final OpService ops, final String opName, final Class<T> opClass, final Nil<?> outType, final Nil<?>... inTypes) {
106+
final Type[] types = new Type[inTypes.length];
107+
for (int i=0; i<inTypes.length; i++) types[i] = inTypes[i].getType();
108+
final Type specialType = Types.parameterize(opClass, types);
109+
return (T) ops.findOp(opName, Nil.of(specialType), inTypes, outType);
110+
}
111+
112112
public static class InplaceInfo {
113113

114114
private final int arity;

0 commit comments

Comments
 (0)