Class BooleanConversion
- java.lang.Object
-
- com.univocity.parsers.conversions.NullConversion<java.lang.String,T>
-
- com.univocity.parsers.conversions.ObjectConversion<java.lang.Boolean>
-
- com.univocity.parsers.conversions.BooleanConversion
-
- All Implemented Interfaces:
Conversion<java.lang.String,java.lang.Boolean>
public class BooleanConversion extends ObjectConversion<java.lang.Boolean>
Converts Strings to Booleans and vice versaThis class supports multiple representations of boolean values. For example, you can define conversions from different Strings such as "Yes, Y, 1" to true, and "No, N, 0" to false.
The reverse conversion from a Boolean to String (in
revert(Boolean)
will return the first String provided in this class constructorUsing the previous example, a call to
revert(true)
will produce "Yes" and a callrevert(false)
will produce "No".
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.String
defaultForFalse
private java.lang.String
defaultForTrue
private java.util.Set<java.lang.String>
falseValues
private java.util.Set<java.lang.String>
trueValues
-
Constructor Summary
Constructors Constructor Description BooleanConversion(java.lang.Boolean valueIfStringIsNull, java.lang.String valueIfObjectIsNull, java.lang.String[] valuesForTrue, java.lang.String[] valuesForFalse)
Creates a Conversion from String to Boolean with default values to return when the input is null.BooleanConversion(java.lang.String[] valuesForTrue, java.lang.String[] valuesForFalse)
Creates conversions from String to Boolean.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected java.lang.Boolean
fromString(java.lang.String input)
Converts a String to a Booleanstatic java.lang.Boolean
getBoolean(java.lang.String booleanString, java.lang.String[] trueValues, java.lang.String[] falseValues)
Returns theBoolean
value represented by aString
, as defined by sets of Strings that representtrue
andfalse
values.private static java.lang.Boolean
getBoolean(java.lang.String defaultString, java.util.Set<java.lang.String> trueValues, java.util.Set<java.lang.String> falseValues)
private static java.lang.String
normalize(java.lang.String string)
Normalizes a given String by trimming whitespaces and converting it to lower case.private static void
normalize(java.util.Collection<java.lang.String> strings)
Normalizes the Strings in a given collection by trimming all elements and converting them to lower case.java.lang.String
revert(java.lang.Boolean input)
Converts a Boolean back to a String-
Methods inherited from class com.univocity.parsers.conversions.ObjectConversion
execute, fromInput, getValueIfObjectIsNull, getValueIfStringIsNull, setValueIfObjectIsNull, setValueIfStringIsNull, undo
-
Methods inherited from class com.univocity.parsers.conversions.NullConversion
getValueOnNullInput, getValueOnNullOutput, setValueOnNullInput, setValueOnNullOutput
-
-
-
-
Constructor Detail
-
BooleanConversion
public BooleanConversion(java.lang.String[] valuesForTrue, java.lang.String[] valuesForFalse)
Creates conversions from String to Boolean. This default constructor assumes the output of a conversion should be null when input is nullThe list of Strings that identify "true" the list of Strings that identify "false" are mandatory.
- Parameters:
valuesForTrue
- Strings that identify the boolean value true. The first element will be returned when executingrevert(true)
valuesForFalse
- Strings that identify the boolean value false. The first element will be returned when executing#revert(false)
-
BooleanConversion
public BooleanConversion(java.lang.Boolean valueIfStringIsNull, java.lang.String valueIfObjectIsNull, java.lang.String[] valuesForTrue, java.lang.String[] valuesForFalse)
Creates a Conversion from String to Boolean with default values to return when the input is null.The list of Strings that identify "true" the list of Strings that identify "false" are mandatory.
- Parameters:
valueIfStringIsNull
- default Boolean value to be returned when the input String is null. Used whenObjectConversion.execute(String)
is invoked.valueIfObjectIsNull
- default String value to be returned when a Boolean input is null. Used whenrevert(Boolean)
is invoked.valuesForTrue
- Strings that identify the boolean value true. The first element will be returned when executingrevert(true)
valuesForFalse
- Strings that identify the boolean value false. The first element will be returned when executing#revert(false)
-
-
Method Detail
-
revert
public java.lang.String revert(java.lang.Boolean input)
Converts a Boolean back to a StringThe return value depends on the list of values for true/false provided in the constructor of this class.
- Specified by:
revert
in interfaceConversion<java.lang.String,java.lang.Boolean>
- Overrides:
revert
in classObjectConversion<java.lang.Boolean>
- Parameters:
input
- the Boolean to be converted to a String- Returns:
- a String representation for this boolean value, or the value of
ObjectConversion.getValueIfObjectIsNull()
if the Boolean input is null.
-
fromString
protected java.lang.Boolean fromString(java.lang.String input)
Converts a String to a Boolean- Specified by:
fromString
in classObjectConversion<java.lang.Boolean>
- Parameters:
input
- a String to be converted into a Boolean value.- Returns:
- true if the input String is part of
trueValues
, false if the input String is part offalseValues
, orObjectConversion.getValueIfStringIsNull()
if the input String is null.
-
getBoolean
public static java.lang.Boolean getBoolean(java.lang.String booleanString, java.lang.String[] trueValues, java.lang.String[] falseValues)
Returns theBoolean
value represented by aString
, as defined by sets of Strings that representtrue
andfalse
values.- Parameters:
booleanString
- the value that represents eithertrue
orfalse
trueValues
- a set of possible string values that representtrue
. If empty, then "true" will be assumed as the only acceptable representation.falseValues
- a set of possible string values that representfalse
. If empty, then "false" will be assumed as the only acceptable representation.- Returns:
- the boolean value that the input string represents
- Throws:
DataProcessingException
- if the input string does not match any value provided in neither set of possible values.
-
getBoolean
private static java.lang.Boolean getBoolean(java.lang.String defaultString, java.util.Set<java.lang.String> trueValues, java.util.Set<java.lang.String> falseValues)
-
normalize
private static java.lang.String normalize(java.lang.String string)
Normalizes a given String by trimming whitespaces and converting it to lower case.- Parameters:
string
- a String to be normalized.- Returns:
- the normalized version of the original String.
-
normalize
private static void normalize(java.util.Collection<java.lang.String> strings)
Normalizes the Strings in a given collection by trimming all elements and converting them to lower case.- Parameters:
strings
- a String collection with elements to be normalized. The original contents of the collection will be modified.
-
-