Skip to content

Commit 7a700ed

Browse files
committed
understand "on"/"off", "yes"/"no", "1"/"0" as boolean values (analogous to CustomBooleanEditor)
1 parent b465f20 commit 7a700ed

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToBooleanConverter.java

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,53 @@
1616

1717
package org.springframework.core.convert.support;
1818

19+
import java.util.HashSet;
20+
import java.util.Set;
21+
1922
import org.springframework.core.convert.converter.Converter;
23+
import org.springframework.util.StringUtils;
2024

2125
/**
2226
* Converts String to a Boolean.
2327
*
2428
* @author Keith Donald
29+
* @author Juergen Hoeller
2530
* @since 3.0
2631
*/
2732
final class StringToBooleanConverter implements Converter<String, Boolean> {
2833

34+
private static final Set<String> trueValues = new HashSet<String>(4);
35+
36+
private static final Set<String> falseValues = new HashSet<String>(4);
37+
38+
static {
39+
trueValues.add("true");
40+
falseValues.add("false");
41+
42+
trueValues.add("on");
43+
falseValues.add("off");
44+
45+
trueValues.add("yes");
46+
falseValues.add("no");
47+
48+
trueValues.add("1");
49+
falseValues.add("0");
50+
}
51+
52+
2953
public Boolean convert(String source) {
30-
if (source.equals("")) {
54+
String value = (source != null ? source.trim() : null);
55+
if (!StringUtils.hasLength(value)) {
3156
return null;
32-
} else if (source.equals("true")) {
57+
}
58+
else if (trueValues.contains(value)) {
3359
return Boolean.TRUE;
3460
}
35-
else if (source.equals("false")) {
61+
else if (falseValues.contains(value)) {
3662
return Boolean.FALSE;
3763
}
3864
else {
39-
throw new IllegalArgumentException("Invalid boolean string '" + source + "'; expected \"\", 'true', or 'false'");
65+
throw new IllegalArgumentException("Invalid boolean value '" + source + "'");
4066
}
4167
}
4268

org.springframework.core/src/test/java/org/springframework/core/convert/support/DefaultConversionServiceTests.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@
1616

1717
package org.springframework.core.convert.support;
1818

19-
import static org.junit.Assert.assertEquals;
20-
import static org.junit.Assert.fail;
21-
2219
import java.math.BigDecimal;
2320
import java.math.BigInteger;
2421
import java.util.Locale;
2522

23+
import static org.junit.Assert.*;
2624
import org.junit.Test;
25+
2726
import org.springframework.core.convert.converter.Converter;
2827

2928
/**
@@ -56,12 +55,18 @@ public void testStringToCharacterInvalidString() {
5655
public void testStringToBooleanTrue() {
5756
StringToBooleanConverter c = new StringToBooleanConverter();
5857
assertEquals(Boolean.valueOf(true), c.convert("true"));
58+
assertEquals(Boolean.valueOf(true), c.convert("on"));
59+
assertEquals(Boolean.valueOf(true), c.convert("yes"));
60+
assertEquals(Boolean.valueOf(true), c.convert("1"));
5961
}
6062

6163
@Test
6264
public void testStringToBooleanFalse() {
6365
StringToBooleanConverter c = new StringToBooleanConverter();
6466
assertEquals(Boolean.valueOf(false), c.convert("false"));
67+
assertEquals(Boolean.valueOf(false), c.convert("off"));
68+
assertEquals(Boolean.valueOf(false), c.convert("no"));
69+
assertEquals(Boolean.valueOf(false), c.convert("0"));
6570
}
6671

6772
@Test

0 commit comments

Comments
 (0)