Browse Source

understand "on"/"off", "yes"/"no", "1"/"0" as boolean values (analogous to CustomBooleanEditor)

pull/23217/head
Juergen Hoeller 15 years ago
parent
commit
7a700edaa7
  1. 34
      org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToBooleanConverter.java
  2. 11
      org.springframework.core/src/test/java/org/springframework/core/convert/support/DefaultConversionServiceTests.java

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

@ -16,27 +16,53 @@ @@ -16,27 +16,53 @@
package org.springframework.core.convert.support;
import java.util.HashSet;
import java.util.Set;
import org.springframework.core.convert.converter.Converter;
import org.springframework.util.StringUtils;
/**
* Converts String to a Boolean.
*
* @author Keith Donald
* @author Juergen Hoeller
* @since 3.0
*/
final class StringToBooleanConverter implements Converter<String, Boolean> {
private static final Set<String> trueValues = new HashSet<String>(4);
private static final Set<String> falseValues = new HashSet<String>(4);
static {
trueValues.add("true");
falseValues.add("false");
trueValues.add("on");
falseValues.add("off");
trueValues.add("yes");
falseValues.add("no");
trueValues.add("1");
falseValues.add("0");
}
public Boolean convert(String source) {
if (source.equals("")) {
String value = (source != null ? source.trim() : null);
if (!StringUtils.hasLength(value)) {
return null;
} else if (source.equals("true")) {
}
else if (trueValues.contains(value)) {
return Boolean.TRUE;
}
else if (source.equals("false")) {
else if (falseValues.contains(value)) {
return Boolean.FALSE;
}
else {
throw new IllegalArgumentException("Invalid boolean string '" + source + "'; expected \"\", 'true', or 'false'");
throw new IllegalArgumentException("Invalid boolean value '" + source + "'");
}
}

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

@ -16,14 +16,13 @@ @@ -16,14 +16,13 @@
package org.springframework.core.convert.support;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Locale;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.core.convert.converter.Converter;
/**
@ -56,12 +55,18 @@ public class DefaultConversionServiceTests { @@ -56,12 +55,18 @@ public class DefaultConversionServiceTests {
public void testStringToBooleanTrue() {
StringToBooleanConverter c = new StringToBooleanConverter();
assertEquals(Boolean.valueOf(true), c.convert("true"));
assertEquals(Boolean.valueOf(true), c.convert("on"));
assertEquals(Boolean.valueOf(true), c.convert("yes"));
assertEquals(Boolean.valueOf(true), c.convert("1"));
}
@Test
public void testStringToBooleanFalse() {
StringToBooleanConverter c = new StringToBooleanConverter();
assertEquals(Boolean.valueOf(false), c.convert("false"));
assertEquals(Boolean.valueOf(false), c.convert("off"));
assertEquals(Boolean.valueOf(false), c.convert("no"));
assertEquals(Boolean.valueOf(false), c.convert("0"));
}
@Test

Loading…
Cancel
Save