Am 16.04.2014 17:59, schrieb
jmcg...@gmail.com:
> Is there a different way I can tell SnakeYaml to use a different
> constructor for enums instead of enum.valueOf()?
Java simply does not allow to use a constructor on an enum (with a reason).
Java enums are for the use case that the Java side defines a fixed set
of values. So deserialization cannot *construct* an enum value, it can
only *select* one of the provided values.
If it's the database that defines what values exist, an enum simply
doesn't fit, you'll need a normal value type.
If it's indeed an enum, you'll have to write your own implementation for
valueOf.
E.g. I tend to write something like this:
enum Foo {
value_1("value-1"),
value_2("value_2");
private Foo(String databaseString) {
this.databaseString = databaseString;
dbToEnum.put(databaseString, this);
}
private static Map<String, Foo> dbToEnum
= new HashMap<String, Foo>();
public static Foo fromDbString(String dbString) {
return dbToEnum.get(dbString);
}
}
// Warning: unpolished&untested code
A HashMap might be overkill. AFAIK the valueOf function simply iterates
over all values of the enum and returns the first match.