Radika Moonesinghe
Radika Moonesinghe

Reputation: 499

handling XML enum types in java

In Java I have a class with a property :

@XmlElement(name = "Ability")
protected String Ability;

Ability can be either Low, Medium or High.

Later on in the code some un-validated data needs to be assigned to Ability.

test.setAbility(pdf.get("Ability"));

Should I enum ability and how can I make it so that Ability only gets set if it is one of the enum types?

Upvotes: 0

Views: 416

Answers (1)

DGK
DGK

Reputation: 3015

Using an enum would be beneficial in your situation, you could use a mapper to convert the strings:

public static String mapAbilityEnum(String ability) {
    switch (ability) {
    case "LOW":
        return AbilityEnum.LOW;
    case "MEDIUM":
        return AbilityEnum.MEDIUM;
    case "HIGH":
        return AbilityEnum.HIGH;
    default:
        return foo; (whatever you want the default to be)
    }
}

Upvotes: 2

Related Questions