1 | package com.hammurapi.extract; |
2 | |
3 | import java.lang.reflect.Array; |
4 | import java.util.Arrays; |
5 | import java.util.HashSet; |
6 | import java.util.Map; |
7 | import java.util.Set; |
8 | import java.util.TreeSet; |
9 | |
10 | /** |
11 | * Extractor which maps arguments to the target extractor. |
12 | * @author Pavel Vlasov |
13 | * |
14 | * @param <T> |
15 | * @param <V> |
16 | * @param <C> |
17 | */ |
18 | public class MappedExtractor<T, V, C> implements FacadeExtractor<T, V, C>, Mappable<T,V,C> { |
19 | |
20 | private Extractor<T, V, C> target; |
21 | private int[] map; |
22 | private TreeSet<Integer> parameterIndices; |
23 | |
24 | /** |
25 | * @param target Target extractor |
26 | * @param map Index map, e.g. if it is {5, 3} then the first argument |
27 | * of this extractor becomes the sixth argument of the target extractor |
28 | * and the second argument of this extractor becomes the 4th argument |
29 | * of the target extractor. |
30 | */ |
31 | protected MappedExtractor(Extractor<T, V, C> target, int[] map) { |
32 | super(); |
33 | this.target = target; |
34 | this.map = map; |
35 | |
36 | parameterIndices = new TreeSet<Integer>(); |
37 | |
38 | Set<Integer> tpIdx = target.parameterIndices(); |
39 | for (int i=0; i<map.length; ++i) { |
40 | if (tpIdx.contains(map[i])) { |
41 | parameterIndices.add(i); |
42 | } |
43 | } |
44 | } |
45 | |
46 | @Override |
47 | public V extract(C context, Map<C, Map<Extractor<T, ? super V, C>, ? super V>> cache, T... obj) { |
48 | int maxTargetIndex = -1; |
49 | for (Integer tIdx: target.parameterIndices()) { |
50 | if (tIdx>maxTargetIndex) { |
51 | maxTargetIndex=tIdx; |
52 | } |
53 | } |
54 | |
55 | @SuppressWarnings("unchecked") |
56 | T[] tObj = (T[]) Array.newInstance(obj.getClass().getComponentType(), maxTargetIndex+1); |
57 | for (int i=0; i<map.length; ++i) { |
58 | if (parameterIndices.contains(i)) { |
59 | tObj[map[i]] = obj[i]; |
60 | } |
61 | } |
62 | |
63 | return target.extract(context, cache, tObj); |
64 | } |
65 | |
66 | @Override |
67 | public Set<Integer> parameterIndices() { |
68 | return parameterIndices; |
69 | } |
70 | |
71 | @Override |
72 | public boolean isContextDependent() { |
73 | return target.isContextDependent(); |
74 | } |
75 | |
76 | @Override |
77 | public ComparisonResult compareTo(Extractor<T, V, C> other) { |
78 | ComparisonResult tcr = target.compareTo(other); |
79 | if (tcr==null) { |
80 | return null; |
81 | } |
82 | if (tcr.isOneToOneMapping()) { |
83 | // Inverse mapping. |
84 | int dim = -1; |
85 | for (int i=0; i<map.length; ++i) { |
86 | if (parameterIndices.contains(i) && map[i]>dim) { |
87 | dim = map[i]; |
88 | } |
89 | } |
90 | int[] rmap = new int[dim+1]; |
91 | Arrays.fill(rmap, -1); |
92 | for (int i=0; i<map.length; ++i) { |
93 | if (parameterIndices.contains(i)) { |
94 | rmap[map[i]] = i; |
95 | } |
96 | } |
97 | return new ComparisonResult(tcr.getType(), rmap); |
98 | } |
99 | |
100 | int[] tcrim = tcr.getIndexMap(); |
101 | int[] cmap = new int[map.length]; |
102 | Arrays.fill(cmap, -1); |
103 | for (int i=0; i<map.length; ++i) { |
104 | if (parameterIndices.contains(i)) { |
105 | if (tcrim.length>map[i]) { |
106 | cmap[i] = tcrim[map[i]]; |
107 | } else { |
108 | return null; |
109 | } |
110 | } |
111 | } |
112 | return new ComparisonResult(tcr.getType(), cmap); |
113 | } |
114 | |
115 | @Override |
116 | public String toString() { |
117 | return "MappedExtractor [target=" + target + ", map=" |
118 | + Arrays.toString(map) + "]"; |
119 | } |
120 | |
121 | @Override |
122 | public double getCost() { |
123 | return target.getCost(); |
124 | } |
125 | |
126 | |
127 | public static <T,V,C> Extractor<T,V,C> mapExtractor(Extractor<T,V,C> target, int[] map) { |
128 | if (ComparisonResult.isOneToOneMapping(map)) { |
129 | return target; |
130 | } |
131 | |
132 | // Target is not index-dependent (e.g. constant). |
133 | if (target.parameterIndices().isEmpty()) { |
134 | return target; |
135 | } |
136 | if (target instanceof Mappable) { |
137 | @SuppressWarnings("unchecked") |
138 | Extractor<T, V, C> ret = ((Mappable<T,V,C>) target).map(map); |
139 | if (ret!=null) { |
140 | return ret; |
141 | } |
142 | } |
143 | |
144 | return new MappedExtractor<T, V, C>(target, map); |
145 | } |
146 | |
147 | @SuppressWarnings("unchecked") |
148 | @Override |
149 | public Extractor<T, V, C> map(int[] map) { |
150 | if (ComparisonResult.isOneToOneMapping(map)) { |
151 | return this; |
152 | } |
153 | |
154 | int[] newMap = new int[map.length]; |
155 | Arrays.fill(newMap, -1); |
156 | |
157 | Set<Integer> pi = new HashSet<Integer>(parameterIndices); |
158 | |
159 | for (int i=0; i<newMap.length; ++i) { |
160 | if (map[i]!=-1) { |
161 | if (pi.remove(map[i])) { |
162 | newMap[i]=this.map[map[i]]; |
163 | } |
164 | } |
165 | } |
166 | |
167 | if (!pi.isEmpty()) { // Not all indexes were mapped. |
168 | return null; |
169 | } |
170 | |
171 | if (ComparisonResult.isOneToOneMapping(newMap)) { |
172 | return target; |
173 | } |
174 | |
175 | if (this instanceof MappedPredicate) { |
176 | return (Extractor<T, V, C>) new MappedPredicate<T, C>((Predicate<T, C>) target, newMap); |
177 | } |
178 | return new MappedExtractor<T, V, C>(target, newMap); |
179 | } |
180 | |
181 | public Extractor<T, V, C> getTarget() { |
182 | return target; |
183 | } |
184 | |
185 | public int[] getMap() { |
186 | return map; |
187 | } |
188 | } |