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.DoubleCellProcessor;
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 a Character. If the String has a {@literal length > 1}, then an Exception is thrown.
25   * 
26   * @since 1.10
27   * @author Kasper B. Graversen
28   */
29  public class ParseChar extends CellProcessorAdaptor implements StringCellProcessor {
30  	
31  	/**
32  	 * Constructs a new <tt>ParseChar</tt> processor, which converts a String to a Character.
33  	 */
34  	public ParseChar() {
35  		super();
36  	}
37  	
38  	/**
39  	 * Constructs a new <tt>ParseChar</tt> processor, which converts a String to a Character, then calls the next
40  	 * processor in the chain.
41  	 * 
42  	 * @param next
43  	 *            the next processor in the chain
44  	 * @throws NullPointerException
45  	 *             if next is null
46  	 */
47  	public ParseChar(final DoubleCellProcessor next) {
48  		super(next);
49  	}
50  	
51  	/**
52  	 * {@inheritDoc}
53  	 * 
54  	 * @throws SuperCsvCellProcessorException
55  	 *             if value is null, isn't a Character or String, or is a String of multiple characters
56  	 */
57  	public Object execute(final Object value, final CsvContext context) {
58  		validateInputNotNull(value, context);
59  		
60  		final Character result;
61  		if( value instanceof Character ) {
62  			result = (Character) value;
63  		} else if( value instanceof String ) {
64  			final String stringValue = (String) value;
65  			if( stringValue.length() == 1 ) {
66  				result = Character.valueOf(stringValue.charAt(0));
67  			} else {
68  				throw new SuperCsvCellProcessorException(String.format(
69  					"'%s' cannot be parsed as a char as it is a String longer than 1 character", stringValue), context,
70  					this);
71  			}
72  		} else {
73  			final String actualClassName = value.getClass().getName();
74  			throw new SuperCsvCellProcessorException(String.format(
75  				"the input value should be of type Character or String but is of type %s", actualClassName), context,
76  				this);
77  		}
78  		
79  		return next.execute(result, context);
80  	}
81  }