All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.datalking.common.convert.support.StringToBooleanConverter Maven / Gradle / Ivy

package com.github.datalking.common.convert.support;

import com.github.datalking.common.convert.converter.Converter;

import java.util.HashSet;
import java.util.Set;

/**
 */
public final class StringToBooleanConverter implements Converter {

	private static final Set trueValues = new HashSet<>(4);

	private static final Set falseValues = new HashSet<>(4);

	static {
		trueValues.add("true");
		trueValues.add("on");
		trueValues.add("yes");
		trueValues.add("1");

		falseValues.add("false");
		falseValues.add("off");
		falseValues.add("no");
		falseValues.add("0");
	}

	public Boolean convert(String source) {
		String value = source.trim();
		if ("".equals(value)) {
			return null;
		}
		value = value.toLowerCase();
		if (trueValues.contains(value)) {
			return Boolean.TRUE;
		}
		else if (falseValues.contains(value)) {
			return Boolean.FALSE;
		}
		else {
			throw new IllegalArgumentException("Invalid boolean value '" + source + "'");
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy