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

package com.thoughtworks.xstream.converters.enums;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.mapper.Mapper;
import com.thoughtworks.xstream.core.util.Fields;

import java.lang.reflect.Field;
import java.util.EnumSet;
import java.util.Iterator;

/**
 * Serializes a Java 5 EnumSet.
 *
 * @author Joe Walnes
 */
public class EnumSetConverter implements Converter {

    private final Field typeField;
    private final Mapper mapper;

    public EnumSetConverter(Mapper mapper) {
        this.mapper = mapper;
        typeField = Fields.find(EnumSet.class, "elementType");
    }

    public boolean canConvert(Class type) {
        return EnumSet.class.isAssignableFrom(type);
    }

    public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
        EnumSet set = (EnumSet) source;
        Class enumTypeForSet = (Class) Fields.read(typeField, set);
        writer.addAttribute(mapper.aliasForAttribute("enum-type"), mapper.serializedClass(enumTypeForSet));
        writer.setValue(joinEnumValues(set));
    }

    private String joinEnumValues(EnumSet set) {
        boolean seenFirst = false;
        StringBuffer result = new StringBuffer();
        for (Iterator iterator = set.iterator(); iterator.hasNext();) {
            Enum value = (Enum) iterator.next();
            if (seenFirst) {
                result.append(',');
            } else {
                seenFirst = true;
            }
            result.append(value.name());
        }
        return result.toString();
    }

    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
        Class enumTypeForSet = mapper.realClass(reader.getAttribute(mapper.aliasForAttribute("enum-type")));
        EnumSet set = EnumSet.noneOf(enumTypeForSet);
        String[] enumValues = reader.getValue().split(",");
        for (int i = 0; i < enumValues.length; i++) {
            String enumValue = enumValues[i];
            if(enumValue.length() > 0) {
                set.add(Enum.valueOf(enumTypeForSet, enumValue));
            }
        }
        return set;
    }

}

@@ -820,16 +820,46 @@
  Walnes%0A
+ * @author J&ouml;rg Schaible%0A
  */%0Apubl
@@ -926,16 +926,23 @@
 e final 
+static 
 Field ty
@@ -958,79 +958,293 @@
     
-private final Mapper mapper;%0A%0A    public EnumSetConverter(Mapper mapper
+static %7B%0A        // field name is %22elementType%22 in Sun JDK, but different in Harmony %0A        Field assumedTypeField = null;%0A        Field%5B%5D fields = EnumSet.class.getDeclaredFields();%0A        for (int i = 0; i %3C fields.length; i++) %7B%0A            if (fields%5Bi%5D.getType() == Class.class
 ) %7B%0A
@@ -1255,91 +1255,487 @@
     
+ 
-this.mapper = mapper;%0A        typeField = Fields.find(EnumSet.class, %22elementType%22)
+       // take the fist member of type %22Class%22%0A                assumedTypeField = fields%5Bi%5D;%0A                assumedTypeField.setAccessible(true);%0A                break;%0A            %7D%0A        %7D%0A        if (assumedTypeField == null) %7B%0A            throw new InternalError(%22Cannot detect element type of EnumSet%22);%0A        %7D%0A        typeField = assumedTypeField;%0A    %7D%0A    %0A    private final Mapper mapper;%0A%0A    public EnumSetConverter(Mapper mapper) %7B%0A        this.mapper = mapper
 ;%0A  

Fix EnumSetConverter on Harmony (XSTR-379).