org.supercsv.cellprocessor.ConvertNullTo Maven / Gradle / Ivy
Show all versions of super-csv Show documentation
/*
* Copyright 2007 Kasper B. Graversen
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.supercsv.cellprocessor;
import org.supercsv.cellprocessor.ift.BoolCellProcessor;
import org.supercsv.cellprocessor.ift.CellProcessor;
import org.supercsv.cellprocessor.ift.DateCellProcessor;
import org.supercsv.cellprocessor.ift.DoubleCellProcessor;
import org.supercsv.cellprocessor.ift.LongCellProcessor;
import org.supercsv.cellprocessor.ift.StringCellProcessor;
import org.supercsv.util.CsvContext;
/**
* This processor returns a specified default value if the input is null. This is handy when writing partially
* filled beans, maps and arrays, as for each column a default value can be specified.
*
* To return the String "" when a null is encountered use
* new ConvertNullTo("\"\"");
*
*
* If you need further processing of the value in case the value is not null, you can link the processor with
* other processors such as
* new ConvertNullTo("\"\"", new Trim(3))
*
*
* @since 1.20
* @author Kasper B. Graversen
*/
public class ConvertNullTo extends CellProcessorAdaptor implements BoolCellProcessor, DateCellProcessor,
DoubleCellProcessor, LongCellProcessor, StringCellProcessor {
private final Object returnValue;
/**
* Constructs a new ConvertNullTo processor, which returns a specified default value if the input is
* null.
*
* @param returnValue
* the value to return if the input is null
*/
public ConvertNullTo(final Object returnValue) {
super();
this.returnValue = returnValue;
}
/**
* Constructs a new ConvertNullTo processor, which returns a specified default value if the input is
* null. If the input is not null, then the next processor is executed.
*
* @param returnValue
* the value to return if the input is null
* @param next
* the next CellProcessor in the chain
* @throws NullPointerException
* if next is null
*/
public ConvertNullTo(final Object returnValue, final CellProcessor next) {
super(next);
this.returnValue = returnValue;
}
/**
* {@inheritDoc}
*/
public Object execute(final Object value, final CsvContext context) {
if( value == null ) {
return returnValue;
}
return next.execute(value, context);
}
}