diff --git a/src/main/java/io/github/dgroup/xlsx/cell/CellOf.java b/src/main/java/io/github/dgroup/xlsx/cell/CellOf.java index 15b35a4..f6797f0 100644 --- a/src/main/java/io/github/dgroup/xlsx/cell/CellOf.java +++ b/src/main/java/io/github/dgroup/xlsx/cell/CellOf.java @@ -24,11 +24,11 @@ package io.github.dgroup.xlsx.cell; +import io.github.dgroup.xlsx.style.Style; +import io.github.dgroup.xlsx.style.StyleOf; import java.util.Objects; import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFRow; -import org.apache.poi.xssf.usermodel.XSSFSheet; -import org.cactoos.Func; import org.cactoos.Proc; import org.cactoos.Scalar; import org.cactoos.func.UncheckedProc; @@ -106,21 +106,27 @@ public CellOf(final String cid, final Scalar val) { * @param val The excel cell value. */ public CellOf(final Scalar cid, final Scalar val) { - this(cid, val, sheet -> { - return sheet.getWorkbook().createCellStyle(); - }); + this(cid, val, new Style.No()); } /** * Ctor. * @param cid The number of excel cell. * @param val The excel cell value. - * @param style The excel cell formatting style. + * @param style The style of excel cell. */ - protected CellOf( - final Scalar cid, final Scalar val, final Func style - ) { - this(cid, val, new Change<>(cid, val, style)); + public CellOf(final Scalar cid, final Scalar val, final XSSFCellStyle style) { + this(cid, val, new StyleOf(style)); + } + + /** + * Ctor. + * @param cid The number of excel cell. + * @param val The excel cell value. + * @param styling The procedure to apply the style to the excel cell. + */ + public CellOf(final Scalar cid, final Scalar val, final Style styling) { + this(cid, val, new Change<>(cid, val, styling)); } /** diff --git a/src/main/java/io/github/dgroup/xlsx/cell/Change.java b/src/main/java/io/github/dgroup/xlsx/cell/Change.java index 68849c9..a7af08e 100644 --- a/src/main/java/io/github/dgroup/xlsx/cell/Change.java +++ b/src/main/java/io/github/dgroup/xlsx/cell/Change.java @@ -24,18 +24,15 @@ package io.github.dgroup.xlsx.cell; +import io.github.dgroup.xlsx.style.Style; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.ZoneId; import java.util.Date; import org.apache.poi.xssf.usermodel.XSSFCell; -import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFRow; -import org.apache.poi.xssf.usermodel.XSSFSheet; -import org.cactoos.Func; import org.cactoos.Proc; import org.cactoos.Scalar; -import org.cactoos.func.UncheckedFunc; import org.cactoos.scalar.Unchecked; /** @@ -59,22 +56,22 @@ public final class Change implements Proc { /** * Function to evaluate formatting style of the cell in Excel row. */ - private final UncheckedFunc style; + private final Style styling; /** * Ctor. * @param cid The number of cell in Excel row. * @param val The value of cell in Excel row. - * @param style Function to evaluate formatting style of the cell in Excel row. + * @param styling The procedure to apply the style to the excel cell. */ public Change( final Scalar cid, final Scalar val, - final Func style + final Style styling ) { this.index = new Unchecked<>(cid); this.val = new Unchecked<>(val); - this.style = new UncheckedFunc<>(style); + this.styling = styling; } @Override @@ -83,7 +80,7 @@ public void exec(final XSSFRow row) { XSSFCell cell = row.getCell(this.index.value()); if (cell == null) { cell = row.createCell(this.index.value()); - cell.setCellStyle(this.style.apply(row.getSheet())); + this.styling.exec(row.getSheet(), cell); } final T value = this.val.value(); if (value instanceof Double) { diff --git a/src/main/java/io/github/dgroup/xlsx/cell/DateOf.java b/src/main/java/io/github/dgroup/xlsx/cell/DateOf.java index b59d045..524c4db 100644 --- a/src/main/java/io/github/dgroup/xlsx/cell/DateOf.java +++ b/src/main/java/io/github/dgroup/xlsx/cell/DateOf.java @@ -24,10 +24,10 @@ package io.github.dgroup.xlsx.cell; +import io.github.dgroup.xlsx.style.Style; import java.time.LocalDate; import java.time.ZoneId; import org.apache.poi.xssf.usermodel.XSSFCell; -import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.cactoos.Scalar; import org.cactoos.scalar.Sticky; @@ -48,7 +48,8 @@ public DateOf(final XSSFCell cell) { () -> cell.getDateCellValue() .toInstant() .atZone(ZoneId.systemDefault()) - .toLocalDate() + .toLocalDate(), + (sheet, actual) -> actual.setCellStyle(cell.getCellStyle()) ); } @@ -56,54 +57,39 @@ public DateOf(final XSSFCell cell) { * Ctor. * @param cid The number of excel cell. * @param val The excel cell value. + * @param styling The procedure to apply the style to the excel cell. */ - public DateOf(final int cid, final LocalDate val) { - this(() -> cid, () -> val); + public DateOf(final int cid, final LocalDate val, final Style styling) { + this(() -> cid, () -> val, styling); } /** * Ctor. * @param cid The number of excel cell. * @param val The excel cell value. + * @param styling The procedure to apply the style to the excel cell. */ - public DateOf(final String cid, final LocalDate val) { - this(cid, () -> val); + public DateOf(final String cid, final LocalDate val, final Style styling) { + this(cid, () -> val, styling); } /** * Ctor. * @param cid The number of excel cell. * @param val The excel cell value. + * @param styling The procedure to apply the style to the excel cell. */ - public DateOf(final String cid, final Scalar val) { - this(new Sticky<>(new IndexOf(cid)), val); + public DateOf(final String cid, final Scalar val, final Style styling) { + this(new Sticky<>(new IndexOf(cid)), val, styling); } /** * Ctor. * @param cid The number of excel cell. * @param val The excel cell value. + * @param styling The procedure to apply the style to the excel cell. */ - public DateOf(final Scalar cid, final Scalar val) { - this(cid, val, "yyyy-mm-dd"); - } - - /** - * Ctor. - * @param cid The number of excel cell. - * @param val The excel cell value. - * @param pattern The format of the date for target {@link XSSFCell}. - */ - public DateOf(final Scalar cid, final Scalar val, final String pattern) { - super(cid, val, sheet -> { - final XSSFCellStyle date = sheet.getWorkbook().createCellStyle(); - date.setDataFormat( - sheet.getWorkbook() - .getCreationHelper() - .createDataFormat() - .getFormat(pattern) - ); - return date; - }); + public DateOf(final Scalar cid, final Scalar val, final Style styling) { + super(cid, val, styling); } } diff --git a/src/main/java/io/github/dgroup/xlsx/cell/MonthOf.java b/src/main/java/io/github/dgroup/xlsx/cell/MonthOf.java index 7b0c9d2..7fa170e 100644 --- a/src/main/java/io/github/dgroup/xlsx/cell/MonthOf.java +++ b/src/main/java/io/github/dgroup/xlsx/cell/MonthOf.java @@ -24,9 +24,12 @@ package io.github.dgroup.xlsx.cell; +import io.github.dgroup.xlsx.style.Style; +import io.github.dgroup.xlsx.style.StyleOf; import java.time.YearMonth; import java.time.format.TextStyle; import java.util.Locale; +import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.cactoos.scalar.Sticky; /** @@ -42,6 +45,30 @@ public final class MonthOf extends CellOf { * @param month The cell value. */ public MonthOf(final int cid, final YearMonth month) { - super(cid, new Sticky<>(() -> month.getMonth().getDisplayName(TextStyle.SHORT, Locale.US))); + this(cid, month, new Style.No()); + } + + /** + * Ctor. + * @param cid The number of excel column number. + * @param month The cell value. + * @param style The style of excel cell. + */ + public MonthOf(final int cid, final YearMonth month, final XSSFCellStyle style) { + this(cid, month, new StyleOf(style)); + } + + /** + * Ctor. + * @param cid The number of excel column number. + * @param month The cell value. + * @param styling The procedure to apply the style to the excel cell. + */ + public MonthOf(final int cid, final YearMonth month, final Style styling) { + super( + () -> cid, + new Sticky<>(() -> month.getMonth().getDisplayName(TextStyle.SHORT, Locale.US)), + styling + ); } } diff --git a/src/main/java/io/github/dgroup/xlsx/cell/Range.java b/src/main/java/io/github/dgroup/xlsx/cell/Range.java index 6164631..7375ca3 100644 --- a/src/main/java/io/github/dgroup/xlsx/cell/Range.java +++ b/src/main/java/io/github/dgroup/xlsx/cell/Range.java @@ -24,6 +24,7 @@ package io.github.dgroup.xlsx.cell; +import io.github.dgroup.xlsx.style.Style; import java.util.List; import java.util.Objects; import java.util.concurrent.atomic.AtomicInteger; @@ -60,6 +61,11 @@ public final class Range implements MutableCell> { */ private final Unchecked tostr; + /** + * The procedure to apply the style to the excel cell. + */ + private final Style styling; + /** * Ctor. * @param cid The number of excel column number. @@ -75,7 +81,7 @@ public Range(final int cid, final T... values) { * @param values The cell(s) values. */ public Range(final int cid, final List values) { - this(() -> cid, values); + this(() -> cid, values, new Style.No()); } /** @@ -83,6 +89,7 @@ public Range(final int cid, final List values) { * @param cid The name of excel column number. * @param values The cell(s) values. */ + @SafeVarargs public Range(final String cid, final T... values) { this(cid, new ListOf<>(values)); } @@ -93,17 +100,19 @@ public Range(final String cid, final T... values) { * @param values The cell(s) values. */ public Range(final String cid, final List values) { - this(new Sticky<>(new IndexOf(cid)), values); + this(new Sticky<>(new IndexOf(cid)), values, new Style.No()); } /** * Ctor. * @param cid The number of excel column number. * @param values The cell(s) values. + * @param styling The procedure to apply the style to the excel cell. */ - public Range(final Scalar cid, final List values) { + public Range(final Scalar cid, final List values, final Style styling) { this.cid = new Unchecked<>(cid); this.values = values; + this.styling = styling; this.tostr = new Unchecked<>( new Sticky<>( () -> String.format( @@ -119,7 +128,7 @@ public Range(final Scalar cid, final List values) { public void change(final XSSFRow row) { final AtomicInteger column = new AtomicInteger(this.index()); for (final T val : this.values) { - new CellOf<>(column.getAndIncrement(), val) + new CellOf<>(column::getAndIncrement, () -> val, this.styling) .change(row); } } diff --git a/src/main/java/io/github/dgroup/xlsx/style/Style.java b/src/main/java/io/github/dgroup/xlsx/style/Style.java new file mode 100644 index 0000000..d48df5a --- /dev/null +++ b/src/main/java/io/github/dgroup/xlsx/style/Style.java @@ -0,0 +1,56 @@ +/* + * MIT License + * + * Copyright (c) 2019-2020 Yurii Dubinka + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom + * the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE + * OR OTHER DEALINGS IN THE SOFTWARE. + */ +package io.github.dgroup.xlsx.style; + +import org.apache.poi.xssf.usermodel.XSSFCell; +import org.apache.poi.xssf.usermodel.XSSFSheet; +import org.cactoos.BiProc; + +/** + * The procedure to apply the style to the excel cell. + * + * @since 0.2.1 + */ +public interface Style extends BiProc { + + /** + * Apply the expected style to the target excel cell. + * @param sheet The current excel sheet. + * @param cell The target excel cell. + */ + void exec(XSSFSheet sheet, XSSFCell cell); + + /** + * No style defined for excel cell. + * + * @since 0.3.0 + */ + class No implements Style { + + @Override + public void exec(final XSSFSheet sheet, final XSSFCell cell) { + // No implementation required + } + } +} diff --git a/src/main/java/io/github/dgroup/xlsx/style/StyleOf.java b/src/main/java/io/github/dgroup/xlsx/style/StyleOf.java new file mode 100644 index 0000000..c43529c --- /dev/null +++ b/src/main/java/io/github/dgroup/xlsx/style/StyleOf.java @@ -0,0 +1,97 @@ +/* + * MIT License + * + * Copyright (c) 2019-2020 Yurii Dubinka + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom + * the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE + * OR OTHER DEALINGS IN THE SOFTWARE. + */ +package io.github.dgroup.xlsx.style; + +import org.apache.poi.xssf.usermodel.XSSFCell; +import org.apache.poi.xssf.usermodel.XSSFCellStyle; +import org.apache.poi.xssf.usermodel.XSSFSheet; +import org.cactoos.Scalar; +import org.cactoos.scalar.Sticky; +import org.cactoos.scalar.Unchecked; + +/** + * The style to be applied to the target excel cell(s). + * + * @since 0.2.1 + */ +public final class StyleOf implements Style { + + /** + * The expected style to be applied. + */ + private final Unchecked style; + + /** + * Build default style from target sheet. + * @param sheet The target excel sheet. + */ + public StyleOf(final XSSFSheet sheet) { + this(new Sticky<>(() -> sheet.getWorkbook().createCellStyle())); + } + + /** + * Build date style from target sheet. + * @param sheet The target excel sheet. + * @param pattern The date format for excel cell(s). + */ + public StyleOf(final XSSFSheet sheet, final String pattern) { + this( + new Sticky<>( + () -> { + final XSSFCellStyle date = sheet.getWorkbook().createCellStyle(); + date.setDataFormat( + sheet.getWorkbook() + .getCreationHelper() + .createDataFormat() + .getFormat(pattern) + ); + return date; + } + ) + ); + } + + /** + * Ctor. + * @param style The expected style to be applied to target excel cell(s). + */ + public StyleOf(final XSSFCellStyle style) { + this(() -> style); + } + + /** + * Ctor. + * @param style The expected style to be applied to target excel cell(s). + */ + public StyleOf(final Scalar style) { + this.style = new Unchecked<>(style); + } + + @Override + public void exec(final XSSFSheet sheet, final XSSFCell cell) { + if (this.style != null) { + cell.setCellStyle(this.style.value()); + } + } +} diff --git a/src/main/java/io/github/dgroup/xlsx/style/package-info.java b/src/main/java/io/github/dgroup/xlsx/style/package-info.java new file mode 100644 index 0000000..9dac9bd --- /dev/null +++ b/src/main/java/io/github/dgroup/xlsx/style/package-info.java @@ -0,0 +1,30 @@ +/* + * MIT License + * + * Copyright (c) 2019-2020 Yurii Dubinka + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom + * the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE + * OR OTHER DEALINGS IN THE SOFTWARE. + */ + +/** + * The classes related to Apache POI styles. + * + * @since 0.3.0 + */ +package io.github.dgroup.xlsx.style;