Вопрос по new-operator, java – SimpleXML перечисление чувствительность к регистру

3

Error: User Rate Limit ExceededError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

 package pojos;

public enum MyEnum {

    NEW("new"),
    OLD("old");

     private final String value;

     MyEnum(String v)
     {
         value = v;
     }

     public String value() {
            return value;
        }

        public static MyEnum fromValue(String v) {
            for (MyEnum c: MyEnum.values()) {
                if (c.value.equals(v)) {
                    return c;
                }
            }
            throw new IllegalArgumentException(v);
        }

}

Error: User Rate Limit Exceeded

import java.io.File;

import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;

import pojos.MyEnum;


public class TestEnum {

    /**
     * @param args
     * @throws Exception 
     */
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub

        Serializer serializer = new Persister();
        MyEnum example = MyEnum.NEW;
        File result = new File("myenum.xml");

        serializer.write(example, result);

    }

}

Error: User Rate Limit Exceeded

<myEnum>NEW</myEnum>

Error: User Rate Limit Exceeded

<myEnum>new</myEnum>

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Ваш Ответ

3   ответа
1

Error: User Rate Limit ExceededtoString()

@Override 
public String toString() {
       return this.value.toLowerCase();
}

Error: User Rate Limit Exceeded

serializer.write(example.toString(), result);
Error: User Rate Limit Exceeded zombie
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded zombie
Error: User Rate Limit Exceeded zombie
Error: User Rate Limit Exceeded
0

12

TransformEnumTransformTransformtoString()name()

class MyEnumTransform implements Transform<Enum> {
    private final Class type;

    public MyEnumTransform(Class type) {
        this.type = type;
    }

    public Enum read(String value) throws Exception {
        for (Object o : type.getEnumConstants()) {
            if (o.toString().equals(value)) {
                return (Enum) o;
            }
        }
        return null;
    }

    public String write(Enum value) throws Exception {
        return value.toString();
    }
}

TransformmatchMatcherMatcherTransformerMatcherPersister

Persister serializer = new Persister(new Matcher() {
    public Transform match(Class type) throws Exception {
        if (type.isEnum()) {
            return new MyEnumTransform(type);
        }
        return null;
    }
 });

Error: User Rate Limit Exceeded zombie
Error: User Rate Limit Exceeded zombie
Error: User Rate Limit Exceeded zombie
Error: User Rate Limit Exceededcannot return custom Transform objectsError: User Rate Limit Exceeded
Error: User Rate Limit Exceeded

Похожие вопросы