File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\util\LRUStringConverter.java.html

1 package hudson.util;
2
3 import com.thoughtworks.xstream.converters.basic.AbstractSingleValueConverter;
4 import com.thoughtworks.xstream.converters.basic.StringConverter;
5 import org.apache.commons.collections.map.LRUMap;
6
7 import java.util.Collections;
8 import java.util.HashMap;
9 import java.util.Map;
10
11 public class LRUStringConverter extends AbstractSingleValueConverter {
12
13 /**
14 * A Map to store strings as long as needed to map similar strings onto the same instance and conserve memory. The
15 * map can be set from the outside during construction, so it can be a LRU map or a weak map, synchronised or not.
16 */
17 private final Map<String,String> cache;
18
19 public LRUStringConverter() {
20 this(1000);
21 }
22
23 public LRUStringConverter(int size) {
24 cache = Collections.synchronizedMap(new LRUMap(size));
25 }
26
27 public boolean canConvert(final Class type) {
28 return type.equals(String.class);
29 }
30
31 public Object fromString(final String str) {
32 String s = cache.get(str);
33
34 if (s == null) {
35 cache.put(str, str);
36 s = str;
37 }
38
39 return s;
40 }
41 }