View Javadoc
1   /*
2    * Copyright 2007 Kasper B. Graversen
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 org.supercsv.cellprocessor;
17  
18  import org.supercsv.cellprocessor.ift.CellProcessor;
19  import org.supercsv.cellprocessor.ift.StringCellProcessor;
20  import org.supercsv.exception.SuperCsvCellProcessorException;
21  import org.supercsv.util.CsvContext;
22  
23  /**
24   * Converts a String to an Enum. Patch originally supplied by Adrian Ber.
25   * 
26   * @author James Bassett
27   * @since 2.2.0
28   */
29  public class ParseEnum extends CellProcessorAdaptor implements StringCellProcessor {
30  	
31  	private final Class<? extends Enum<?>> enumClass;
32  	
33  	private final boolean ignoreCase;
34  	
35  	/**
36  	 * Constructs a new <tt>ParseEnum</tt> processor, which converts a String to a Enum.
37  	 * 
38  	 * @param enumClass
39  	 *            the enum class to convert to
40  	 * @param <T>
41  	 *            the Enum type
42  	 * @throws NullPointerException
43  	 *             if enumClass is null
44  	 */
45  	public <T extends Enum<?>> ParseEnum(final Class<T> enumClass) {
46  		super();
47  		checkPreconditions(enumClass);
48  		this.enumClass = enumClass;
49  		this.ignoreCase = false;
50  	}
51  	
52  	/**
53  	 * Constructs a new <tt>ParseEnum</tt> processor, which converts a String to a Enum, ignoring the case of the input
54  	 * (or not) depending on the supplied flag.
55  	 * 
56  	 * @param enumClass
57  	 *            the enum class to convert to
58  	 * @param ignoreCase
59  	 *            whether to ignore the case of the input
60  	 * @param <T>
61  	 *            the Enum type
62  	 * @throws NullPointerException
63  	 *             if enumClass is null
64  	 */
65  	public <T extends Enum<?>> ParseEnum(final Class<T> enumClass, final boolean ignoreCase) {
66  		super();
67  		checkPreconditions(enumClass);
68  		this.enumClass = enumClass;
69  		this.ignoreCase = ignoreCase;
70  	}
71  	
72  	/**
73  	 * Constructs a new <tt>ParseEnum</tt> processor, which converts a String to a Enum then calls the next processor in
74  	 * the chain.
75  	 * 
76  	 * @param enumClass
77  	 *            the enum class to convert to
78  	 * @param next
79  	 *            the next processor in the chain
80  	 * @param <T>
81  	 *            the Enum type
82  	 * @throws NullPointerException
83  	 *             if enumClass or next is null
84  	 */
85  	public <T extends Enum<?>> ParseEnum(final Class<T> enumClass, final CellProcessor next) {
86  		super(next);
87  		checkPreconditions(enumClass);
88  		this.enumClass = enumClass;
89  		this.ignoreCase = false;
90  	}
91  	
92  	/**
93  	 * Constructs a new <tt>ParseEnum</tt> processor, which converts a String to a Enum, ignoring the case of the input
94  	 * (or not) depending on the supplied flag, then calls the next processor in the chain.
95  	 * 
96  	 * @param enumClass
97  	 *            the enum class to convert to
98  	 * @param ignoreCase
99  	 *            whether to ignore the case of the input
100 	 * @param next
101 	 *            the next processor in the chain
102 	 * @param <T>
103 	 *            the Enum type
104 	 * @throws NullPointerException
105 	 *             if enumClass or next is null
106 	 */
107 	public <T extends Enum<?>> ParseEnum(final Class<T> enumClass, final boolean ignoreCase, final CellProcessor next) {
108 		super(next);
109 		checkPreconditions(enumClass);
110 		this.enumClass = enumClass;
111 		this.ignoreCase = ignoreCase;
112 	}
113 	
114 	/**
115 	 * Checks the preconditions for creating a new ParseEnum processor.
116 	 * 
117 	 * @param enumClass
118 	 *            the enum class
119 	 * @throws NullPointerException
120 	 *             if enumClass is null
121 	 */
122 	private static void checkPreconditions(final Class<?> enumClass) {
123 		if( enumClass == null ) {
124 			throw new NullPointerException("enumClass should not be null");
125 		}
126 	}
127 	
128 	/**
129 	 * {@inheritDoc}
130 	 * 
131 	 * @throws SuperCsvCellProcessorException
132 	 *             if value is null or can't be parsed as an Enum
133 	 */
134 	public Object execute(final Object value, final CsvContext context) {
135 		validateInputNotNull(value, context);
136 		
137 		final String inputString = value.toString();
138 		
139 		for( final Enum<?> enumConstant : enumClass.getEnumConstants() ) {
140 			String constantName = enumConstant.name();
141 			if( ignoreCase ? constantName.equalsIgnoreCase(inputString) : constantName.equals(inputString) ) {
142 				return enumConstant;
143 			}
144 		}
145 		
146 		throw new SuperCsvCellProcessorException(String.format("'%s' could not be parsed as a enum of type %s", value,
147 			enumClass.getName()), context, this);
148 		
149 	}
150 }