Package org.supercsv.cellprocessor
Class ParseBool
- java.lang.Object
-
- org.supercsv.cellprocessor.CellProcessorAdaptor
-
- org.supercsv.cellprocessor.ParseBool
-
- All Implemented Interfaces:
CellProcessor
,StringCellProcessor
public class ParseBool extends CellProcessorAdaptor implements StringCellProcessor
Converts a String to a Boolean.The default values for true are: "true", "1", "y", "t"
The default values for false are: "false", "0", "n", "f"
By default (unless the ignoreCase parameter is supplied on the constructor) this processor will ignore the case of the value, i.e. "true", "TRUE", and "True" will all be converted to true (likewise for false).
Prior to version 2.2.1, this processor always ignored case, so it was necessary to ensure that all supplied true/false values were lowercase, as the input was converted to lowercase before comparison against the true/false values (to handle all variations of case in the input). This is no longer required (just use the ignoreCase parameter).
- Since:
- 1.0
-
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.String[]
DEFAULT_FALSE_VALUES
private static java.lang.String[]
DEFAULT_TRUE_VALUES
private java.util.Set<java.lang.String>
falseValues
private boolean
ignoreCase
private java.util.Set<java.lang.String>
trueValues
-
Fields inherited from class org.supercsv.cellprocessor.CellProcessorAdaptor
next
-
-
Constructor Summary
Constructors Constructor Description ParseBool()
Constructs a new ParseBool processor, which converts a String to a Boolean using the default values (ignoring case).ParseBool(boolean ignoreCase)
Constructs a new ParseBool processor, which converts a String to a Boolean using the default values (ignoring case if desired).ParseBool(boolean ignoreCase, BoolCellProcessor next)
Constructs a new ParseBool processor, which converts a String to a Boolean using the default values (ignoring case if desired), then calls the next processor in the chain.ParseBool(java.lang.String[] trueValues, java.lang.String[] falseValues)
Constructs a new ParseBool processor, which converts a String to a Boolean using the supplied true/false values (ignoring case).ParseBool(java.lang.String[] trueValues, java.lang.String[] falseValues, boolean ignoreCase)
Constructs a new ParseBool processor, which converts a String to a Boolean using the supplied true/false values (ignoring case if desired).ParseBool(java.lang.String[] trueValues, java.lang.String[] falseValues, boolean ignoreCase, BoolCellProcessor next)
Constructs a new ParseBool processor, which converts a String to a Boolean using the supplied true/false values (ignoring case if desired), then calls the next processor in the chain.ParseBool(java.lang.String[] trueValues, java.lang.String[] falseValues, BoolCellProcessor next)
Constructs a new ParseBool processor, which converts a String to a Boolean using the supplied true/false values (ignoring case), then calls the next processor in the chain.ParseBool(java.lang.String trueValue, java.lang.String falseValue)
Constructs a new ParseBool processor, which converts a String to a Boolean using the supplied true/false values (ignoring case).ParseBool(java.lang.String trueValue, java.lang.String falseValue, boolean ignoreCase)
Constructs a new ParseBool processor, which converts a String to a Boolean using the supplied true/false values (ignoring case if desired).ParseBool(java.lang.String trueValue, java.lang.String falseValue, boolean ignoreCase, BoolCellProcessor next)
Constructs a new ParseBool processor, which converts a String to a Boolean using the supplied true/false values (ignoring case if desired), then calls the next processor in the chain.ParseBool(java.lang.String trueValue, java.lang.String falseValue, BoolCellProcessor next)
Constructs a new ParseBool processor, which converts a String to a Boolean using the supplied true/false values (ignoring case), then calls the next processor in the chain.ParseBool(BoolCellProcessor next)
Constructs a new ParseBool processor, which converts a String to a Boolean using the default values (ignoring case), then calls the next processor in the chain.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static void
checkPreconditions(java.lang.String[] trueValues, java.lang.String[] falseValues)
Checks the preconditions for constructing a new ParseBool processor.private static void
checkPreconditions(java.lang.String trueValue, java.lang.String falseValue)
Checks the preconditions for constructing a new ParseBool processor.private static boolean
contains(java.util.Set<java.lang.String> set, java.lang.String value, boolean ignoreCase)
Returns true if the set contains the value, otherwise false.java.lang.Object
execute(java.lang.Object value, CsvContext context)
This method is invoked by the framework when the processor needs to process data or check constraints.-
Methods inherited from class org.supercsv.cellprocessor.CellProcessorAdaptor
toString, validateInputNotNull
-
-
-
-
Field Detail
-
DEFAULT_TRUE_VALUES
private static final java.lang.String[] DEFAULT_TRUE_VALUES
-
DEFAULT_FALSE_VALUES
private static final java.lang.String[] DEFAULT_FALSE_VALUES
-
trueValues
private final java.util.Set<java.lang.String> trueValues
-
falseValues
private final java.util.Set<java.lang.String> falseValues
-
ignoreCase
private final boolean ignoreCase
-
-
Constructor Detail
-
ParseBool
public ParseBool()
Constructs a new ParseBool processor, which converts a String to a Boolean using the default values (ignoring case).
-
ParseBool
public ParseBool(boolean ignoreCase)
Constructs a new ParseBool processor, which converts a String to a Boolean using the default values (ignoring case if desired).- Parameters:
ignoreCase
- whether to ignore the case when comparing against the true/false values- Since:
- 2.2.1
-
ParseBool
public ParseBool(BoolCellProcessor next)
Constructs a new ParseBool processor, which converts a String to a Boolean using the default values (ignoring case), then calls the next processor in the chain.- Parameters:
next
- the next processor in the chain- Throws:
java.lang.NullPointerException
- if next is null
-
ParseBool
public ParseBool(boolean ignoreCase, BoolCellProcessor next)
Constructs a new ParseBool processor, which converts a String to a Boolean using the default values (ignoring case if desired), then calls the next processor in the chain.- Parameters:
ignoreCase
- whether to ignore the case when comparing against the true/false valuesnext
- the next processor in the chain- Throws:
java.lang.NullPointerException
- if next is null- Since:
- 2.2.1
-
ParseBool
public ParseBool(java.lang.String trueValue, java.lang.String falseValue)
Constructs a new ParseBool processor, which converts a String to a Boolean using the supplied true/false values (ignoring case).- Parameters:
trueValue
- the String which represents truefalseValue
- the String which represents false- Throws:
java.lang.NullPointerException
- if trueValue or falseValue is null
-
ParseBool
public ParseBool(java.lang.String trueValue, java.lang.String falseValue, boolean ignoreCase)
Constructs a new ParseBool processor, which converts a String to a Boolean using the supplied true/false values (ignoring case if desired).- Parameters:
trueValue
- the String which represents truefalseValue
- the String which represents falseignoreCase
- whether to ignore the case when comparing against the true/false values- Throws:
java.lang.NullPointerException
- if trueValue or falseValue is null- Since:
- 2.2.1
-
ParseBool
public ParseBool(java.lang.String[] trueValues, java.lang.String[] falseValues)
Constructs a new ParseBool processor, which converts a String to a Boolean using the supplied true/false values (ignoring case).- Parameters:
trueValues
- the array of Strings which represent truefalseValues
- the array of Strings which represent false- Throws:
java.lang.IllegalArgumentException
- if trueValues or falseValues is emptyjava.lang.NullPointerException
- if trueValues or falseValues is null
-
ParseBool
public ParseBool(java.lang.String[] trueValues, java.lang.String[] falseValues, boolean ignoreCase)
Constructs a new ParseBool processor, which converts a String to a Boolean using the supplied true/false values (ignoring case if desired).- Parameters:
trueValues
- the array of Strings which represent truefalseValues
- the array of Strings which represent falseignoreCase
- whether to ignore the case when comparing against the true/false values- Throws:
java.lang.IllegalArgumentException
- if trueValues or falseValues is emptyjava.lang.NullPointerException
- if trueValues or falseValues is null- Since:
- 2.2.1
-
ParseBool
public ParseBool(java.lang.String trueValue, java.lang.String falseValue, BoolCellProcessor next)
Constructs a new ParseBool processor, which converts a String to a Boolean using the supplied true/false values (ignoring case), then calls the next processor in the chain.- Parameters:
trueValue
- the String which represents truefalseValue
- the String which represents falsenext
- the next processor in the chain- Throws:
java.lang.NullPointerException
- if trueValue, falseValue or next is null
-
ParseBool
public ParseBool(java.lang.String trueValue, java.lang.String falseValue, boolean ignoreCase, BoolCellProcessor next)
Constructs a new ParseBool processor, which converts a String to a Boolean using the supplied true/false values (ignoring case if desired), then calls the next processor in the chain.- Parameters:
trueValue
- the String which represents truefalseValue
- the String which represents falseignoreCase
- whether to ignore the case when comparing against the true/false valuesnext
- the next processor in the chain- Throws:
java.lang.NullPointerException
- if trueValue, falseValue or next is null- Since:
- 2.2.1
-
ParseBool
public ParseBool(java.lang.String[] trueValues, java.lang.String[] falseValues, BoolCellProcessor next)
Constructs a new ParseBool processor, which converts a String to a Boolean using the supplied true/false values (ignoring case), then calls the next processor in the chain.- Parameters:
trueValues
- the array of Strings which represent truefalseValues
- the array of Strings which represent falsenext
- the next processor in the chain- Throws:
java.lang.IllegalArgumentException
- if trueValues or falseValues is emptyjava.lang.NullPointerException
- if trueValues, falseValues, or next is null
-
ParseBool
public ParseBool(java.lang.String[] trueValues, java.lang.String[] falseValues, boolean ignoreCase, BoolCellProcessor next)
Constructs a new ParseBool processor, which converts a String to a Boolean using the supplied true/false values (ignoring case if desired), then calls the next processor in the chain.- Parameters:
trueValues
- the array of Strings which represent truefalseValues
- the array of Strings which represent falseignoreCase
- whether to ignore the case when comparing against the true/false valuesnext
- the next processor in the chain- Throws:
java.lang.IllegalArgumentException
- if trueValues or falseValues is emptyjava.lang.NullPointerException
- if trueValues, falseValues, or next is null- Since:
- 2.2.1
-
-
Method Detail
-
checkPreconditions
private static void checkPreconditions(java.lang.String trueValue, java.lang.String falseValue)
Checks the preconditions for constructing a new ParseBool processor.- Parameters:
trueValue
- the String which represents truefalseValue
- the String which represents false- Throws:
java.lang.NullPointerException
- if trueValue or falseValue is null
-
checkPreconditions
private static void checkPreconditions(java.lang.String[] trueValues, java.lang.String[] falseValues)
Checks the preconditions for constructing a new ParseBool processor.- Parameters:
trueValues
- the array of Strings which represent truefalseValues
- the array of Strings which represent false- Throws:
java.lang.IllegalArgumentException
- if trueValues or falseValues is emptyjava.lang.NullPointerException
- if trueValues or falseValues is null
-
execute
public java.lang.Object execute(java.lang.Object value, CsvContext context)
This method is invoked by the framework when the processor needs to process data or check constraints.- Specified by:
execute
in interfaceCellProcessor
- Parameters:
value
- the value to be processedcontext
- the CSV context- Returns:
- the result of cell processor execution
- Throws:
SuperCsvCellProcessorException
- if value is null, not a String, or can't be parsed to a Boolean
-
contains
private static boolean contains(java.util.Set<java.lang.String> set, java.lang.String value, boolean ignoreCase)
Returns true if the set contains the value, otherwise false.- Parameters:
set
- the setvalue
- the value to findignoreCase
- whether to ignore case- Returns:
- true if the set contains the value, otherwise false
-
-