Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,6 @@
*/
public final class BooleanConverter extends AbstractConverter<Boolean> {

/**
* Copies the provided array, and ensures that all the strings in the newly created array contain only lower-case letters.
* <p>
* Using this method to copy string arrays means that changes to the src array do not modify the dst array.
* </p>
*/
private static String[] copyStrings(final String[] src) {
final String[] dst = new String[src.length];
for (int i = 0; i < src.length; ++i) {
dst[i] = toLowerCase(src[i]);
}
return dst;
}

/**
* The set of strings that are known to map to Boolean.TRUE.
*/
Expand Down Expand Up @@ -97,8 +83,8 @@ public BooleanConverter(final Boolean defaultValue) {
* @since 1.8.0
*/
public BooleanConverter(final String[] trueStrings, final String[] falseStrings) {
this.trueStrings = copyStrings(trueStrings);
this.falseStrings = copyStrings(falseStrings);
this.trueStrings = trueStrings.clone();
this.falseStrings = falseStrings.clone();
}

/**
Expand All @@ -115,8 +101,8 @@ public BooleanConverter(final String[] trueStrings, final String[] falseStrings)
*/
public BooleanConverter(final String[] trueStrings, final String[] falseStrings, final Boolean defaultValue) {
super(defaultValue);
this.trueStrings = copyStrings(trueStrings);
this.falseStrings = copyStrings(falseStrings);
this.trueStrings = trueStrings.clone();
this.falseStrings = falseStrings.clone();
}

/**
Expand All @@ -136,20 +122,16 @@ public BooleanConverter(final String[] trueStrings, final String[] falseStrings,
@Override
protected <T> T convertToType(final Class<T> type, final Object value) throws Throwable {
if (Boolean.class.equals(type) || Boolean.TYPE.equals(type)) {
// All the values in the trueStrings and falseStrings arrays are
// guaranteed to be lower-case. By converting the input value
// to lowercase too, we can use the efficient String.equals method
// instead of the less-efficient String.equalsIgnoreCase method.
final String stringValue = toLowerCase(value);
final String stringValue = toString(value);

for (final String trueString : trueStrings) {
if (trueString.equals(stringValue)) {
if (trueString.equalsIgnoreCase(stringValue)) {
return type.cast(Boolean.TRUE);
}
}

for (final String falseString : falseStrings) {
if (falseString.equals(stringValue)) {
if (falseString.equalsIgnoreCase(stringValue)) {
return type.cast(Boolean.FALSE);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ class BooleanConverterTest {

@Test
void testAdditionalStrings() {
final String[] trueStrings = { "sure" };
final String[] falseStrings = { "nope" };
final String[] trueStrings = { "sure", "ναι" };
final String[] falseStrings = { "nope", "hayır" };
final AbstractConverter<Boolean> converter = new BooleanConverter(trueStrings, falseStrings);
testConversionValues(converter, new String[] { "sure", "Sure" }, new String[] { "nope", "nOpE" });
testConversionValues(converter, new String[] { "sure", "Sure", "ναι", "Ναι" }, new String[] { "nope", "nOpE", "hayır", "Hayır" });

assertThrows(ConversionException.class, () -> converter.convert(Boolean.class, "true"));
assertThrows(ConversionException.class, () -> converter.convert(Boolean.class, "bogus"));
Expand Down