svn-gvsig-desktop / 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 @ 41192
History | View | Annotate | Download (1.39 KB)
1 | 40911 | jldominguez | package org.gvsig.labeling.lang; |
---|---|---|---|
2 | |||
3 | import java.util.HashMap; |
||
4 | import java.util.Iterator; |
||
5 | import java.util.Map; |
||
6 | |||
7 | import org.gvsig.tools.evaluator.EvaluatorWithDescriptions; |
||
8 | import org.gvsig.tools.evaluator.sqljep.SQLJEPEvaluator; |
||
9 | |||
10 | |||
11 | /**
|
||
12 | *
|
||
13 | * Utility class to centralize the creation of an
|
||
14 | * @link {@link EvaluatorWithDescriptions}
|
||
15 | *
|
||
16 | * TODO Perhaps create a manager in Tools to perform this operation
|
||
17 | *
|
||
18 | * @author jldominguez
|
||
19 | *
|
||
20 | */
|
||
21 | public class EvaluatorCreator { |
||
22 | |||
23 | private static Map<String, EvaluatorWithDescriptions> evCache = |
||
24 | new HashMap<String, EvaluatorWithDescriptions>(); |
||
25 | |||
26 | /**
|
||
27 | * Create an {@link EvaluatorWithDescriptions} that uses
|
||
28 | * the string expression provided
|
||
29 | *
|
||
30 | * @param expr
|
||
31 | * @return
|
||
32 | */
|
||
33 | public static EvaluatorWithDescriptions getEvaluator(String expr) { |
||
34 | |||
35 | EvaluatorWithDescriptions resp = evCache.get(expr); |
||
36 | if (resp == null) { |
||
37 | resp = new SQLJEPEvaluator(expr);
|
||
38 | if (evCache.size() > 100) { |
||
39 | removeOne(evCache); |
||
40 | } |
||
41 | evCache.put(expr, resp); |
||
42 | } |
||
43 | return resp;
|
||
44 | } |
||
45 | |||
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 | } |