CSVRow { @Nullable public Boolean parseBoolean(T csvColumn) { String value = parse(csvColumn); if (hasNullValue(value)) { return null; } String lowerCase = value.toLowerCase(); switch (lowerCase) { case "": return null; case "1": case "true": return true; case "0": case "false": return false; default: throw new NumberFormatException(lowerCase + " is neither true nor false"); } } CSVRow(ImmutableMap<String, String> lineByHeader); boolean hasEntry(T csvColumn); String parse(T csvColumn); Integer parseInt(T csvColumn); Double parseDouble(T csvColumn); BigDecimal parseBigDecimal(T csvColumn); @Nullable Boolean parseBoolean(T csvColumn); E parseEnum(T csvColumn, Class<E> enumClass); final ImmutableMap<String, String> lineByHeader; }
@Test void testParseTrueBoolean() { CSVRow<TestCSVColumn> mixedCaseTrue = CSVRowBuilder.create(TestCSVColumn.class) .withValue(TestCSVColumn.BOOLEAN, "tRue") .build(); CSVRow<TestCSVColumn> numberRepresentationTrue = CSVRowBuilder.create(TestCSVColumn.class) .withValue(TestCSVColumn.BOOLEAN, 1) .build(); Assertions.assertTrue(mixedCaseTrue.parseBoolean(TestCSVColumn.BOOLEAN)); Assertions.assertTrue(numberRepresentationTrue.parseBoolean(TestCSVColumn.BOOLEAN)); }
@Test void testParseFalseBoolean() { CSVRow<TestCSVColumn> mixedCaseFalse = CSVRowBuilder.create(TestCSVColumn.class) .withValue(TestCSVColumn.BOOLEAN, "FAlsE") .build(); CSVRow<TestCSVColumn> numberRepresentationFalse = CSVRowBuilder.create(TestCSVColumn.class) .withValue(TestCSVColumn.BOOLEAN, 0) .build(); Assertions.assertFalse(mixedCaseFalse.parseBoolean(TestCSVColumn.BOOLEAN)); Assertions.assertFalse(numberRepresentationFalse.parseBoolean(TestCSVColumn.BOOLEAN)); }
@Test void testParseMissingBoolean() { Assertions.assertNull(EMPTY_ROW.parseBoolean(TestCSVColumn.BOOLEAN)); }
@Test void testParseIncorrectBoolean() { Assertions.assertThrows(NumberFormatException.class, () -> INCORRECT_ROW.parseBoolean(TestCSVColumn.BOOLEAN)); }