2014-08-12 15:38:23 +00:00
|
|
|
package com.moandjiezana.toml;
|
|
|
|
|
2014-08-13 12:31:50 +00:00
|
|
|
import static com.moandjiezana.toml.ValueConverterUtils.INVALID;
|
2014-08-12 14:44:47 +00:00
|
|
|
|
2014-08-13 12:27:23 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import org.parboiled.Parboiled;
|
|
|
|
import org.parboiled.parserunners.BasicParseRunner;
|
|
|
|
|
2014-08-13 12:31:50 +00:00
|
|
|
class BooleanConverter implements ValueConverter {
|
2014-08-12 14:44:47 +00:00
|
|
|
|
2014-08-13 12:31:50 +00:00
|
|
|
static final BooleanConverter BOOLEAN_PARSER = new BooleanConverter();
|
2014-08-12 14:44:47 +00:00
|
|
|
|
|
|
|
@Override
|
2014-08-13 12:31:50 +00:00
|
|
|
public boolean canConvert(String s) {
|
2014-08-12 14:44:47 +00:00
|
|
|
return s.startsWith("true") || s.startsWith("false");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-08-13 12:31:50 +00:00
|
|
|
public Object convert(String s) {
|
2014-08-13 12:36:21 +00:00
|
|
|
ValueParser parser = Parboiled.createParser(ValueParser.class);
|
2014-08-13 12:27:23 +00:00
|
|
|
|
|
|
|
List<String> resultValue = new BasicParseRunner<List<String>>(parser.Boolean()).run(s).resultValue;
|
|
|
|
|
|
|
|
if (resultValue == null) {
|
2014-08-12 15:38:23 +00:00
|
|
|
return INVALID;
|
2014-08-12 14:44:47 +00:00
|
|
|
}
|
|
|
|
|
2014-08-13 12:27:23 +00:00
|
|
|
return Boolean.valueOf(resultValue.get(0));
|
2014-08-12 14:44:47 +00:00
|
|
|
}
|
|
|
|
|
2014-08-13 12:31:50 +00:00
|
|
|
private BooleanConverter() {}
|
2014-08-12 14:44:47 +00:00
|
|
|
}
|