Top Description Fields Constructors Methods
com.univocity.parsers.conversions

public Class BooleanConversion

extends ObjectConversion<Boolean>
Class Inheritance
Imports
com.univocity.parsers.common.*, java.util.*

Converts Strings to Booleans and vice versa

This 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 BooleanConversion#revert(Boolean) will return the first String provided in this class constructor

Using the previous example, a call to revert(true) will produce "Yes" and a call revert(false) will produce "No".

Author
Univocity Software Pty Ltd - [email protected]

Field Summary

Modifier and TypeField and Description
private String
private String
private final Set<String>
private final Set<String>

Constructor Summary

AccessConstructor and Description
public
BooleanConversion(String[]
Strings that identify the boolean value true. The first element will be returned when executing revert(true)
valuesForTrue
,
String[]
Strings that identify the boolean value false. The first element will be returned when executing #revert(false)
valuesForFalse
)

Creates conversions from String to Boolean.

public
BooleanConversion(Boolean
default Boolean value to be returned when the input String is null. Used when ObjectConversion#execute(String) is invoked.
valueIfStringIsNull
,
String
default String value to be returned when a Boolean input is null. Used when BooleanConversion#revert(Boolean) is invoked.
valueIfObjectIsNull
,
String[]
Strings that identify the boolean value true. The first element will be returned when executing revert(true)
valuesForTrue
,
String[]
Strings that identify the boolean value false. The first element will be returned when executing #revert(false)
valuesForFalse
)

Creates a Conversion from String to Boolean with default values to return when the input is null.

Method Summary

Modifier and TypeMethod and Description
protected Boolean

Returns:

true if the input String is part of BooleanConversion#trueValues, false if the input String is part of BooleanConversion#falseValues, or BooleanConversion#getValueIfStringIsNull() if the input String is null.
fromString
(String
a String to be converted into a Boolean value.
input
)

Implements abstract com.univocity.parsers.conversions.ObjectConversion.fromString.

Converts a String to a Boolean
public static Boolean

Returns:

the boolean value that the input string represents
getBoolean
(String
the value that represents either true or false
booleanString
,
String[]
a set of possible string values that represent true. If empty, then "true" will be assumed as the only acceptable representation.
trueValues
,
String[]
a set of possible string values that represent false. If empty, then "false" will be assumed as the only acceptable representation.
falseValues
)

Returns the Boolean value represented by a String, as defined by sets of Strings that represent true and false values.

private static Boolean
getBoolean(String defaultString, Set<String> trueValues, Set<String> falseValues)

private static String

Returns:

the normalized version of the original String.
normalize
(String
a String to be normalized.
string
)

Normalizes a given String by trimming whitespaces and converting it to lower case.

private static void
normalize(Collection<String>
a String collection with elements to be normalized. The original contents of the collection will be modified.
strings
)

Normalizes the Strings in a given collection by trimming all elements and converting them to lower case.

public String

Returns:

a String representation for this boolean value, or the value of BooleanConversion#getValueIfObjectIsNull() if the Boolean input is null.
revert
(Boolean
the Boolean to be converted to a String
input
)

Overrides com.univocity.parsers.conversions.ObjectConversion.revert.

Implements com.univocity.parsers.conversions.Conversion.revert.

Converts a Boolean back to a String

The return value depends on the list of values for true/false provided in the constructor of this class.

Inherited from com.univocity.parsers.conversions.ObjectConversion:
executefromInputgetValueIfObjectIsNullgetValueIfStringIsNullsetValueIfObjectIsNullsetValueIfStringIsNullundo

Field Detail

defaultForFalseback to summary
private String defaultForFalse
defaultForTrueback to summary
private String defaultForTrue
falseValuesback to summary
private final Set<String> falseValues
trueValuesback to summary
private final Set<String> trueValues

Constructor Detail

BooleanConversionback to summary
public BooleanConversion(String[] valuesForTrue, String[] valuesForFalse)

Creates conversions from String to Boolean. This default constructor assumes the output of a conversion should be null when input is null

The list of Strings that identify "true" the list of Strings that identify "false" are mandatory.

Parameters
valuesForTrue:String[]

Strings that identify the boolean value true. The first element will be returned when executing revert(true)

valuesForFalse:String[]

Strings that identify the boolean value false. The first element will be returned when executing #revert(false)

BooleanConversionback to summary
public BooleanConversion(Boolean valueIfStringIsNull, String valueIfObjectIsNull, String[] valuesForTrue, 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:Boolean

default Boolean value to be returned when the input String is null. Used when ObjectConversion#execute(String) is invoked.

valueIfObjectIsNull:String

default String value to be returned when a Boolean input is null. Used when BooleanConversion#revert(Boolean) is invoked.

valuesForTrue:String[]

Strings that identify the boolean value true. The first element will be returned when executing revert(true)

valuesForFalse:String[]

Strings that identify the boolean value false. The first element will be returned when executing #revert(false)

Method Detail

fromStringback to summary
protected Boolean fromString(String input)

Implements abstract com.univocity.parsers.conversions.ObjectConversion.fromString.

Converts a String to a Boolean

Parameters
input:String

a String to be converted into a Boolean value.

Returns:Boolean

true if the input String is part of BooleanConversion#trueValues, false if the input String is part of BooleanConversion#falseValues, or BooleanConversion#getValueIfStringIsNull() if the input String is null.

Annotations
@Override
getBooleanback to summary
public static Boolean getBoolean(String booleanString, String[] trueValues, String[] falseValues)

Returns the Boolean value represented by a String, as defined by sets of Strings that represent true and false values.

Parameters
booleanString:String

the value that represents either true or false

trueValues:String[]

a set of possible string values that represent true. If empty, then "true" will be assumed as the only acceptable representation.

falseValues:String[]

a set of possible string values that represent false. If empty, then "false" will be assumed as the only acceptable representation.

Returns:Boolean

the boolean value that the input string represents

Exceptions
DataProcessingException:
if the input string does not match any value provided in neither set of possible values.
getBooleanback to summary
private static Boolean getBoolean(String defaultString, Set<String> trueValues, Set<String> falseValues)
normalizeback to summary
private static String normalize(String string)

Normalizes a given String by trimming whitespaces and converting it to lower case.

Parameters
string:String

a String to be normalized.

Returns:String

the normalized version of the original String.

normalizeback to summary
private static void normalize(Collection<String> strings)

Normalizes the Strings in a given collection by trimming all elements and converting them to lower case.

Parameters
strings:Collection<String>

a String collection with elements to be normalized. The original contents of the collection will be modified.

revertback to summary
public String revert(Boolean input)

Overrides com.univocity.parsers.conversions.ObjectConversion.revert.

Implements com.univocity.parsers.conversions.Conversion.revert.

Converts a Boolean back to a String

The return value depends on the list of values for true/false provided in the constructor of this class.

Parameters
input:Boolean

the Boolean to be converted to a String

Returns:String

a String representation for this boolean value, or the value of BooleanConversion#getValueIfObjectIsNull() if the Boolean input is null.

Annotations
@Override