Skip to content

Commit

Permalink
#14: No styling by default
Browse files Browse the repository at this point in the history
Fixed #14.
  • Loading branch information
dgroup committed Jan 21, 2020
1 parent 0061547 commit 793f2cd
Show file tree
Hide file tree
Showing 8 changed files with 261 additions and 53 deletions.
26 changes: 16 additions & 10 deletions src/main/java/io/github/dgroup/xlsx/cell/CellOf.java
Expand Up @@ -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;
Expand Down Expand Up @@ -106,21 +106,27 @@ public CellOf(final String cid, final Scalar<T> val) {
* @param val The excel cell value.
*/
public CellOf(final Scalar<Integer> cid, final Scalar<T> 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<Integer> cid, final Scalar<T> val, final Func<XSSFSheet, XSSFCellStyle> style
) {
this(cid, val, new Change<>(cid, val, style));
public CellOf(final Scalar<Integer> cid, final Scalar<T> 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<Integer> cid, final Scalar<T> val, final Style styling) {
this(cid, val, new Change<>(cid, val, styling));
}

/**
Expand Down
15 changes: 6 additions & 9 deletions src/main/java/io/github/dgroup/xlsx/cell/Change.java
Expand Up @@ -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;

/**
Expand All @@ -59,22 +56,22 @@ public final class Change<T> implements Proc<XSSFRow> {
/**
* Function to evaluate formatting style of the cell in Excel row.
*/
private final UncheckedFunc<XSSFSheet, XSSFCellStyle> 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<Integer> cid,
final Scalar<T> val,
final Func<XSSFSheet, XSSFCellStyle> style
final Style styling
) {
this.index = new Unchecked<>(cid);
this.val = new Unchecked<>(val);
this.style = new UncheckedFunc<>(style);
this.styling = styling;
}

@Override
Expand All @@ -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) {
Expand Down
44 changes: 15 additions & 29 deletions src/main/java/io/github/dgroup/xlsx/cell/DateOf.java
Expand Up @@ -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;

Expand All @@ -48,62 +48,48 @@ public DateOf(final XSSFCell cell) {
() -> cell.getDateCellValue()
.toInstant()
.atZone(ZoneId.systemDefault())
.toLocalDate()
.toLocalDate(),
(sheet, actual) -> actual.setCellStyle(cell.getCellStyle())
);
}

/**
* 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<LocalDate> val) {
this(new Sticky<>(new IndexOf(cid)), val);
public DateOf(final String cid, final Scalar<LocalDate> 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<Integer> cid, final Scalar<LocalDate> 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<Integer> cid, final Scalar<LocalDate> 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<Integer> cid, final Scalar<LocalDate> val, final Style styling) {
super(cid, val, styling);
}
}
29 changes: 28 additions & 1 deletion src/main/java/io/github/dgroup/xlsx/cell/MonthOf.java
Expand Up @@ -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;

/**
Expand All @@ -42,6 +45,30 @@ public final class MonthOf extends CellOf<String> {
* @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
);
}
}
17 changes: 13 additions & 4 deletions src/main/java/io/github/dgroup/xlsx/cell/Range.java
Expand Up @@ -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;
Expand Down Expand Up @@ -60,6 +61,11 @@ public final class Range<T> implements MutableCell<List<T>> {
*/
private final Unchecked<String> tostr;

/**
* The procedure to apply the style to the excel cell.
*/
private final Style styling;

/**
* Ctor.
* @param cid The number of excel column number.
Expand All @@ -75,14 +81,15 @@ public Range(final int cid, final T... values) {
* @param values The cell(s) values.
*/
public Range(final int cid, final List<T> values) {
this(() -> cid, values);
this(() -> cid, values, new Style.No());
}

/**
* Ctor.
* @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));
}
Expand All @@ -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<T> 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<Integer> cid, final List<T> values) {
public Range(final Scalar<Integer> cid, final List<T> values, final Style styling) {
this.cid = new Unchecked<>(cid);
this.values = values;
this.styling = styling;
this.tostr = new Unchecked<>(
new Sticky<>(
() -> String.format(
Expand All @@ -119,7 +128,7 @@ public Range(final Scalar<Integer> cid, final List<T> 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);
}
}
Expand Down
56 changes: 56 additions & 0 deletions 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<XSSFSheet, XSSFCell> {

/**
* 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
}
}
}

0 comments on commit 793f2cd

Please sign in to comment.