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.exception.SuperCsvCellProcessorException; 25 import org.supercsv.util.CsvContext; 26 27 /** 28 * This processor is used in the situations you want to be able to check for the presence of a "special 29 * token". Such a token could be the string "[empty]" which could denote that a column is different from the empty 30 * string "". 31 * <p> 32 * For example, to convert the String <tt>"[empty]"</tt> to -1 (an int representing 'empty') you could use <code> 33 * new Token("[empty]", -1) 34 * </code> 35 * <p> 36 * Comparison between the input and the <tt>token</tt> is based on the object's <tt>equals()</tt> method. 37 * 38 * @since 1.02 39 * @author Kasper B. Graversen 40 */ 41 public class Token extends CellProcessorAdaptor implements BoolCellProcessor, DateCellProcessor, DoubleCellProcessor, 42 LongCellProcessor, StringCellProcessor { 43 44 private final Object returnValue; 45 private final Object token; 46 47 /** 48 * Constructs a new <tt>Token</tt> processor, which returns the supplied value if the token is encountered, 49 * otherwise it returns the input unchanged. 50 * 51 * @param token 52 * the token 53 * @param returnValue 54 * the value to return if the token is encountered 55 */ 56 public Token(final Object token, final Object returnValue) { 57 super(); 58 this.token = token; 59 this.returnValue = returnValue; 60 } 61 62 /** 63 * Constructs a new <tt>Token</tt> processor, which returns the supplied value if the token is encountered, 64 * otherwise it passes the input unchanged to the next processor in the chain. 65 * 66 * @param token 67 * the token 68 * @param returnValue 69 * the value to return if the token is encountered 70 * @param next 71 * the next processor in the chain 72 * @throws NullPointerException 73 * if next is null 74 */ 75 public Token(final Object token, final Object returnValue, final CellProcessor next) { 76 super(next); 77 this.token = token; 78 this.returnValue = returnValue; 79 } 80 81 /** 82 * {@inheritDoc} 83 * 84 * @throws SuperCsvCellProcessorException 85 * if value is null 86 */ 87 public Object execute(final Object value, final CsvContext context) { 88 validateInputNotNull(value, context); 89 90 if( value.equals(token) ) { 91 return returnValue; 92 } 93 94 return next.execute(value, context); 95 } 96 }