1 /*
2 * Copyright 2006-2016 The JGUIraffe Team.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License")
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package net.sf.jguiraffe.di;
17
18 import org.apache.commons.beanutils.ConversionException;
19 import org.apache.commons.beanutils.Converter;
20
21 /**
22 * <p>
23 * A specialized {@code Converter} implementation dealing with all types of
24 * enumeration classes.
25 * </p>
26 * <p>
27 * The implementation of the {@link #convert(Class, Object)} method expects that
28 * the passed in value is a constant as defined for an enumeration class as
29 * string. It tries to find the corresponding instance of the enumeration class.
30 * </p>
31 * <p>
32 * An instance of this class is registered per default as base class converter
33 * at any new {@link InvocationHelper} instance. Therefore conversions to
34 * parameters or properties of {@code Enum} types are supported out of the box.
35 * </p>
36 * <p>
37 * Implementation note: This class has no state. So a single instance can be
38 * shared between multiple instances. This instance can be obtained using the
39 * {@link #getInstance()} factory method; creating new instances is now allowed.
40 * </p>
41 *
42 * @author Oliver Heger
43 * @version $Id: EnumConverter.java 205 2012-01-29 18:29:57Z oheger $
44 */
45 public final class EnumConverter implements Converter
46 {
47 /** Constant for the single shared instance of this class. */
48 private static final EnumConverter INSTANCE = new EnumConverter();
49
50 /**
51 * Private constructor so that no new instances can be created.
52 */
53 private EnumConverter()
54 {
55 }
56
57 /**
58 * Returns an instance of {@code EnumConverter}. Use this factory method to
59 * obtain a new instance rather than invoking a constructor.
60 *
61 * @return the {@code EnumConverter} instance
62 */
63 public static EnumConverter getInstance()
64 {
65 return INSTANCE;
66 }
67
68 /**
69 * Performs the type conversion to an enumeration constant. If this is not
70 * possible, a {@code ConversionException} is thrown. There are multiple
71 * reasons why this could happen, e.g. the value is not an exact string
72 * representation of an enumeration constant, or the class might be no
73 * {@code Enum} class.
74 *
75 * @param type the class to convert to
76 * @param value the object to be converted
77 * @return the result of the conversion
78 */
79 public Object convert(@SuppressWarnings("rawtypes") Class type, Object value)
80 {
81 assert type != null : "No target class!";
82 if (value == null)
83 {
84 throw new ConversionException(
85 "Cannot convert null enumeration constant!");
86 }
87
88 try
89 {
90 // valueOf() will throw an IllegalArgumentException if the class is
91 // not an Enum class.
92 @SuppressWarnings("unchecked")
93 Object result = Enum.valueOf(type, String.valueOf(value));
94 return result;
95 }
96 catch (IllegalArgumentException iex)
97 {
98 throw new ConversionException("Cannot convert enum constant "
99 + value + " to class " + type, iex);
100 }
101 }
102 }