KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.thoughtworks.xstream.mapper;
2
3 import com.thoughtworks.xstream.alias.ClassMapper;
4
5 import java.util.Collections JavaDoc;
6 import java.util.HashMap JavaDoc;
7 import java.util.Map JavaDoc;
8
9 /**
10  * Mapper that resolves default implementations of classes. For example, mapper.lookupName(ArrayList.class) will return
11  * java.util.List. Calling mapper.defaultImplementationOf(List.class) will return ArrayList.
12  *
13  * @author Joe Walnes
14  */

15 public class DefaultImplementationsMapper extends MapperWrapper {
16
17     private final Map JavaDoc typeToImpl = Collections.synchronizedMap(new HashMap JavaDoc());
18     private final Map JavaDoc implToType = Collections.synchronizedMap(new HashMap JavaDoc());
19
20     public DefaultImplementationsMapper(ClassMapper wrapped) {
21         super(wrapped);
22         addDefaults();
23     }
24
25     protected void addDefaults() {
26         // register primitive types
27
addDefaultImplementation(Boolean JavaDoc.class, boolean.class);
28         addDefaultImplementation(Character JavaDoc.class, char.class);
29         addDefaultImplementation(Integer JavaDoc.class, int.class);
30         addDefaultImplementation(Float JavaDoc.class, float.class);
31         addDefaultImplementation(Double JavaDoc.class, double.class);
32         addDefaultImplementation(Short JavaDoc.class, short.class);
33         addDefaultImplementation(Byte JavaDoc.class, byte.class);
34         addDefaultImplementation(Long JavaDoc.class, long.class);
35     }
36
37     public void addDefaultImplementation(Class JavaDoc defaultImplementation, Class JavaDoc ofType) {
38         typeToImpl.put(ofType, defaultImplementation);
39         implToType.put(defaultImplementation, ofType);
40     }
41
42     public String JavaDoc serializedClass(Class JavaDoc type) {
43         Class JavaDoc baseType = (Class JavaDoc) implToType.get(type);
44         return baseType == null ? super.serializedClass(type) : super.serializedClass(baseType);
45     }
46
47     public Class JavaDoc defaultImplementationOf(Class JavaDoc type) {
48         Class JavaDoc result = (Class JavaDoc) typeToImpl.get(type);
49         return result == null ? super.defaultImplementationOf(type) : result;
50     }
51
52 }
53
Popular Tags