|
31 | 31 |
|
32 | 32 | import java.lang.reflect.Method; |
33 | 33 | import java.lang.reflect.Parameter; |
34 | | -import java.util.HashMap; |
| 34 | +import java.util.ArrayList; |
35 | 35 | import java.util.List; |
36 | | -import java.util.Map; |
37 | | -import java.util.stream.Collectors; |
38 | 36 |
|
39 | | -import org.scijava.function.Producer; |
40 | | -import org.scijava.ops.api.Ops; |
41 | 37 | import org.scijava.ops.engine.exceptions.impl.NullablesOnMultipleMethodsException; |
42 | 38 | import org.scijava.ops.spi.OpDependency; |
43 | 39 | import org.scijava.struct.FunctionalMethodType; |
44 | | -import org.scijava.types.inference.FunctionalInterfaces; |
| 40 | +import org.scijava.struct.ItemIO; |
45 | 41 |
|
46 | 42 | /** |
47 | 43 | * Lazily generates the parameter data for a {@link List} of |
|
52 | 48 | * |
53 | 49 | * @author Gabriel Selzer |
54 | 50 | */ |
55 | | -public class LazilyGeneratedMethodParameterData implements ParameterData { |
56 | | - |
57 | | - private static final Map<Method, MethodParamInfo> paramDataMap = |
58 | | - new HashMap<>(); |
| 51 | +public class SynthesizedMethodParameterData implements ParameterData { |
59 | 52 |
|
60 | 53 | private final Method m; |
61 | 54 | private final Class<?> opType; |
62 | 55 |
|
63 | | - public LazilyGeneratedMethodParameterData(Method m, Class<?> opType) { |
| 56 | + public SynthesizedMethodParameterData(Method m, Class<?> opType) { |
64 | 57 | this.m = m; |
65 | 58 | this.opType = opType; |
66 | 59 | } |
67 | 60 |
|
68 | | - public static MethodParamInfo getInfo(List<FunctionalMethodType> fmts, |
69 | | - Method m, Class<?> opType) |
70 | | - { |
71 | | - if (!paramDataMap.containsKey(m)) generateMethodParamInfo(fmts, m, opType); |
72 | | - MethodParamInfo info = paramDataMap.get(m); |
73 | | - if (!info.containsAll(fmts)) updateMethodParamInfo(info, fmts, m, opType); |
74 | | - return info; |
75 | | - } |
76 | | - |
77 | | - private static void updateMethodParamInfo(MethodParamInfo info, |
78 | | - List<FunctionalMethodType> fmts, Method m, Class<?> opType) |
79 | | - { |
80 | | - Map<FunctionalMethodType, String> fmtNames = info.getFmtNames(); |
81 | | - Map<FunctionalMethodType, String> fmtDescriptions = info |
82 | | - .getFmtDescriptions(); |
83 | | - Map<FunctionalMethodType, Boolean> fmtNullability = info |
84 | | - .getFmtNullability(); |
85 | | - // determine the Op inputs/outputs |
86 | | - long numOpParams = m.getParameterCount(); |
87 | | - long numReturns = m.getReturnType() == void.class ? 0 : 1; |
88 | | - Boolean[] paramNullability = getParameterNullability(m, opType, |
89 | | - (int) numOpParams); |
90 | | - |
91 | | - addSynthesizedMethodParamInfo(fmtNames, fmtDescriptions, fmts, |
92 | | - fmtNullability, paramNullability); |
93 | | - } |
94 | | - |
95 | | - public static synchronized void generateMethodParamInfo( |
96 | | - List<FunctionalMethodType> fmts, Method m, Class<?> opType) |
97 | | - { |
98 | | - if (paramDataMap.containsKey(m)) return; |
99 | | - |
100 | | - // determine the Op inputs/outputs |
101 | | - long numOpParams = m.getParameterCount(); |
102 | | - long numReturns = m.getReturnType() == void.class ? 0 : 1; |
103 | | - |
104 | | - opType = FunctionalInterfaces.findFrom(opType); |
105 | | - Boolean[] paramNullability = getParameterNullability(m, opType, |
106 | | - (int) numOpParams); |
107 | | - |
108 | | - paramDataMap.put(m, synthesizedMethodParamInfo(fmts, paramNullability)); |
109 | | - } |
110 | | - |
111 | | - private static void addSynthesizedMethodParamInfo( |
112 | | - Map<FunctionalMethodType, String> fmtNames, |
113 | | - Map<FunctionalMethodType, String> fmtDescriptions, |
114 | | - List<FunctionalMethodType> fmts, |
115 | | - Map<FunctionalMethodType, Boolean> fmtNullability, |
116 | | - Boolean[] paramNullability) |
| 61 | + private List<String> getParameterNames( |
| 62 | + List<FunctionalMethodType> fmts) |
117 | 63 | { |
| 64 | + List<String> fmtNames = new ArrayList<>(fmts.size()); |
118 | 65 | int ins, outs, containers, mutables; |
119 | 66 | ins = outs = containers = mutables = 1; |
120 | | - int nullableIndex = 0; |
121 | 67 | for (FunctionalMethodType fmt : fmts) { |
122 | | - fmtDescriptions.put(fmt, ""); |
123 | 68 | switch (fmt.itemIO()) { |
124 | 69 | case INPUT: |
125 | | - fmtNames.put(fmt, "input" + ins++); |
126 | | - fmtNullability.put(fmt, paramNullability[nullableIndex++]); |
| 70 | + fmtNames.add("input" + ins++); |
127 | 71 | break; |
128 | 72 | case OUTPUT: |
129 | | - fmtNames.put(fmt, "output" + outs++); |
| 73 | + fmtNames.add("output" + outs++); |
130 | 74 | break; |
131 | 75 | case CONTAINER: |
132 | | - fmtNames.put(fmt, "container" + containers++); |
| 76 | + fmtNames.add("container" + containers++); |
133 | 77 | break; |
134 | 78 | case MUTABLE: |
135 | | - fmtNames.put(fmt, "mutable" + mutables++); |
| 79 | + fmtNames.add("mutable" + mutables++); |
136 | 80 | break; |
137 | 81 | default: |
138 | 82 | throw new RuntimeException("Unexpected ItemIO type encountered!"); |
139 | 83 | } |
140 | 84 | } |
141 | | - } |
142 | | - |
143 | | - private static MethodParamInfo synthesizedMethodParamInfo( |
144 | | - List<FunctionalMethodType> fmts, Boolean[] paramNullability) |
145 | | - { |
146 | | - Map<FunctionalMethodType, String> fmtNames = new HashMap<>(fmts.size()); |
147 | | - Map<FunctionalMethodType, String> fmtDescriptions = new HashMap<>(fmts |
148 | | - .size()); |
149 | | - Map<FunctionalMethodType, Boolean> fmtNullability = new HashMap<>(fmts |
150 | | - .size()); |
151 | | - |
152 | | - addSynthesizedMethodParamInfo(fmtNames, fmtDescriptions, fmts, |
153 | | - fmtNullability, paramNullability); |
154 | | - |
155 | | - return new MethodParamInfo(fmtNames, fmtDescriptions, fmtNullability); |
| 85 | + return fmtNames; |
156 | 86 | } |
157 | 87 |
|
158 | 88 | @Override |
159 | 89 | public List<SynthesizedParameterMember<?>> synthesizeMembers( |
160 | 90 | List<FunctionalMethodType> fmts) |
161 | 91 | { |
162 | | - Producer<MethodParamInfo> p = // |
163 | | - () -> LazilyGeneratedMethodParameterData.getInfo(fmts, m, opType); |
164 | | - |
165 | | - return fmts.stream() // |
166 | | - .map(fmt -> new SynthesizedParameterMember<>(fmt, p)) // |
167 | | - .collect(Collectors.toList()); |
| 92 | + Boolean[] optionality = getParameterNullability(m, opType, m.getParameterCount()); |
| 93 | + List<String> names = getParameterNames(fmts); |
| 94 | + int p = 0; |
| 95 | + List<SynthesizedParameterMember<?>> members = new ArrayList<>(fmts.size()); |
| 96 | + for (FunctionalMethodType fmt: fmts) { |
| 97 | + String name = names.get(p); |
| 98 | + boolean optional = fmt.itemIO() != ItemIO.OUTPUT && optionality[p++]; |
| 99 | + members.add(new SynthesizedParameterMember<>(fmt, name, !optional, "")); |
| 100 | + } |
| 101 | + return members; |
168 | 102 | } |
169 | 103 |
|
170 | 104 | private static Boolean[] getParameterNullability(Method m, Class<?> opType, |
|
0 commit comments