From ea4887c3c165aa74558e1a2f7cf989ca4051f723 Mon Sep 17 00:00:00 2001 From: Vitaliy Velikodniy Date: Sun, 13 Oct 2024 15:14:30 +0300 Subject: [PATCH] add keeping whitespaces option --- build.gradle | 2 +- .../velikodniy/vitaliy/fixedlength/FixedLength.java | 2 +- .../vitaliy/fixedlength/annotation/FixedField.java | 8 ++++++++ .../velikodniy/vitaliy/fixedlength/ParserTest.java | 12 ++++++++++++ .../velikodniy/vitaliy/fixedlength/StringHolder.java | 8 ++++++++ 5 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 src/test/java/name/velikodniy/vitaliy/fixedlength/StringHolder.java diff --git a/build.gradle b/build.gradle index 28394ba..b2f9b1b 100644 --- a/build.gradle +++ b/build.gradle @@ -16,7 +16,7 @@ plugins { } group 'name.velikodniy.vitaliy' -version '0.12' +version '0.13' repositories { mavenLocal() diff --git a/src/main/java/name/velikodniy/vitaliy/fixedlength/FixedLength.java b/src/main/java/name/velikodniy/vitaliy/fixedlength/FixedLength.java index dd96b5a..1f8fee5 100644 --- a/src/main/java/name/velikodniy/vitaliy/fixedlength/FixedLength.java +++ b/src/main/java/name/velikodniy/vitaliy/fixedlength/FixedLength.java @@ -326,7 +326,7 @@ private boolean acceptFieldContent(String content, FixedField fieldAnnotation) { if (content == null) { return false; } - if (content.trim().isEmpty()) { + if (content.trim().isEmpty() && !fieldAnnotation.allowEmptyStrings()) { return false; } if (fieldAnnotation.ignore().isEmpty()) { diff --git a/src/main/java/name/velikodniy/vitaliy/fixedlength/annotation/FixedField.java b/src/main/java/name/velikodniy/vitaliy/fixedlength/annotation/FixedField.java index ab8f82a..7bc0ae8 100644 --- a/src/main/java/name/velikodniy/vitaliy/fixedlength/annotation/FixedField.java +++ b/src/main/java/name/velikodniy/vitaliy/fixedlength/annotation/FixedField.java @@ -57,4 +57,12 @@ * @return pattern matching content to ignore */ String ignore() default ""; + + /** + * Allows empty string as value, by default it is false. + * If true - values will be kept as is + * If false - value will be null + * @return allows empty string as value boolean + */ + boolean allowEmptyStrings() default false; } diff --git a/src/test/java/name/velikodniy/vitaliy/fixedlength/ParserTest.java b/src/test/java/name/velikodniy/vitaliy/fixedlength/ParserTest.java index b03d754..28be9bd 100644 --- a/src/test/java/name/velikodniy/vitaliy/fixedlength/ParserTest.java +++ b/src/test/java/name/velikodniy/vitaliy/fixedlength/ParserTest.java @@ -230,4 +230,16 @@ void testParseMixedLineTypeCustomPredicate() throws FixedLengthException { assertEquals(2, parse.size()); } + + @Test + @DisplayName("Parse object with whitespaces preventing it to cast as null") + void testParseWhitespaceNonNull() throws FixedLengthException { + + List holders = new FixedLength() + .registerLineType(StringHolder.class) + .parse(new ByteArrayInputStream(" some text to drop".getBytes())); + + assertEquals(1, holders.size()); + assertEquals(" ", holders.get(0).value); + } } diff --git a/src/test/java/name/velikodniy/vitaliy/fixedlength/StringHolder.java b/src/test/java/name/velikodniy/vitaliy/fixedlength/StringHolder.java new file mode 100644 index 0000000..a9c0eb3 --- /dev/null +++ b/src/test/java/name/velikodniy/vitaliy/fixedlength/StringHolder.java @@ -0,0 +1,8 @@ +package name.velikodniy.vitaliy.fixedlength; + +import name.velikodniy.vitaliy.fixedlength.annotation.FixedField; + +public class StringHolder { + @FixedField(offset = 1, length = 3, padding = Character.MIN_VALUE, allowEmptyStrings = true) + public String value; +}