1 | package com.hammurapi.extract; |
2 | |
3 | import java.util.Map; |
4 | import java.util.Set; |
5 | import java.util.TreeSet; |
6 | import java.util.concurrent.TimeUnit; |
7 | |
8 | |
9 | abstract class ArithmeticExtractor<T, V, C> extends ExtractorBase<T, V, C> implements Extractor<T, V, C>, Mappable<T, V, C>, BinaryExtractor<T,V,C> { |
10 | |
11 | protected enum PromotionLevel { |
12 | INT(0), |
13 | LONG(1), |
14 | FLOAT(2), |
15 | DOUBLE(3); |
16 | |
17 | private PromotionLevel(int level) { |
18 | this.level = level; |
19 | } |
20 | |
21 | int level; |
22 | } |
23 | |
24 | protected Extractor<T, V, C> leftExtractor; |
25 | protected Extractor<T, V, C> rightExtractor; |
26 | private boolean commutative; |
27 | |
28 | protected ArithmeticExtractor(double initialCost, TimeUnit costUnit, Extractor<T, V, C> leftExtractor, Extractor<T, V, C> rightExtractor, boolean commutative) { |
29 | super(initialCost, costUnit); |
30 | this.leftExtractor = leftExtractor; |
31 | this.rightExtractor = rightExtractor; |
32 | this.commutative = commutative; |
33 | } |
34 | |
35 | @Override |
36 | public boolean isContextDependent() { |
37 | return leftExtractor.isContextDependent() || rightExtractor.isContextDependent(); |
38 | } |
39 | |
40 | @Override |
41 | public Set<Integer> parameterIndices() { |
42 | Set<Integer> ret = new TreeSet<Integer>(); |
43 | ret.addAll(leftExtractor.parameterIndices()); |
44 | ret.addAll(rightExtractor.parameterIndices()); |
45 | return ret; |
46 | } |
47 | |
48 | @Override |
49 | public int hashCode() { |
50 | int result = ((leftExtractor == null) ? 0 : leftExtractor.hashCode()); |
51 | result += ((rightExtractor == null) ? 0 : rightExtractor.hashCode()); |
52 | return result; |
53 | } |
54 | |
55 | @Override |
56 | public ComparisonResult compareTo(Extractor<T, V, C> obj) { |
57 | if (this == obj) |
58 | return ComparisonResult.EQUAL_NM; |
59 | if (obj == null) |
60 | return ComparisonResult.NOT_EQUAL_NM; |
61 | if (getClass() != obj.getClass()) |
62 | return ComparisonResult.NOT_EQUAL_NM; |
63 | |
64 | ArithmeticExtractor other = (ArithmeticExtractor) obj; |
65 | if (leftExtractor.equals(other.leftExtractor) && rightExtractor.equals(other.rightExtractor)) { |
66 | return ComparisonResult.EQUAL_NM; |
67 | } |
68 | |
69 | if (commutative && leftExtractor.equals(other.rightExtractor) && rightExtractor.equals(other.leftExtractor)) { |
70 | return ComparisonResult.EQUAL_NM; |
71 | } |
72 | return super.compareTo(obj); |
73 | } |
74 | |
75 | @SuppressWarnings("unchecked") |
76 | @Override |
77 | protected V extractInternal( |
78 | C context, |
79 | Map<C, Map<Extractor<T, ? super V, C>, ? super V>> cache, |
80 | T... obj) { |
81 | |
82 | V o1 = leftExtractor.extract(context, cache, obj); |
83 | V o2 = rightExtractor.extract(context, cache, obj); |
84 | |
85 | if (o1 instanceof Number && o2 instanceof Number) { |
86 | Number n1 = (Number) o1; |
87 | Number n2 = (Number) o2; |
88 | switch (promote(n1, n2)) { |
89 | case INT: |
90 | return (V) doOp(n1.intValue(), n2.intValue()); |
91 | case LONG: |
92 | return (V) doOp(n1.longValue(), n2.longValue()); |
93 | case FLOAT: |
94 | return (V) doOp(n1.floatValue(), n2.floatValue()); |
95 | case DOUBLE: |
96 | return (V) doOp(n1.doubleValue(), n2.doubleValue()); |
97 | default: |
98 | throw new IllegalArgumentException("Unexpected promotion level "+promote(n1, n2)); |
99 | } |
100 | } else { |
101 | return doOp(o1, o2); |
102 | } |
103 | } |
104 | |
105 | protected V doOp(Object o1, Object o2) { |
106 | throw new UnsupportedOperationException("Can't do "+getClass().getName()+" on "+o1+" and "+o2); |
107 | } |
108 | |
109 | protected abstract Object doOp(int n1, int n2); |
110 | protected abstract Object doOp(long n1, long n2); |
111 | protected abstract Object doOp(float n1, float n2); |
112 | protected abstract Object doOp(double n1, double n2); |
113 | |
114 | private PromotionLevel nLevel(Number n) { |
115 | if (n instanceof Integer) { |
116 | return PromotionLevel.INT; |
117 | } |
118 | if (n instanceof Long) { |
119 | return PromotionLevel.LONG; |
120 | } |
121 | if (n instanceof Float) { |
122 | return PromotionLevel.FLOAT; |
123 | } |
124 | if (n instanceof Double) { |
125 | return PromotionLevel.DOUBLE; |
126 | } |
127 | throw new IllegalArgumentException(n.getClass()+" is not supported"); |
128 | } |
129 | |
130 | protected PromotionLevel promote(Number n1, Number n2) { |
131 | PromotionLevel pl1 = nLevel(n1); |
132 | PromotionLevel pl2 = nLevel(n2); |
133 | return pl1.level>=pl2.level ? pl1 : pl2; |
134 | } |
135 | |
136 | @Override |
137 | public String toString() { |
138 | return getClass().getName()+"("+leftExtractor+", "+rightExtractor+")"; |
139 | } |
140 | |
141 | @Override |
142 | public double getCost() { |
143 | if (cost==-1 && costUnit==null) { |
144 | return leftExtractor.getCost()+rightExtractor.getCost(); |
145 | } |
146 | return super.getCost(); |
147 | } |
148 | |
149 | @SuppressWarnings("unchecked") |
150 | @Override |
151 | public Extractor<T, V, C> map(int[] map) { |
152 | Mappable<T, V, C> leftMappable = ExtractorUtil.toMappable(leftExtractor); |
153 | if (leftMappable!=null) { |
154 | Mappable<T, V, C> rightMappable = ExtractorUtil.toMappable(rightExtractor); |
155 | if (rightMappable!=null) { |
156 | Extractor<T,V,C> lMapped = leftMappable.map(map); |
157 | if (lMapped!=null) { |
158 | Extractor<T,V,C> rMapped = rightMappable.map(map); |
159 | if (rMapped!=null) { |
160 | return newInstance(lMapped, rMapped); |
161 | } |
162 | } |
163 | } |
164 | } |
165 | return null; |
166 | } |
167 | |
168 | protected abstract Extractor<T, V, C> newInstance(Extractor<T, V, C> leftExtractor, Extractor<T, V, C> rightExtractor); |
169 | |
170 | public Extractor<T, V, C> getLeftExtractor() { |
171 | return leftExtractor; |
172 | } |
173 | |
174 | public Extractor<T, V, C> getRightExtractor() { |
175 | return rightExtractor; |
176 | } |
177 | } |