| 1 | package com.hammurapi.extract.java; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Collection; |
| 5 | import java.util.Collections; |
| 6 | import java.util.List; |
| 7 | import java.util.Map; |
| 8 | import java.util.Set; |
| 9 | import java.util.concurrent.TimeUnit; |
| 10 | |
| 11 | import org.codehaus.commons.compiler.CompileException; |
| 12 | import org.codehaus.janino.ExpressionEvaluator; |
| 13 | |
| 14 | import com.hammurapi.common.Identifiable; |
| 15 | import com.hammurapi.extract.ComparisonResult; |
| 16 | import com.hammurapi.extract.Extractor; |
| 17 | import com.hammurapi.extract.ExtractorBase; |
| 18 | import com.hammurapi.extract.ExtractorException; |
| 19 | import com.hammurapi.extract.Mappable; |
| 20 | |
| 21 | public class JavaExtractor<T, V, C> extends ExtractorBase<T, V, C> implements Extractor<T, V, C> , Mappable<T,V,C>, Identifiable<List<Object>> { |
| 22 | |
| 23 | private Params<T, C> params; |
| 24 | private Class<V> valueType; |
| 25 | private ClassLoader classLoader; |
| 26 | |
| 27 | public JavaExtractor(double initialCost, TimeUnit costUnit, Params<T, C> params, Class<V> valueType, ClassLoader classLoader) { |
| 28 | super(initialCost, costUnit); |
| 29 | this.params = params; |
| 30 | this.valueType = valueType; |
| 31 | this.classLoader = classLoader; |
| 32 | } |
| 33 | |
| 34 | private ThreadLocal<ExpressionEvaluator> eetl = new ThreadLocal<ExpressionEvaluator>() { |
| 35 | |
| 36 | protected ExpressionEvaluator initialValue() { |
| 37 | try { |
| 38 | // TODO - Pass classLoader? |
| 39 | return new ExpressionEvaluator(params.expr.toString(), valueType, params.names, params.types); |
| 40 | } catch (CompileException e) { |
| 41 | throw new ExtractorException("Compile exception in code '"+params.expr+"': "+e, e); |
| 42 | } |
| 43 | }; |
| 44 | }; |
| 45 | |
| 46 | @SuppressWarnings("unchecked") |
| 47 | @Override |
| 48 | protected V extractInternal(C context, Map<C, Map<Extractor<T, ? super V, C>, ? super V>> cache, T... obj) { |
| 49 | Object[] args = params.translate(obj, context); |
| 50 | try { |
| 51 | return (V) eetl.get().evaluate(args); |
| 52 | } catch (Exception e) { |
| 53 | throw new ExtractorException("Invocation exception in code '"+params.expr+"': "+e, e); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public boolean isContextDependent() { |
| 59 | return params.isContextDependent(); |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public Set<Integer> parameterIndices() { |
| 64 | return params.indices; |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public String toString() { |
| 69 | return getClass().getName()+"(code: '"+params.expr+"', parameter indices "+parameterIndices()+", cost: "+getCost()+")"; |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public int hashCode() { |
| 74 | final int prime = 31; |
| 75 | int result = 1; |
| 76 | result = prime * result |
| 77 | + ((classLoader == null) ? 0 : classLoader.hashCode()); |
| 78 | result = prime * result + ((params == null) ? 0 : params.hashCode()); |
| 79 | result = prime * result |
| 80 | + ((valueType == null) ? 0 : valueType.hashCode()); |
| 81 | return result; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public ComparisonResult compareTo(Extractor<T, V, C> obj) { |
| 86 | if (this == obj) { |
| 87 | return ComparisonResult.EQUAL_NM; |
| 88 | } |
| 89 | if (obj == null) { |
| 90 | return ComparisonResult.NOT_EQUAL_NM; |
| 91 | } |
| 92 | |
| 93 | if (getClass() != obj.getClass()) { |
| 94 | return super.compareTo(obj); // For parenthesis, facades and other stuff. |
| 95 | } |
| 96 | |
| 97 | JavaExtractor other = (JavaExtractor) obj; |
| 98 | if (classLoader == null) { |
| 99 | if (other.classLoader != null) { |
| 100 | return ComparisonResult.NOT_EQUAL_NM; |
| 101 | } |
| 102 | } else if (!classLoader.equals(other.classLoader)) { |
| 103 | return ComparisonResult.NOT_EQUAL_NM; |
| 104 | } |
| 105 | |
| 106 | if (params == null) { |
| 107 | if (other.params != null) { |
| 108 | return ComparisonResult.NOT_EQUAL_NM; |
| 109 | } |
| 110 | } else if (!params.equals(other.params)) { |
| 111 | return ComparisonResult.NOT_EQUAL_NM; |
| 112 | } |
| 113 | if (valueType == null) { |
| 114 | if (other.valueType != null){ |
| 115 | return ComparisonResult.NOT_EQUAL_NM; |
| 116 | } |
| 117 | } else if (!valueType.equals(other.valueType)) { |
| 118 | return ComparisonResult.NOT_EQUAL_NM; |
| 119 | } |
| 120 | return ComparisonResult.EQUAL_NM; |
| 121 | } |
| 122 | |
| 123 | @SuppressWarnings("unchecked") |
| 124 | @Override |
| 125 | public Extractor<T, V, C> map(int[] map) { |
| 126 | if (ComparisonResult.isOneToOneMapping(map)) { |
| 127 | return this; |
| 128 | } |
| 129 | |
| 130 | Params<T,C> newParams = params.map(map); |
| 131 | if (newParams==null) { |
| 132 | return null; |
| 133 | } |
| 134 | |
| 135 | if (this instanceof JavaPredicate) { |
| 136 | return (Extractor<T, V, C>) new JavaPredicate<T, C>(initialCost, costUnit, newParams, classLoader); |
| 137 | } |
| 138 | |
| 139 | return new JavaExtractor<T, V, C>(initialCost, costUnit, newParams, valueType, classLoader); |
| 140 | } |
| 141 | |
| 142 | public List<Object> getIdentity() { |
| 143 | return params.getIdentity(); |
| 144 | } |
| 145 | |
| 146 | public interface Parameter { |
| 147 | int getIndex(); |
| 148 | Class<?> getType(); |
| 149 | String getName(); |
| 150 | } |
| 151 | |
| 152 | public Iterable<Parameter> getParameters() { |
| 153 | Collection<Parameter> ret = new ArrayList<Parameter>(); |
| 154 | final Integer[] ia = params.indices.toArray(new Integer[params.indices.size()]); |
| 155 | for (int i=0; i<params.names.length; ++i) { |
| 156 | class ParameterImpl implements Parameter { |
| 157 | |
| 158 | private int idx; |
| 159 | |
| 160 | public ParameterImpl(int idx) { |
| 161 | this.idx = idx; |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public int getIndex() { |
| 166 | return ia[idx]; |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public Class<?> getType() { |
| 171 | return params.types[idx]; |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public String getName() { |
| 176 | return params.names[idx]; |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public String toString() { |
| 181 | return "Parameter[idx=" + idx + ", type=" |
| 182 | + getType() + ", name=" + getName() + "]"; |
| 183 | } |
| 184 | |
| 185 | |
| 186 | } |
| 187 | |
| 188 | ret.add(new ParameterImpl(i)); |
| 189 | |
| 190 | } |
| 191 | |
| 192 | return Collections.unmodifiableCollection(ret); |
| 193 | } |
| 194 | |
| 195 | public String getExpression() { |
| 196 | return params.expr.toString(); |
| 197 | } |
| 198 | } |