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.CellProcessor;
20  import org.supercsv.cellprocessor.ift.DateCellProcessor;
21  import org.supercsv.cellprocessor.ift.DoubleCellProcessor;
22  import org.supercsv.cellprocessor.ift.LongCellProcessor;
23  import org.supercsv.cellprocessor.ift.StringCellProcessor;
24  import org.supercsv.util.CsvContext;
25  
26  /**
27   * This processor returns a specified default value if the input is <tt>null</tt>. This is handy when writing partially
28   * filled beans, maps and arrays, as for each column a default value can be specified.
29   * <p>
30   * To return the String <tt>""</tt> when a null is encountered use <code>
31   * new ConvertNullTo("\"\"");
32   * </code>
33   * <p>
34   * If you need further processing of the value in case the value is not <tt>null</tt>, you can link the processor with
35   * other processors such as <code>
36   * new ConvertNullTo("\"\"", new Truncate(3))
37   * </code>
38   * 
39   * @since 1.20
40   * @author Kasper B. Graversen
41   */
42  public class ConvertNullTo extends CellProcessorAdaptor implements BoolCellProcessor, DateCellProcessor,
43  	DoubleCellProcessor, LongCellProcessor, StringCellProcessor {
44  	
45  	private final Object returnValue;
46  	
47  	/**
48  	 * Constructs a new <tt>ConvertNullTo</tt> processor, which returns a specified default value if the input is
49  	 * <tt>null</tt>.
50  	 * 
51  	 * @param returnValue
52  	 *            the value to return if the input is <tt>null</tt>
53  	 */
54  	public ConvertNullTo(final Object returnValue) {
55  		super();
56  		this.returnValue = returnValue;
57  	}
58  	
59  	/**
60  	 * Constructs a new <tt>ConvertNullTo</tt> processor, which returns a specified default value if the input is
61  	 * <tt>null</tt>. If the input is not <tt>null</tt>, then the next processor is executed.
62  	 * 
63  	 * @param returnValue
64  	 *            the value to return if the input is <tt>null</tt>
65  	 * @param next
66  	 *            the next <tt>CellProcessor</tt> in the chain
67  	 * @throws NullPointerException
68  	 *             if next is null
69  	 */
70  	public ConvertNullTo(final Object returnValue, final CellProcessor next) {
71  		super(next);
72  		this.returnValue = returnValue;
73  	}
74  	
75  	/**
76  	 * {@inheritDoc}
77  	 */
78  	public Object execute(final Object value, final CsvContext context) {
79  		if( value == null ) {
80  			return returnValue;
81  		}
82  		
83  		return next.execute(value, context);
84  	}
85  }