diff --git a/README.md b/README.md index 682b2c6..d5615a8 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ ```java @DateFormatter(format = "yyyy-MM-dd") +@DecimalFormatter(format = "#.00") @StringFormatter(format = Format.UPPERCASE) ``` @@ -19,3 +20,14 @@ new CSVGenerator(List data, char delimiter).generate(); new ExcelGenerator(List data).generate(); ``` + +##Date Formatter## + +Format used by java.text.SimpleDateFormat + +##Decimal Formatter## +Format used by java.text.DecimalFormat + +##String Formatter## +- Format.UPPERCASE +- Format.LOWERCASE diff --git a/pom.xml b/pom.xml index 45bdefa..334740a 100644 --- a/pom.xml +++ b/pom.xml @@ -26,9 +26,19 @@ 1.8 - org.apache.poi - poi-ooxml - 4.1.2 + org.apache.poi + poi-ooxml + 4.1.2 + + + org.apache.pdfbox + pdfbox + 2.0.20 + + + com.itextpdf + itextpdf + 5.5.13.1 org.projectlombok diff --git a/src/main/java/com/ljunggren/reportGenerator/Generator.java b/src/main/java/com/ljunggren/reportGenerator/Generator.java index 073d185..26b0900 100644 --- a/src/main/java/com/ljunggren/reportGenerator/Generator.java +++ b/src/main/java/com/ljunggren/reportGenerator/Generator.java @@ -11,6 +11,7 @@ import com.ljunggren.reportGenerator.annotation.Reportable; import com.ljunggren.reportGenerator.annotation.formatter.DateFormatterChain; +import com.ljunggren.reportGenerator.annotation.formatter.DecimalFormatterChain; import com.ljunggren.reportGenerator.annotation.formatter.FormatterCatchAll; import com.ljunggren.reportGenerator.annotation.formatter.StringFormatterChain; @@ -55,9 +56,7 @@ private List generateHeaders(List fields) { List headers = new ArrayList(); - for (Field field: fields) { - headers.add(field.getAnnotation(Reportable.class).headerName()); - } + fields.forEach(field -> headers.add(field.getAnnotation(Reportable.class).headerName())); return headers; } @@ -84,10 +83,9 @@ protected String getValueFromItem(Item item) { return new DateFormatterChain().nextChain( new StringFormatterChain().nextChain( + new DecimalFormatterChain().nextChain( new FormatterCatchAll() - )).format(item); + ))).format(item); } - - } diff --git a/src/main/java/com/ljunggren/reportGenerator/annotation/DecimalFormatter.java b/src/main/java/com/ljunggren/reportGenerator/annotation/DecimalFormatter.java new file mode 100644 index 0000000..13c4225 --- /dev/null +++ b/src/main/java/com/ljunggren/reportGenerator/annotation/DecimalFormatter.java @@ -0,0 +1,14 @@ +package com.ljunggren.reportGenerator.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface DecimalFormatter { + + String format(); + +} diff --git a/src/main/java/com/ljunggren/reportGenerator/annotation/formatter/DateFormatterChain.java b/src/main/java/com/ljunggren/reportGenerator/annotation/formatter/DateFormatterChain.java index 82fede1..d8288b5 100644 --- a/src/main/java/com/ljunggren/reportGenerator/annotation/formatter/DateFormatterChain.java +++ b/src/main/java/com/ljunggren/reportGenerator/annotation/formatter/DateFormatterChain.java @@ -6,13 +6,13 @@ import com.ljunggren.reportGenerator.Item; import com.ljunggren.reportGenerator.annotation.DateFormatter; -public class DateFormatterChain extends FormatterChain{ +public class DateFormatterChain extends FormatterChain { public String format(Item item) { DateFormatter formatter = item.getField().getAnnotation(DateFormatter.class); if (formatter != null && item.getValue() instanceof Date) { - SimpleDateFormat format = new SimpleDateFormat(formatter.format()); - return format.format(item.getValue()); + SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatter.format()); + return simpleDateFormat.format(item.getValue()); } return nextChain.format(item); } diff --git a/src/main/java/com/ljunggren/reportGenerator/annotation/formatter/DecimalFormatterChain.java b/src/main/java/com/ljunggren/reportGenerator/annotation/formatter/DecimalFormatterChain.java new file mode 100644 index 0000000..8e779da --- /dev/null +++ b/src/main/java/com/ljunggren/reportGenerator/annotation/formatter/DecimalFormatterChain.java @@ -0,0 +1,26 @@ +package com.ljunggren.reportGenerator.annotation.formatter; + +import java.text.DecimalFormat; + +import com.ljunggren.reportGenerator.Item; +import com.ljunggren.reportGenerator.annotation.DecimalFormatter; + +public class DecimalFormatterChain extends FormatterChain { + + @Override + public String format(Item item) { + DecimalFormatter formatter = item.getField().getAnnotation(DecimalFormatter.class); + if (formatter != null && isNumberInstance(item.getValue())) { + DecimalFormat decimalFormat = new DecimalFormat(formatter.format()); + return decimalFormat.format(item.getValue()); + } + return nextChain.format(item); + } + + private boolean isNumberInstance(Object value) { + return value instanceof Integer || + value instanceof Double || + value instanceof Float; + } + +} diff --git a/src/main/java/com/ljunggren/reportGenerator/annotation/formatter/FormatterCatchAll.java b/src/main/java/com/ljunggren/reportGenerator/annotation/formatter/FormatterCatchAll.java index 60e20c1..b83775c 100644 --- a/src/main/java/com/ljunggren/reportGenerator/annotation/formatter/FormatterCatchAll.java +++ b/src/main/java/com/ljunggren/reportGenerator/annotation/formatter/FormatterCatchAll.java @@ -5,7 +5,7 @@ public class FormatterCatchAll extends FormatterChain { public String format(Item item) { - return item.getValue().toString(); + return item.getValue() != null ? item.getValue().toString() : null; } } diff --git a/src/test/java/com/ljunggren/reportGenerator/ExcelGeneratorTest.java b/src/test/java/com/ljunggren/reportGenerator/ExcelGeneratorTest.java index 042277b..55df94a 100644 --- a/src/test/java/com/ljunggren/reportGenerator/ExcelGeneratorTest.java +++ b/src/test/java/com/ljunggren/reportGenerator/ExcelGeneratorTest.java @@ -54,7 +54,7 @@ } @Test - public void generatorTest() { + public void generateTest() { ExcelGenerator generator = new ExcelGenerator(data); Workbook workbook = generator.generate(); assertEquals(1, workbook.getNumberOfSheets()); diff --git a/src/test/java/com/ljunggren/reportGenerator/annotation/formatter/DateFormatterChainTest.java b/src/test/java/com/ljunggren/reportGenerator/annotation/formatter/DateFormatterChainTest.java index 898cc1c..592b945 100644 --- a/src/test/java/com/ljunggren/reportGenerator/annotation/formatter/DateFormatterChainTest.java +++ b/src/test/java/com/ljunggren/reportGenerator/annotation/formatter/DateFormatterChainTest.java @@ -26,14 +26,6 @@ private Date date; } - @Getter - @AllArgsConstructor - private class BadDateFormatPojo { - @Reportable(headerName = "Date", order = 0) - @DateFormatter(format = "nonsense") - private Date date; - } - private Date createDate() { try { return new SimpleDateFormat("yyyy/MM/dd").parse("2020/06/28"); @@ -54,6 +46,26 @@ assertEquals(expected, csv); } + @Test + public void formatNullTest() { + DateFormatPojo pojo = new DateFormatPojo(null); + CSVGenerator generator = new CSVGenerator(Arrays.asList(new DateFormatPojo[] {pojo}), ','); + String csv = generator.generate(); + String expected = new StringBuilder() + .append("Date\r\n") + .append("\r\n") + .toString(); + assertEquals(expected, csv); + } + + @Getter + @AllArgsConstructor + private class BadDateFormatPojo { + @Reportable(headerName = "Date", order = 0) + @DateFormatter(format = "nonsense") + private Date date; + } + @Test(expected = IllegalArgumentException.class) public void badFormatTest() { BadDateFormatPojo pojo = new BadDateFormatPojo(createDate()); diff --git a/src/test/java/com/ljunggren/reportGenerator/annotation/formatter/DecimalFormatterChainTest.java b/src/test/java/com/ljunggren/reportGenerator/annotation/formatter/DecimalFormatterChainTest.java new file mode 100644 index 0000000..5cacb57 --- /dev/null +++ b/src/test/java/com/ljunggren/reportGenerator/annotation/formatter/DecimalFormatterChainTest.java @@ -0,0 +1,98 @@ +package com.ljunggren.reportGenerator.annotation.formatter; + +import static org.junit.Assert.assertEquals; + +import java.util.Arrays; + +import org.junit.Test; + +import com.ljunggren.reportGenerator.CSVGenerator; +import com.ljunggren.reportGenerator.annotation.DecimalFormatter; +import com.ljunggren.reportGenerator.annotation.Reportable; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +public class DecimalFormatterChainTest { + + @Getter + @AllArgsConstructor + private class IntFormatPojo { + @Reportable(headerName = "Balance", order = 0) + @DecimalFormatter(format = "#.00") + private int balance; + } + + @Test + public void formatIntTest() { + IntFormatPojo pojo = new IntFormatPojo(250); + CSVGenerator generator = new CSVGenerator(Arrays.asList(new IntFormatPojo[] {pojo}), ','); + String csv = generator.generate(); + String expected = new StringBuilder() + .append("Balance\r\n") + .append("250.00\r\n") + .toString(); + assertEquals(expected, csv); + } + + @Getter + @AllArgsConstructor + private class DoubleFormatPojo { + @Reportable(headerName = "Balance", order = 0) + @DecimalFormatter(format = "#.00") + private double balance; + } + + @Test + public void formatDoubleTest() { + DoubleFormatPojo pojo = new DoubleFormatPojo(250.123); + CSVGenerator generator = new CSVGenerator(Arrays.asList(new DoubleFormatPojo[] {pojo}), ','); + String csv = generator.generate(); + String expected = new StringBuilder() + .append("Balance\r\n") + .append("250.12\r\n") + .toString(); + assertEquals(expected, csv); + } + + @Getter + @AllArgsConstructor + private class FloatFormatPojo { + @Reportable(headerName = "Balance", order = 0) + @DecimalFormatter(format = "#.00") + private float balance; + } + + @Test + public void formatFloatTest() { + FloatFormatPojo pojo = new FloatFormatPojo(250.123f); + CSVGenerator generator = new CSVGenerator(Arrays.asList(new FloatFormatPojo[] {pojo}), ','); + String csv = generator.generate(); + String expected = new StringBuilder() + .append("Balance\r\n") + .append("250.12\r\n") + .toString(); + assertEquals(expected, csv); + } + + @Getter + @AllArgsConstructor + private class FloatObjectFormatPojo { + @Reportable(headerName = "Balance", order = 0) + @DecimalFormatter(format = "#.00") + private Float balance; + } + + @Test + public void formatFloatObjectTest() { + FloatObjectFormatPojo pojo = new FloatObjectFormatPojo(null); + CSVGenerator generator = new CSVGenerator(Arrays.asList(new FloatObjectFormatPojo[] {pojo}), ','); + String csv = generator.generate(); + String expected = new StringBuilder() + .append("Balance\r\n") + .append("\r\n") + .toString(); + assertEquals(expected, csv); + } + +} diff --git a/src/test/java/com/ljunggren/reportGenerator/annotation/formatter/FormatterCatchAllTest.java b/src/test/java/com/ljunggren/reportGenerator/annotation/formatter/FormatterCatchAllTest.java index cb031f1..a03d9ca 100644 --- a/src/test/java/com/ljunggren/reportGenerator/annotation/formatter/FormatterCatchAllTest.java +++ b/src/test/java/com/ljunggren/reportGenerator/annotation/formatter/FormatterCatchAllTest.java @@ -33,4 +33,16 @@ assertEquals(expected, csv); } + @Test + public void formatCatchAllNullTest() { + Pojo pojo = new Pojo(null); + CSVGenerator generator = new CSVGenerator(Arrays.asList(new Pojo[] {pojo}), ','); + String csv = generator.generate(); + String expected = new StringBuilder() + .append("Name\r\n") + .append("\r\n") + .toString(); + assertEquals(expected, csv); + } + } diff --git a/src/test/java/com/ljunggren/reportGenerator/annotation/formatter/StringFormatterChainTest.java b/src/test/java/com/ljunggren/reportGenerator/annotation/formatter/StringFormatterChainTest.java index b5979af..07e4db6 100644 --- a/src/test/java/com/ljunggren/reportGenerator/annotation/formatter/StringFormatterChainTest.java +++ b/src/test/java/com/ljunggren/reportGenerator/annotation/formatter/StringFormatterChainTest.java @@ -56,4 +56,16 @@ assertEquals(expected, csv); } + @Test + public void formatLowercaseNullTest() { + LowercasePojo pojo = new LowercasePojo(null); + CSVGenerator generator = new CSVGenerator(Arrays.asList(new LowercasePojo[] {pojo}), ','); + String csv = generator.generate(); + String expected = new StringBuilder() + .append("Name\r\n") + .append("\r\n") + .toString(); + assertEquals(expected, csv); + } + }