com.bbn.bue.common.converters.StrictStringToBoolean Maven / Gradle / Ivy
The newest version!
package com.bbn.bue.common.converters;
import com.google.common.annotations.Beta;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Decodes "true" and "false" values and disallows others.
*
* @author rgabbard
*/
@Beta
public class StrictStringToBoolean implements StringConverter {
/**
* If s
is "true" returns true
; if "false", returns false
;
* otherwise, throws a ValidationException.
*
* @param s May not be null.
*/
@Override
public Boolean decode(final String s) {
checkNotNull(s);
if ("true".equals(s)) {
return Boolean.TRUE;
} else if ("false".equals(s)) {
return Boolean.FALSE;
} else {
throw new ConversionException(
String.format("Strictly-converted booleans must be either 'true' or 'false', not %s", s));
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy