Skip to content

Commit

Permalink
Added append methods that accept custom styler.
Browse files Browse the repository at this point in the history
  • Loading branch information
payammeyer committed Sep 25, 2024
1 parent 82e5448 commit 82cfaff
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,55 +7,83 @@
import java.util.Date;

public abstract class SpreadsheetTarget<R, C extends SpreadsheetTargetConfig<?, ?>> implements AutoCloseable {

private final SpreadsheetTargetConfig<R, C> dataConfig;

public SpreadsheetTarget(SpreadsheetTargetConfig<R, C> dataConfig) {
this.dataConfig = dataConfig;
}

public void appendValue(Collection<?> value) {
dataConfig.getCollectionHandler().writeType(generateReference(), value);
}

public void appendValue(Boolean value) {
dataConfig.getBooleanTypeHandler().writeType(generateReference(), value);
}

public void appendValue(Date value) {
dataConfig.getDateTypeHandler().writeType(generateReference(), value);
}

public void appendValue(Number value) {
dataConfig.getNumberTypeHandler().writeType(generateReference(), value);
}

public void appendValue(String value) {
dataConfig.getStringTypeHandler().writeType(generateReference(), value);
}

public void appendValue(Link link) {
dataConfig.getLinkTypeHandler().writeType(generateReference(), link);
}

public void appendGenericValue(Object o) {
dataConfig.getDefaultTypeHandler().writeType(generateReference(), o);
}


public void appendValue(SpreadsheetTypeHandler<R, Collection<?>> spreadsheetTypeHandler, Collection<?> value) {
spreadsheetTypeHandler.writeType(generateReference(), value);
}

public void appendValue(SpreadsheetTypeHandler<R, Boolean> spreadsheetTypeHandler, Boolean value) {
spreadsheetTypeHandler.writeType(generateReference(), value);
}

public void appendValue(SpreadsheetTypeHandler<R, Date> spreadsheetTypeHandler, Date value) {
spreadsheetTypeHandler.writeType(generateReference(), value);
}

public void appendValue(SpreadsheetTypeHandler<R, Number> spreadsheetTypeHandler, Number value) {
spreadsheetTypeHandler.writeType(generateReference(), value);
}

public void appendValue(SpreadsheetTypeHandler<R, String> spreadsheetTypeHandler, String value) {
spreadsheetTypeHandler.writeType(generateReference(), value);
}

public void appendValue(SpreadsheetTypeHandler<R, Link> spreadsheetTypeHandler, Link value) {
spreadsheetTypeHandler.writeType(generateReference(), value);
}

public void appendGenericValue(SpreadsheetTypeHandler<R, Object> spreadsheetTypeHandler, Object o) {
spreadsheetTypeHandler.writeType(generateReference(), o);
}

protected void writeHeaders(Collection<String> headers) {
SpreadsheetTypeHandler<R, String> headerCellHandler = dataConfig.getHeaderHandler();
for (String header : headers) {
headerCellHandler.writeType(generateReference(), header);
}
finishRow();
}

protected abstract R generateReference();

public void appendLink(String label, String href) {
appendValue(new Link(label, href));
}

public void appendValue(Object o) {
switch (o) {
case Collection<?> collection -> appendValue(collection);
Expand All @@ -67,35 +95,35 @@ public void appendValue(Object o) {
case null, default -> appendGenericValue(o);
}
}

public void appendValues(String... values) {
for (String value : values) {
appendValue(value);
}
}

public void writeRow(String... values) {
for (String value : values) {
appendValue(value);
}
finishRow();
}

public void writeRow(Object... values) {
for (Object value : values) {
appendValue(value);
}
finishRow();
}

public void writeRow(Collection<?> values) {
for (Object value : values) {
appendValue(value);
}
finishRow();
}

public abstract void finishRow();

public abstract void close() throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
import java.util.Date;

public abstract class SpreadsheetTargetConfig<T, S extends SpreadsheetTargetConfig<?, ?>> {

private final DataOutputStream dataStream;

private Collection<String> headers;

private DelimitedListHandler delimitedListHandler = new DefaultDelimitedListHandler(';');

private SpreadsheetTypeHandler<T, String> stringTypeHandler;
private SpreadsheetTypeHandler<T, Date> dateTypeHandler;
private SpreadsheetTypeHandler<T, Number> numberTypeHandler;
Expand All @@ -24,110 +24,120 @@ public abstract class SpreadsheetTargetConfig<T, S extends SpreadsheetTargetConf
private SpreadsheetTypeHandler<T, Link> linkTypeHandler;
private SpreadsheetTypeHandler<T, Object> defaultTypeHandler;
private SpreadsheetTypeHandler<T, String> headerHandler;

private SpreadsheetTypeHandler<T, String> boldHandler;

public SpreadsheetTargetConfig(DataOutputStream dataStream) {
this.dataStream = dataStream;
withListDelimiter(';');
}

public DataOutputStream getDataStream() {
return dataStream;
}

protected abstract S getSelf();

public S withListDelimiter(char listDelimiter) {
this.delimitedListHandler = new DefaultDelimitedListHandler(listDelimiter);
return getSelf();
}

public S withDelimitedListHandler(DelimitedListHandler delimitedListHandler) {
this.delimitedListHandler = delimitedListHandler;
return getSelf();
}

public S withHeaders(Collection<String> headers) {
this.headers = headers;
return getSelf();
}

public Collection<String> getHeaders() {
return headers;
}

public DelimitedListHandler getDelimitedListHandler() {
return delimitedListHandler;
}

public SpreadsheetTypeHandler<T, String> getStringTypeHandler() {
return stringTypeHandler;
}

public S withStringHandler(SpreadsheetTypeHandler<T, String> stringTypeHandler) {
this.stringTypeHandler = stringTypeHandler;
return getSelf();
}

public SpreadsheetTypeHandler<T, Date> getDateTypeHandler() {
return dateTypeHandler;
}

public S withDateTypeHandler(SpreadsheetTypeHandler<T, Date> dateTypeHandler) {
this.dateTypeHandler = dateTypeHandler;
return getSelf();
}

public SpreadsheetTypeHandler<T, Number> getNumberTypeHandler() {
return numberTypeHandler;
}

public S withNumberTypeHandler(SpreadsheetTypeHandler<T, Number> numberTypeHandler) {
this.numberTypeHandler = numberTypeHandler;
return getSelf();
}

public SpreadsheetTypeHandler<T, Boolean> getBooleanTypeHandler() {
return booleanTypeHandler;
}

public S withBooleanTypeHandler(SpreadsheetTypeHandler<T, Boolean> booleanTypeHandler) {
this.booleanTypeHandler = booleanTypeHandler;
return getSelf();
}

public SpreadsheetTypeHandler<T, Collection<?>> getCollectionHandler() {
return collectionHandler;
}

public S withCollectionHandler(SpreadsheetTypeHandler<T, Collection<?>> collectionHandler) {
this.collectionHandler = collectionHandler;
return getSelf();
}

public SpreadsheetTypeHandler<T, Link> getLinkTypeHandler() {
return linkTypeHandler;
}

public S withLinkTypeHandler(SpreadsheetTypeHandler<T, Link> linkTypeHandler) {
this.linkTypeHandler = linkTypeHandler;
return getSelf();
}

public SpreadsheetTypeHandler<T, Object> getDefaultTypeHandler() {
return defaultTypeHandler;
}

public S withDefaultTypeHandler(SpreadsheetTypeHandler<T, Object> defaultTypeHandler) {
this.defaultTypeHandler = defaultTypeHandler;
return getSelf();
}

public SpreadsheetTypeHandler<T, String> getHeaderHandler() {
return headerHandler;
}

public S withHeaderHandler(SpreadsheetTypeHandler<T, String> headerHandler) {
this.headerHandler = headerHandler;
return getSelf();
}

public SpreadsheetTypeHandler<T, String> getBoldHandler() {
return boldHandler;
}

public S withBoldHandler(SpreadsheetTypeHandler<T, String> boldHandler) {
this.boldHandler = boldHandler;
return getSelf();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected CellReference generateReference() {
SXSSFCell cell = getNextCell();
return new CellReference(workbookHelper, cell);
}

public SXSSFCell getNextCell() {
if (this.row == null) {
this.row = this.sheet.createRow(this.rowIdx);
Expand All @@ -79,7 +79,7 @@ public void finishRow() {
colIdx = 0;
row = null;
}

public void newSheet(String sheetName) {
newSheet(sheetName, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@

import io.zulia.data.output.DataOutputStream;
import io.zulia.data.target.spreadsheet.SpreadsheetTargetConfig;
import io.zulia.data.target.spreadsheet.excel.cell.*;
import io.zulia.data.target.spreadsheet.excel.cell.BoldCellHandler;
import io.zulia.data.target.spreadsheet.excel.cell.BooleanCellHandler;
import io.zulia.data.target.spreadsheet.excel.cell.CellReference;
import io.zulia.data.target.spreadsheet.excel.cell.CollectionCellHandler;
import io.zulia.data.target.spreadsheet.excel.cell.DateCellHandler;
import io.zulia.data.target.spreadsheet.excel.cell.DefaultCellHandler;
import io.zulia.data.target.spreadsheet.excel.cell.HeaderCellHandler;
import io.zulia.data.target.spreadsheet.excel.cell.LinkCellHandler;
import io.zulia.data.target.spreadsheet.excel.cell.NumberCellHandler;
import io.zulia.data.target.spreadsheet.excel.cell.StringCellHandler;

public class ExcelTargetConfig extends SpreadsheetTargetConfig<CellReference, ExcelTargetConfig> {

Expand All @@ -22,7 +31,7 @@ private ExcelTargetConfig(DataOutputStream dataStream) {
withBooleanTypeHandler(new BooleanCellHandler());
withCollectionHandler(new CollectionCellHandler(this));
withHeaderHandler(new HeaderCellHandler());

withBoldHandler(new BoldCellHandler());
}

public ExcelTargetConfig getSelf() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.zulia.data.target.spreadsheet.excel.cell;

import io.zulia.data.target.spreadsheet.SpreadsheetTypeHandler;
import io.zulia.data.target.spreadsheet.excel.WorkbookHelper;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.xssf.streaming.SXSSFCell;

public class BoldCellHandler implements SpreadsheetTypeHandler<CellReference, String> {

@Override
public void writeType(CellReference reference, String value) {
SXSSFCell cell = reference.cell();
if (value != null) {
WorkbookHelper workbookHelper = reference.workbookHelper();
CellStyle linkStyle = workbookHelper.createOrGetStyle("boldStyle", style -> {
Font boldFont = workbookHelper.createOrGetFont("boldFont", font -> {
font.setBold(true);
font.setFontHeightInPoints((short) 10);
});
style.setFont(boldFont);
});
cell.setCellStyle(linkStyle);
cell.setCellValue(value);
}
else {
cell.setBlank();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void excelTarget() throws IOException {
excelTarget.finishRow();

excelTarget.appendValue(2);
excelTarget.appendValue("Tigers");
excelTarget.appendValue(excelTargetConfig.getBoldHandler(), "Tigers");
excelTarget.finishRow();

excelTarget.appendValue(3);
Expand Down Expand Up @@ -87,4 +87,8 @@ public void writeSampleRecords(SpreadsheetTarget<?, ?> spreadsheetTarget) {
spreadsheetTarget.appendValue(new Date());
spreadsheetTarget.finishRow();
}

public static void main(String[] args) throws IOException {
new DataTargetExamples().excelTarget();
}
}

0 comments on commit 82cfaff

Please sign in to comment.