1 | package com.hammurapi.extract; |
2 | |
3 | import java.util.Arrays; |
4 | import java.util.HashMap; |
5 | import java.util.HashSet; |
6 | import java.util.Iterator; |
7 | import java.util.List; |
8 | import java.util.Map; |
9 | import java.util.Set; |
10 | import java.util.TreeSet; |
11 | |
12 | import com.hammurapi.extract.ComparisonResult.Type; |
13 | |
14 | /** |
15 | * Base class for compiled java extractors/predicates. |
16 | * @author Pavel Vlasov |
17 | * |
18 | * @param <T> |
19 | * @param <V> |
20 | * @param <C> |
21 | */ |
22 | @SuppressWarnings("unchecked") |
23 | public abstract class CompiledExtractorBase<T, V, C> extends AbstractExtractor<T, V, C> implements Mappable<T, V, C> { |
24 | |
25 | protected int[] map; |
26 | |
27 | protected CompiledExtractorBase(int[] map, boolean contextDependent, int... parameterIndices) { |
28 | super(0, null, false, parameterIndices); |
29 | this.map = map; |
30 | } |
31 | |
32 | protected abstract CompiledExtractorBase<T, V, C> newInstance(int[] map); |
33 | |
34 | protected abstract List<Object> getIdentity(); |
35 | |
36 | /** |
37 | * Maps argument indices for invocation. |
38 | * @param idx |
39 | * @return |
40 | */ |
41 | protected int mapArg(int idx) { |
42 | if (map==null) { |
43 | return idx; |
44 | } |
45 | |
46 | for (int i=0; i<map.length; ++i) { |
47 | if (map[i]==idx) { |
48 | return i; |
49 | } |
50 | } |
51 | |
52 | throw new IllegalArgumentException("Cannot map index "+idx+" using map "+Arrays.toString(map)); |
53 | } |
54 | |
55 | @Override |
56 | public Extractor<T,V,C> map(int[] map) { |
57 | if (ComparisonResult.isOneToOneMapping(map)) { |
58 | return this; |
59 | } |
60 | if (this.map==null) { |
61 | return newInstance(map); |
62 | } |
63 | |
64 | int[] newMap = new int[map.length]; |
65 | Arrays.fill(newMap, -1); |
66 | |
67 | Set<Integer> pi = new HashSet<Integer>(parameterIndices()); |
68 | |
69 | for (int i=0; i<newMap.length; ++i) { |
70 | if (map[i]!=-1) { |
71 | if (pi.remove(map[i])) { |
72 | newMap[i]=this.map[map[i]]; |
73 | } |
74 | } |
75 | } |
76 | |
77 | if (!pi.isEmpty()) { // Not all indexes were mapped. |
78 | return null; |
79 | } |
80 | |
81 | return newInstance(newMap); |
82 | |
83 | } |
84 | |
85 | @Override |
86 | public ComparisonResult compareTo(Extractor<T, V, C> other) { |
87 | if (other==this || other instanceof CompiledExtractorBase && getIdentity().equals(((CompiledExtractorBase<T,V,C>) other).getIdentity())) { |
88 | return ComparisonResult.EQUAL_NM; |
89 | } |
90 | |
91 | if (other instanceof CompiledExtractorBase) { |
92 | Object oi = ((CompiledExtractorBase<T,V,C>) other).getIdentity(); |
93 | if (oi instanceof List) { |
94 | Map<Integer,Integer> mapping = new HashMap<Integer,Integer>(); |
95 | if (map(getIdentity(), (List<Object>) oi, mapping)) { |
96 | int maxIdx = -1; |
97 | for (Integer idx: mapping.keySet()) { |
98 | if (idx>maxIdx) { |
99 | maxIdx = idx; |
100 | } |
101 | } |
102 | int[] retMap = new int[maxIdx+1]; |
103 | Arrays.fill(retMap, -1); |
104 | for (Map.Entry<Integer, Integer> e: mapping.entrySet()) { |
105 | retMap[e.getKey()] = e.getValue(); |
106 | } |
107 | return new ComparisonResult(Type.EQUAL, retMap); |
108 | } |
109 | } |
110 | } |
111 | return super.compareTo(other); |
112 | } |
113 | |
114 | private static boolean map(List<Object> thisIdentity, List<Object> otherIdentity, Map<Integer,Integer> mapping) { |
115 | if (thisIdentity.size()!=otherIdentity.size()) { |
116 | return false; |
117 | } |
118 | Iterator<Object> tit = thisIdentity.iterator(); |
119 | Iterator<Object> oit = otherIdentity.iterator(); |
120 | if (!(tit.hasNext() && tit.next().equals(oit.next()))) { |
121 | return false; |
122 | } |
123 | |
124 | Object thisName = tit.next(); |
125 | Object otherName = oit.next(); |
126 | if (thisName instanceof Integer && otherName instanceof Integer) { |
127 | Integer existingMapping = mapping.get(thisName); |
128 | if (existingMapping==null) { |
129 | mapping.put((Integer) thisName, (Integer) otherName); |
130 | } else if (!existingMapping.equals(otherName)) { |
131 | return false; |
132 | } |
133 | } else if (!cmp(thisName, otherName)) { |
134 | return false; |
135 | } |
136 | |
137 | // At this point types are equal, values are equal or mapped. |
138 | if (tit.hasNext()) { |
139 | if (!oit.hasNext()) { |
140 | return false; |
141 | } |
142 | List<List<Object>> tChildren = (List<List<Object>>) tit.next(); |
143 | List<List<Object>> oChildren = (List<List<Object>>) oit.next(); |
144 | if (tChildren.size()!=oChildren.size()) { |
145 | return false; |
146 | } |
147 | Iterator<List<Object>> tcit = tChildren.iterator(); |
148 | Iterator<List<Object>> ocit = oChildren.iterator(); |
149 | while (tcit.hasNext()) { |
150 | if (!map(tcit.next(), ocit.next(), mapping)) { |
151 | return false; |
152 | } |
153 | } |
154 | } |
155 | return true; |
156 | } |
157 | |
158 | private static boolean cmp(Object o1, Object o2) { |
159 | if (o1==null) { |
160 | return o2==null; |
161 | } |
162 | |
163 | return o1.equals(o2); |
164 | } |
165 | |
166 | @Override |
167 | public Set<Integer> parameterIndices() { |
168 | if (map==null) { |
169 | return super.parameterIndices(); |
170 | } |
171 | |
172 | Set<Integer> original = super.parameterIndices(); |
173 | Set<Integer> ret = new TreeSet<Integer>(); |
174 | for (int i=0; i<map.length; ++i) { |
175 | if (map[i]!=-1 && original.contains(map[i])) { |
176 | ret.add(i); |
177 | } |
178 | } |
179 | |
180 | return ret; |
181 | } |
182 | |
183 | } |