|
32 | 32 | import java.lang.reflect.Field; |
33 | 33 | import java.lang.reflect.Method; |
34 | 34 | import java.util.ArrayList; |
35 | | -import java.util.HashMap; |
36 | 35 | import java.util.List; |
37 | | -import java.util.Map; |
38 | | -import java.util.stream.Collectors; |
39 | 36 |
|
40 | | -import org.scijava.function.Producer; |
41 | | -import org.scijava.ops.api.Ops; |
42 | 37 | import org.scijava.ops.engine.exceptions.impl.NullablesOnMultipleMethodsException; |
43 | 38 | import org.scijava.struct.FunctionalMethodType; |
| 39 | +import org.scijava.struct.ItemIO; |
44 | 40 | import org.scijava.types.inference.FunctionalInterfaces; |
45 | 41 |
|
46 | 42 | /** |
|
52 | 48 | * |
53 | 49 | * @author Gabriel Selzer |
54 | 50 | */ |
55 | | -public class LazilyGeneratedFieldParameterData implements ParameterData { |
56 | | - |
57 | | - private static final Map<FieldInstance, MethodParamInfo> paramDataMap = |
58 | | - new HashMap<>(); |
| 51 | +public class SynthesizedFieldParameterData implements ParameterData { |
59 | 52 |
|
60 | 53 | private final FieldInstance fieldInstance; |
| 54 | + private final int numInputs; |
61 | 55 |
|
62 | | - public LazilyGeneratedFieldParameterData(FieldInstance fieldInstance) { |
| 56 | + public SynthesizedFieldParameterData(FieldInstance fieldInstance) { |
63 | 57 | this.fieldInstance = fieldInstance; |
| 58 | + this.numInputs = FunctionalInterfaces // |
| 59 | + .functionalMethodOf(fieldInstance.field().getType()) |
| 60 | + .getParameterCount(); |
64 | 61 | } |
65 | 62 |
|
66 | | - public static MethodParamInfo getInfo(List<FunctionalMethodType> fmts, |
67 | | - FieldInstance fieldInstance) |
68 | | - { |
69 | | - if (!paramDataMap.containsKey(fieldInstance)) generateFieldParamInfo(fmts, |
70 | | - fieldInstance); |
71 | | - return paramDataMap.get(fieldInstance); |
72 | | - } |
73 | | - |
74 | | - public static synchronized void generateFieldParamInfo( |
75 | | - List<FunctionalMethodType> fmts, FieldInstance fieldInstance) |
| 63 | + @Override |
| 64 | + public List<SynthesizedParameterMember<?>> synthesizeMembers( |
| 65 | + List<FunctionalMethodType> fmts) |
76 | 66 | { |
77 | | - if (paramDataMap.containsKey(fieldInstance)) return; |
78 | | - |
79 | | - Method sam = FunctionalInterfaces.functionalMethodOf(fieldInstance.field() |
80 | | - .getType()); |
81 | | - // There is always one output, but we need the number of inputs |
82 | | - long numIns = sam.getParameterCount(); |
83 | | - |
84 | | - // determine the Op inputs/outputs |
85 | | - Boolean[] paramNullability = getParameterNullability(fieldInstance |
86 | | - .instance(), fieldInstance.field(), (int) numIns); |
87 | 67 |
|
88 | | - paramDataMap.put(fieldInstance, synthesizedMethodParamInfo(fmts, |
89 | | - paramNullability)); |
| 68 | + // determine the optionality of each input |
| 69 | + Boolean[] optionality = getParameterNullability(fieldInstance |
| 70 | + .instance(), fieldInstance.field(), numInputs); |
| 71 | + // determine the name of each fmt |
| 72 | + List<String> names = getParameterNames(fmts); |
| 73 | + // map fmts to members |
| 74 | + int p = 0; |
| 75 | + List<SynthesizedParameterMember<?>> members = new ArrayList<>(fmts.size()); |
| 76 | + for (FunctionalMethodType fmt: fmts) { |
| 77 | + String name = names.get(p); |
| 78 | + boolean optional = fmt.itemIO() != ItemIO.OUTPUT && optionality[p++]; |
| 79 | + members.add(new SynthesizedParameterMember<>(fmt, name, !optional, "")); |
| 80 | + } |
| 81 | + return members; |
90 | 82 | } |
91 | 83 |
|
92 | | - private static MethodParamInfo synthesizedMethodParamInfo( |
93 | | - List<FunctionalMethodType> fmts, Boolean[] paramNullability) |
| 84 | + private List<String> getParameterNames( |
| 85 | + List<FunctionalMethodType> fmts) |
94 | 86 | { |
95 | | - Map<FunctionalMethodType, String> fmtNames = new HashMap<>(fmts.size()); |
96 | | - Map<FunctionalMethodType, String> fmtDescriptions = new HashMap<>(fmts |
97 | | - .size()); |
98 | | - Map<FunctionalMethodType, Boolean> fmtNullability = new HashMap<>(fmts |
99 | | - .size()); |
100 | | - |
| 87 | + List<String> fmtNames = new ArrayList<>(fmts.size()); |
101 | 88 | int ins, outs, containers, mutables; |
102 | 89 | ins = outs = containers = mutables = 1; |
103 | | - int nullableIndex = 0; |
104 | 90 | for (FunctionalMethodType fmt : fmts) { |
105 | | - fmtDescriptions.put(fmt, ""); |
106 | 91 | switch (fmt.itemIO()) { |
107 | 92 | case INPUT: |
108 | | - fmtNames.put(fmt, "input" + ins++); |
109 | | - fmtNullability.put(fmt, paramNullability[nullableIndex++]); |
| 93 | + fmtNames.add("input" + ins++); |
110 | 94 | break; |
111 | 95 | case OUTPUT: |
112 | | - fmtNames.put(fmt, "output" + outs++); |
| 96 | + fmtNames.add("output" + outs++); |
113 | 97 | break; |
114 | 98 | case CONTAINER: |
115 | | - fmtNames.put(fmt, "container" + containers++); |
| 99 | + fmtNames.add("container" + containers++); |
116 | 100 | break; |
117 | 101 | case MUTABLE: |
118 | | - fmtNames.put(fmt, "mutable" + mutables++); |
| 102 | + fmtNames.add("mutable" + mutables++); |
119 | 103 | break; |
120 | 104 | default: |
121 | 105 | throw new RuntimeException("Unexpected ItemIO type encountered!"); |
122 | 106 | } |
123 | 107 | } |
124 | | - return new MethodParamInfo(fmtNames, fmtDescriptions, fmtNullability); |
125 | | - } |
126 | | - |
127 | | - @Override |
128 | | - public List<SynthesizedParameterMember<?>> synthesizeMembers( |
129 | | - List<FunctionalMethodType> fmts) |
130 | | - { |
131 | | - Producer<MethodParamInfo> p = // |
132 | | - () -> LazilyGeneratedFieldParameterData.getInfo(fmts, fieldInstance); |
133 | | - |
134 | | - return fmts.stream() // |
135 | | - .map(fmt -> new SynthesizedParameterMember<>(fmt, p)) // |
136 | | - .collect(Collectors.toList()); |
| 108 | + return fmtNames; |
137 | 109 | } |
138 | 110 |
|
139 | | - // Helper methods |
140 | 111 | private static Boolean[] getParameterNullability(Object instance, Field field, |
141 | 112 | int opParams) |
142 | 113 | { |
|
0 commit comments