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