KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > converters > enums > EnumSetConverter


1 // ***** READ THIS *****
2
// This class will only compile with JDK 1.5.0 or above as it test Java enums.
3
// If you are using an earlier version of Java, just don't try to build this class. XStream should work fine without it.
4

5 package com.thoughtworks.xstream.converters.enums;
6
7 import com.thoughtworks.xstream.converters.Converter;
8 import com.thoughtworks.xstream.converters.MarshallingContext;
9 import com.thoughtworks.xstream.converters.UnmarshallingContext;
10 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
11 import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
12 import com.thoughtworks.xstream.mapper.Mapper;
13 import com.thoughtworks.xstream.core.util.Fields;
14
15 import java.lang.reflect.Field JavaDoc;
16 import java.util.EnumSet JavaDoc;
17 import java.util.Iterator JavaDoc;
18
19 /**
20  * Serializes a Java 5 EnumSet.
21  *
22  * @author Joe Walnes
23  */

24 public class EnumSetConverter implements Converter {
25
26     private final Field JavaDoc typeField;
27     private final Mapper mapper;
28
29     public EnumSetConverter(Mapper mapper) {
30         this.mapper = mapper;
31         typeField = Fields.find(EnumSet JavaDoc.class, "elementType");
32     }
33
34     public boolean canConvert(Class JavaDoc type) {
35         return EnumSet JavaDoc.class.isAssignableFrom(type);
36     }
37
38     public void marshal(Object JavaDoc source, HierarchicalStreamWriter writer, MarshallingContext context) {
39         EnumSet JavaDoc set = (EnumSet JavaDoc) source;
40         Class JavaDoc enumTypeForSet = (Class JavaDoc) Fields.read(typeField, set);
41         writer.addAttribute(mapper.attributeForEnumType(), mapper.serializedClass(enumTypeForSet));
42         writer.setValue(joinEnumValues(set));
43     }
44
45     private String JavaDoc joinEnumValues(EnumSet JavaDoc set) {
46         boolean seenFirst = false;
47         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
48         for (Iterator JavaDoc iterator = set.iterator(); iterator.hasNext();) {
49             Enum JavaDoc value = (Enum JavaDoc) iterator.next();
50             if (seenFirst) {
51                 result.append(',');
52             } else {
53                 seenFirst = true;
54             }
55             result.append(value.name());
56         }
57         return result.toString();
58     }
59
60     public Object JavaDoc unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
61         Class JavaDoc enumTypeForSet = mapper.realClass(reader.getAttribute(mapper.attributeForEnumType()));
62         EnumSet JavaDoc set = EnumSet.noneOf(enumTypeForSet);
63         String JavaDoc[] enumValues = reader.getValue().split(",");
64         for (int i = 0; i < enumValues.length; i++) {
65             String JavaDoc enumValue = enumValues[i];
66             set.add(Enum.valueOf(enumTypeForSet, enumValue));
67         }
68         return set;
69     }
70
71 }
72
Popular Tags