001package org.javasimon.utils.bean;
002
003/**
004 * Converter from String to Boolean.
005 *
006 * @author <a href="mailto:ivan.mushketyk@gmail.com">Ivan Mushketyk</a>
007 */
008public class ToBooleanConverter implements Converter {
009
010        @Override
011        public Boolean convert(Class<?> targetClass, String strVal) throws ConvertException {
012                if (strVal == null) {
013                        return null;
014                }
015
016                strVal = strVal.toLowerCase();
017                if (strVal.equals("true")) {
018                        return true;
019                } else if (strVal.equals("false")) {
020                        return false;
021                } else {
022                        throw new ConvertException(String.format("Failed to parse '%s' as Boolean", strVal));
023                }
024        }
025}