KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > mapper > CachingMapper


1 package com.thoughtworks.xstream.mapper;
2
3 import com.thoughtworks.xstream.alias.ClassMapper;
4
5 import java.util.Map JavaDoc;
6 import java.util.Collections JavaDoc;
7 import java.util.HashMap JavaDoc;
8
9 /**
10  * Mapper that caches which names map to which classes. Prevents repetitive searching and class loading.
11  *
12  * @author Joe Walnes
13  */

14 public class CachingMapper extends MapperWrapper {
15
16     private final Map JavaDoc cache = Collections.synchronizedMap(new HashMap JavaDoc());
17
18     public CachingMapper(ClassMapper wrapped) {
19         super(wrapped);
20     }
21
22     public Class JavaDoc realClass(String JavaDoc elementName) {
23         final String JavaDoc key = elementName;
24         Class JavaDoc cached = (Class JavaDoc) cache.get(key);
25         if (cached != null) {
26             return cached;
27         } else {
28             Class JavaDoc result = super.realClass(elementName);
29             cache.put(key, result);
30             return result;
31         }
32     }
33
34 }
35
Popular Tags