Skip to content

Commit

Permalink
Tweaked the library to support inheritance
Browse files Browse the repository at this point in the history
- Updated FixedLength.java
- Updated ParserTest.java
- Updated FormatterTest.java
- Created InheritedEmployee.java
- Created AbstractPerson.java
  • Loading branch information
Oliver Stewart committed Apr 29, 2024
1 parent 881e63a commit 86e2e65
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/main/java/name/velikodniy/vitaliy/fixedlength/FixedLength.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private FixedFormatLine<T> classToLineDesc(final Class<? extends T> clazz) {
if (annotation != null) {
fixedFormatLine.startsWith = annotation.startsWith();
}
for (Field field : clazz.getDeclaredFields()) {
for (Field field : getAllFields(clazz)) {
FixedField fieldAnnotation = field.getDeclaredAnnotation(FixedField.class);
if (fieldAnnotation == null) {
continue;
Expand All @@ -71,6 +71,17 @@ private FixedFormatLine<T> classToLineDesc(final Class<? extends T> clazz) {
return fixedFormatLine;
}

List<Field> getAllFields(final Class<?> clazz) {
if (clazz == null) {
return Collections.emptyList();
}

List<Field> result = new ArrayList<>(getAllFields(clazz.getSuperclass()));
List<Field> filteredFields = Arrays.stream(clazz.getDeclaredFields()).collect(Collectors.toList());
result.addAll(filteredFields);
return result;
}

public FixedLength<T> registerLineType(final Class<? extends T> lineClass) {
lineTypes.add(classToLineDesc(lineClass));
return this;
Expand Down Expand Up @@ -306,7 +317,8 @@ public String format(List<T> lines) {

for (T line : lines) {

Arrays.stream(line.getClass().getDeclaredFields())
getAllFields(line.getClass())
.stream()
.filter(
f ->
f.getAnnotation(FixedField.class) != null
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/AbstractPerson.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import name.velikodniy.vitaliy.fixedlength.Align;
import name.velikodniy.vitaliy.fixedlength.annotation.FixedField;

public abstract class AbstractPerson {
@FixedField(offset = 1, length = 10, align = Align.LEFT)
public String firstName;

@FixedField(offset = 11, length = 10, align = Align.LEFT)
String lastName;
}
15 changes: 15 additions & 0 deletions src/test/java/FormatterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,19 @@ void simpleFormat() {

}


@Test
@DisplayName("Simple string format class hierarchy")
void simpleFormatInherited() {

FixedLength<Row> impl = new FixedLength<Row>()
.registerLineType(InheritedEmployee.class);

List<Row> parse = impl
.parse(new ByteArrayInputStream(singleTypeExample.getBytes()));

assertEquals(singleTypeExample, impl.format(parse));

}

}
18 changes: 18 additions & 0 deletions src/test/java/InheritedEmployee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import name.velikodniy.vitaliy.fixedlength.Align;
import name.velikodniy.vitaliy.fixedlength.annotation.FixedField;

import java.math.BigDecimal;
import java.time.LocalDate;

public class InheritedEmployee extends AbstractPerson implements Row {

@FixedField(offset = 21, length = 10, align = Align.LEFT)
protected String title;

@FixedField(offset = 31, length = 6, padding = '0')
private BigDecimal salary;

@FixedField(offset = 37, length = 8, format = "MMddyyyy")
public LocalDate hireDate;

}
15 changes: 15 additions & 0 deletions src/test/java/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,21 @@ class ParserTest {
"CatSnowball 20200103@" +
"EmplJoe3 Smith Developer ";

@Test
@DisplayName("Parse as input stream with default charset and one line type")
void testParseInheritedOneLineType() throws FixedLengthException {
List<Row> parse = new FixedLength<Row>()
.registerLineType(InheritedEmployee.class)
.parse(new ByteArrayInputStream(singleTypeExample.getBytes()));

assertEquals(2, parse.size());
parse.forEach( e ->{
assertNotNull(((InheritedEmployee)e).firstName);
assertNotNull(((InheritedEmployee)e).lastName);
});
}


@Test
@DisplayName("Parse as input stream with default charset and one line type")
void testParseOneLineType() throws FixedLengthException {
Expand Down

0 comments on commit 86e2e65

Please sign in to comment.