01: /*
02: * Copyright (C) 2008 XStream Committers.
03: * All rights reserved.
04: *
05: * The software in this package is published under the terms of the BSD
06: * style license a copy of which has been included with this distribution in
07: * the LICENSE.txt file.
08: *
09: * Created on 12. February 2008 by Joerg Schaible
10: */
11: package com.thoughtworks.xstream.converters.enums;
12:
13: import com.thoughtworks.xstream.converters.basic.AbstractSingleValueConverter;
14:
15: /**
16: * A single value converter for arbitrary enums. Converter is internally automatically
17: * instantiated for enum types.
18: *
19: * @author Jörg Schaible
20: * @since 1.3
21: */
22: public class EnumSingleValueConverter extends
23: AbstractSingleValueConverter {
24:
25: private final Class enumType;
26:
27: public EnumSingleValueConverter(Class type) {
28: if (!Enum.class.isAssignableFrom(type) && type != Enum.class) {
29: throw new IllegalArgumentException(
30: "Converter can only handle defined enums");
31: }
32: enumType = type;
33: }
34:
35: public boolean canConvert(Class type) {
36: return enumType.isAssignableFrom(type);
37: }
38:
39: public Object fromString(String str) {
40: return Enum.valueOf(enumType, str);
41: }
42: }
|