01: /*
02: * Copyright (C) 2005 Joe Walnes.
03: * Copyright (C) 2006, 2007, 2008 XStream Committers.
04: * All rights reserved.
05: *
06: * The software in this package is published under the terms of the BSD
07: * style license a copy of which has been included with this distribution in
08: * the LICENSE.txt file.
09: *
10: * Created on 20. March 2005 by Joe Walnes
11: */
12: package com.thoughtworks.xstream.mapper;
13:
14: import com.thoughtworks.xstream.alias.ClassMapper;
15: import com.thoughtworks.xstream.converters.ConverterLookup;
16: import com.thoughtworks.xstream.converters.SingleValueConverter;
17: import com.thoughtworks.xstream.converters.enums.EnumSingleValueConverter;
18:
19: import java.util.EnumSet;
20: import java.util.Map;
21: import java.util.WeakHashMap;
22:
23: /**
24: * Mapper that handles the special case of polymorphic enums in Java 1.5. This renames MyEnum$1
25: * to MyEnum making it less bloaty in the XML and avoiding the need for an alias per enum value
26: * to be specified. Additionally every enum is treated automatically as immutable type and can
27: * be written as attribute.
28: *
29: * @author Joe Walnes
30: * @author Jörg Schaible
31: */
32: public class EnumMapper extends AttributeMapper {
33:
34: private transient Map enumConverterMap = new WeakHashMap();
35:
36: public EnumMapper(Mapper wrapped, ConverterLookup converterLookup) {
37: super (wrapped, converterLookup);
38: }
39:
40: /**
41: * @deprecated since 1.3, use {@link #EnumMapper(Mapper, ConverterLookup)}
42: */
43: @Deprecated
44: public EnumMapper(Mapper wrapped) {
45: super (wrapped, null);
46: }
47:
48: /**
49: * @deprecated since 1.2, use {@link #EnumMapper(Mapper, ConverterLookup))}
50: */
51: @Deprecated
52: public EnumMapper(ClassMapper wrapped) {
53: this ((Mapper) wrapped, null);
54: }
55:
56: public String serializedClass(Class type) {
57: if (type == null) {
58: return super .serializedClass(type);
59: }
60: if (Enum.class.isAssignableFrom(type)
61: && type.getSuperclass() != Enum.class) {
62: return super .serializedClass(type.getSuperclass());
63: } else if (EnumSet.class.isAssignableFrom(type)) {
64: return super .serializedClass(EnumSet.class);
65: } else {
66: return super .serializedClass(type);
67: }
68: }
69:
70: public boolean isImmutableValueType(Class type) {
71: return (Enum.class.isAssignableFrom(type))
72: || super .isImmutableValueType(type);
73: }
74:
75: protected SingleValueConverter getLocalConverterFromItemType(
76: Class type) {
77: if (Enum.class.isAssignableFrom(type)) {
78: synchronized (enumConverterMap) {
79: SingleValueConverter singleValueConverter = (SingleValueConverter) enumConverterMap
80: .get(type);
81: if (singleValueConverter == null) {
82: singleValueConverter = new EnumSingleValueConverter(
83: type);
84: enumConverterMap.put(type, singleValueConverter);
85: }
86: return singleValueConverter;
87: }
88: }
89: return super .getLocalConverterFromItemType(type);
90: }
91:
92: private Object readResolve() {
93: this .enumConverterMap = new WeakHashMap();
94: return this;
95: }
96: }
|