package org.supercsv.cellprocessor;

import org.supercsv.cellprocessor.ift.BoolCellProcessor;
import org.supercsv.exception.NullInputException;
import org.supercsv.exception.SuperCSVException;
import org.supercsv.util.CSVContext;

/**
 * Convert a string to a boolean while ignoring case. 
 * The strings "true", "false", "0", "1", "N", "Y", "F", "T" are 
 * the default accepted strings.
 * 
 * @author Kasper B. Graversen
 * @author Dominique De Vito
 * @since 1.0
 */
public class ParseBool extends CellProcessorAdaptor {

	private static String[] DEFAULT_TRUE_VALUES = new String[] {
		"1",
		"true",
		"t",
		"y"
	};

	private static String[] DEFAULT_FALSE_VALUES = new String[] {
		"0",
		"false",
		"f",
		"n"
	};
	
	private String[] trueValues;
	private String[] falseValues;

	public ParseBool() {
		this(DEFAULT_TRUE_VALUES, DEFAULT_FALSE_VALUES);
	}

	public ParseBool(final BoolCellProcessor next) {
		this(DEFAULT_TRUE_VALUES, DEFAULT_FALSE_VALUES, next);
	}

	public ParseBool(final String[] trueValues, final String[] falseValues) {
		super();
		this.trueValues = trueValues;
		this.falseValues = falseValues;
	}

	public ParseBool(final String[] trueValues, final String[] falseValues, final BoolCellProcessor next) {
		super(next);
		this.trueValues = trueValues;
		this.falseValues = falseValues;
	}

	
	/**
	 * {@inheritDoc}
	 * 
	 * @throws SuperCSVException
	 *             when the value is some value that cannot be translated into a boolean value
	 */
	@Override
	public Object execute(final Object value, final CSVContext context) throws SuperCSVException {
		if( value == null ) { throw new NullInputException("Input cannot be null", context, this); }
		Boolean result;
		final String sval = ((String) value).toLowerCase();
		if(isFalseValue(sval)) {
			result = Boolean.FALSE;
		}
		else if(isTrueValue(sval)) {
			result = Boolean.TRUE;
		}
		else {
			throw new SuperCSVException("Cannot parse \"" + value + "\" to a boolean", 
					context, this);
		}

		return next.execute(result, context);
	}

	private boolean isTrueValue(final String sval) {
		return indexOf(sval, trueValues) >= 0;
	}

	private boolean isFalseValue(final String sval) {
		return indexOf(sval, falseValues) >= 0;
	}
	
	private static int indexOf(final String sval, final String[] possibleMatches) {
		if (possibleMatches == null) {
			return -1;
		}
		for (int i = 0; i < possibleMatches.length; i++) {
			if (sval.equals(possibleMatches[i])) {
				return i;
			}
		}
		return -1;
	}
}

@@ -458,16 +458,36 @@
  De Vito
+ (ddv36a78@yahoo.fr)
 %0D%0A * @si

add of dom's email address.