EMMA Coverage Report (generated Thu Jan 20 11:39:44 EST 2011)
[all classes][com.hammurapi.extract.scripting]

COVERAGE SUMMARY FOR SOURCE FILE [ScriptingExtractor.java]

nameclass, %method, %block, %line, %
ScriptingExtractor.java100% (2/2)73%  (8/11)74%  (280/380)73%  (51/70)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ScriptingExtractor100% (1/1)67%  (6/9)73%  (269/369)72%  (49/68)
isContextDependent (): boolean 0%   (0/1)0%   (0/2)0%   (0/1)
parameterIndices (): Set 0%   (0/1)0%   (0/3)0%   (0/1)
toString (): String 0%   (0/1)0%   (0/33)0%   (0/1)
extractInternal (Object, Map, Object []): Object 100% (1/1)68%  (45/66)70%  (7/10)
equals (Object): boolean 100% (1/1)74%  (83/112)57%  (16.6/29)
equal (String, String): boolean 100% (1/1)75%  (12/16)75%  (0.8/1)
hashCode (): int 100% (1/1)88%  (56/64)96%  (9.6/10)
ScriptingExtractor (String, ScriptEngineFactory, String []): void 100% (1/1)100% (70/70)100% (14/14)
access$0 (ScriptingExtractor): ScriptEngineFactory 100% (1/1)100% (3/3)100% (1/1)
     
class ScriptingExtractor$1100% (1/1)100% (2/2)100% (11/11)100% (3/3)
ScriptingExtractor$1 (ScriptingExtractor): void 100% (1/1)100% (6/6)100% (2/2)
initialValue (): ScriptEngine 100% (1/1)100% (5/5)100% (1/1)

1package com.hammurapi.extract.scripting;
2 
3import java.util.Arrays;
4import java.util.Map;
5import java.util.Set;
6import java.util.TreeSet;
7 
8import javax.script.ScriptEngine;
9import javax.script.ScriptEngineFactory;
10import javax.script.ScriptException;
11 
12import com.hammurapi.extract.Extractor;
13import com.hammurapi.extract.ExtractorBase;
14import com.hammurapi.extract.ExtractorException;
15import com.hammurapi.extract.ExtractorFactory;
16 
17class ScriptingExtractor<T, V, C> extends ExtractorBase<T, V, C> {
18 
19        private boolean doArgs = true;
20        private Set<Integer> parameterIndices = new TreeSet<Integer>();
21        private String[] parameterNames;
22        private ScriptEngineFactory sef;
23        private String code;
24        
25        public ScriptingExtractor(String code, ScriptEngineFactory sef, String[] parameterNames) {
26                super(0, null); // TODO cost from the factory?
27                for (String pn: parameterNames) {
28                        if (pn!=null) {
29                                doArgs = false;
30                        }
31                }
32                
33                for (int i=0; i<parameterNames.length; ++i) {
34                        if (doArgs || parameterNames[i]!=null) {
35                                parameterIndices.add(i);
36                        }
37                }
38                this.parameterNames = parameterNames;
39                this.sef = sef;
40                this.code = code;
41        }
42                        
43        public boolean isContextDependent() {
44                return true;
45        }
46 
47        public Set<Integer> parameterIndices() {
48                return parameterIndices;
49        }
50        
51        ThreadLocal<ScriptEngine> setl = new ThreadLocal<ScriptEngine>() {
52                
53                @Override
54                protected ScriptEngine initialValue() {
55                        return sef.getScriptEngine();
56                }
57        };
58 
59        @SuppressWarnings("unchecked")
60        @Override
61        protected V extractInternal(C context, Map<C, Map<Extractor<T, ? super V, C>, ? super V>> cache, T... obj) {                                
62                ScriptEngine engine = setl.get(); // Engine is requested once per thread. sef.getScriptEngine();
63                if (doArgs) {
64                        engine.put(ExtractorFactory.ARGS_BINDING, obj);
65                } else {
66                        for (int i=0, l=Math.min(parameterNames.length, obj.length); i<l; ++i) {
67                                if (parameterNames[i]!=null) {
68                                        engine.put(parameterNames[i], obj[i]);
69                                }
70                        }
71                }
72                engine.put(ExtractorFactory.CONTEXT_BINDING, context);
73                try {
74                        return (V) engine.eval(code);
75                } catch (ScriptException e) {
76                        throw new ExtractorException("Error evaluating '"+code+"'", e);
77                }
78        }
79 
80        @Override
81        public int hashCode() {
82                final int prime = 31;
83                int result = 1;
84                result = prime * result + ((code == null) ? 0 : code.hashCode());
85                result = prime * result + (doArgs ? 1231 : 1237);
86                result = prime
87                                * result
88                                + ((parameterIndices == null) ? 0 : parameterIndices.hashCode());
89                result = prime * result + Arrays.hashCode(parameterNames);
90                result = prime * result + ((sef == null) ? 0 : sef.hashCode());
91                return result;
92        }
93 
94        @Override
95        public boolean equals(Object obj) {
96                if (this == obj)
97                        return true;
98                
99                if (obj == null)
100                        return false;
101                
102                if (getClass() != obj.getClass())
103                        return false;
104                
105                ScriptingExtractor other = (ScriptingExtractor) obj;
106                
107                if (code == null) {
108                        if (other.code != null)
109                                return false;
110                } else if (!code.equals(other.code))
111                        return false;
112                
113                if (doArgs != other.doArgs)
114                        return false;
115                
116                if (parameterIndices == null) {
117                        if (other.parameterIndices != null)
118                                return false;
119                } else if (!parameterIndices.equals(other.parameterIndices))
120                        return false;
121                
122                if (!Arrays.equals(parameterNames, other.parameterNames))
123                        return false;
124                
125                if (sef == null) {
126                        if (other.sef != null)
127                                return false;
128                } else if (!sef.equals(other.sef)) {
129                        return sef.getClass().equals(other.sef.getClass()) 
130                        && equal(sef.getEngineName(), other.sef.getEngineName()) 
131                        && equal(sef.getEngineVersion(), other.sef.getEngineVersion());
132                }
133                return true;
134        }
135 
136        private boolean equal(String s1, String s2) {
137                return (s1==null && s2==null) || (s1!=null && s2!=null && s1.equals(s2));
138        }
139        
140        @Override
141        public String toString() {
142                return getClass().getName()+"("+sef.getEngineName()+" v. "+sef.getEngineVersion()+", code '"+code+"', parameter indices "+parameterIndices+")";
143        }
144}

[all classes][com.hammurapi.extract.scripting]
EMMA 2.0.5312 EclEmma Fix 2 (C) Vladimir Roubtsov