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.BoolCellProcessor;
19  import org.supercsv.cellprocessor.ift.StringCellProcessor;
20  import org.supercsv.exception.SuperCsvCellProcessorException;
21  import org.supercsv.util.CsvContext;
22  
23  /**
24   * Converts a Boolean into a formatted string. If you want to convert from a String to a Boolean, use the
25   * {@link ParseBool} processor.
26   * 
27   * @since 1.50
28   * @author Dominique De Vito
29   */
30  public class FmtBool extends CellProcessorAdaptor implements BoolCellProcessor {
31  	
32  	private final String trueValue;
33  	private final String falseValue;
34  	
35  	/**
36  	 * Constructs a new <tt>FmtBool</tt> processor, which converts a Boolean into a formatted string.
37  	 * 
38  	 * @param trueValue
39  	 *            the String to use if the value is true
40  	 * @param falseValue
41  	 *            the String to use if the value is false
42  	 */
43  	public FmtBool(final String trueValue, final String falseValue) {
44  		super();
45  		this.trueValue = trueValue;
46  		this.falseValue = falseValue;
47  	}
48  	
49  	/**
50  	 * Constructs a new <tt>FmtBool</tt> processor, which converts a Boolean into a formatted string, then calls the
51  	 * next processor in the chain.
52  	 * 
53  	 * @param trueValue
54  	 *            the String to use if the value is true
55  	 * @param falseValue
56  	 *            the String to use if the value is false
57  	 * @param next
58  	 *            the next processor in the chain
59  	 * @throws NullPointerException
60  	 *             if next is null
61  	 */
62  	public FmtBool(final String trueValue, final String falseValue, final StringCellProcessor next) {
63  		super(next);
64  		this.trueValue = trueValue;
65  		this.falseValue = falseValue;
66  	}
67  	
68  	/**
69  	 * {@inheritDoc}
70  	 * 
71  	 * @throws SuperCsvCellProcessorException
72  	 *             if value is null or is not a Boolean
73  	 */
74  	public Object execute(final Object value, final CsvContext context) {
75  		validateInputNotNull(value, context);
76  		
77  		if( !(value instanceof Boolean) ) {
78  			throw new SuperCsvCellProcessorException(Boolean.class, value, context, this);
79  		}
80  		
81  		final String result = ((Boolean) value).booleanValue() ? trueValue : falseValue;
82  		return next.execute(result, context);
83  	}
84  }