Revision 44662 trunk/org.gvsig.desktop/org.gvsig.desktop.plugin/org.gvsig.labeling.app/org.gvsig.labeling.app.mainplugin/src/main/java/org/gvsig/labeling/lang/EvaluatorCreator.java

View differences:

EvaluatorCreator.java
1 1
package org.gvsig.labeling.lang;
2 2

  
3
import java.util.HashMap;
4
import java.util.Iterator;
5 3
import java.util.Map;
4
import org.apache.commons.collections.map.LRUMap;
5
import org.gvsig.fmap.dal.DALLocator;
6 6

  
7 7
import org.gvsig.tools.evaluator.EvaluatorWithDescriptions;
8
import org.gvsig.tools.evaluator.sqljep.SQLJEPEvaluator;
9 8

  
10 9

  
11 10
/**
12 11
 * 
13 12
 * Utility class to centralize the creation of an
14 13
 * @link {@link EvaluatorWithDescriptions}
15
 * 
16
 * TODO Perhaps create a manager in Tools to perform this operation
17
 * 
18
 * @author jldominguez
19
 *
20 14
 */
21 15
public class EvaluatorCreator {
22 16

  
23
	private static Map<String, EvaluatorWithDescriptions> evCache =
24
			new HashMap<String, EvaluatorWithDescriptions>();
17
    private static final Map<String, EvaluatorWithDescriptions> EVALUATORS_CACHE = new LRUMap(100);
25 18
	
26 19
    /**
27 20
     * Create an {@link EvaluatorWithDescriptions} that uses
......
31 24
     * @return
32 25
     */
33 26
    public static EvaluatorWithDescriptions getEvaluator(String expr) {
34
    	
35
    	EvaluatorWithDescriptions resp = evCache.get(expr);
27
    	EvaluatorWithDescriptions resp = EVALUATORS_CACHE.get(expr);
36 28
    	if (resp == null) {
37
    		resp = new SQLJEPEvaluator(expr);
38
    		if (evCache.size() > 100) {
39
    			removeOne(evCache);
40
    		}
41
    		evCache.put(expr, resp);
29
                try {
30
                    resp = (EvaluatorWithDescriptions) DALLocator.getDataManager().createFilter(expr);
31
                } catch (Exception ex) {
32
                    throw new RuntimeException("Can't create evaluator for '"+expr+"'.", ex);
33
                }
34
    		EVALUATORS_CACHE.put(expr, resp);
42 35
    	}
43 36
        return resp; 
44 37
    }
45 38
    
46
	private static void removeOne(
47
			Map<String, EvaluatorWithDescriptions> themap) {
48
		
49
		Iterator<String> iter = themap.keySet().iterator();
50
		int count = 50;
51
		String k = null;
52
		while (iter.hasNext() && count > 0) {
53
			k = iter.next();
54
			count--;
55
		}
56
		themap.remove(k);
57
	}
58

  
59 39
}

Also available in: Unified diff